Article.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * 文章控制制器
  6. * @author huwhis@163.com
  7. * @version 0.0.6
  8. * +----------------------------------------------------------------------
  9. */
  10. namespace app\controller;
  11. // 引入框架内置类
  12. use think\facade\View;
  13. use think\exception\HttpException;
  14. use app\model\Article as ArticleModel;
  15. use app\model\ArticleDolikeLog;
  16. use app\model\Tag;
  17. use app\model\TagArticle;
  18. use app\utils\ParsedownUtils;
  19. /**
  20. * 文章
  21. */
  22. class Article extends Base
  23. {
  24. public function index()
  25. {
  26. $params = $this->app->request->param();
  27. $list = ArticleModel::queryPage($params);
  28. View::assign([
  29. 'list' => $list,
  30. 'cid' => $params['cid']
  31. ]);
  32. return View::fetch();
  33. }
  34. /**
  35. * 阅读文章
  36. */
  37. public function read($id = null)
  38. {
  39. $data = ArticleModel::getOne($id);
  40. if (!$data) {
  41. throw new HttpException(404, '页面不存在');
  42. }
  43. $prev_next = ArticleModel::getNextPrev($id, $data->cid);
  44. if ($data->content_type == 1) {
  45. $parsedownExtension = new ParsedownUtils();
  46. // $parsedownExtension->setTocEnabled(true);
  47. $res = $parsedownExtension->text($data->content);
  48. // $data->toc = $res['toc'];
  49. $data->content = $res['content'];
  50. }
  51. View::assign('cid', $data->cid);
  52. View::assign('data', $data);
  53. View::assign('prev_next', $prev_next);
  54. $this->seo['title'] = $data->title;
  55. $this->seo['key'] = $data->keywords;
  56. $this->seo['des'] = $data->summary;
  57. View::assign('seo', $this->seo);
  58. return View::fetch();
  59. }
  60. /**
  61. * 文章点赞
  62. */
  63. public function dolike()
  64. {
  65. $id = $this->request->post('id');
  66. $ip = $this->request->ip();
  67. $log = ArticleDolikeLog::where('aid', $id)->where('ip', $ip)->find();
  68. if ($log) {
  69. return ['code' => 1, 'msg' => '多谢喜欢, 已经点过赞了'];
  70. }
  71. $res = ArticleModel::where('id', $id)->inc('likes')->update();
  72. // 记录 dolike 日志
  73. @ArticleDolikeLog::create([
  74. 'aid' => $id,
  75. 'ip' => $this->request->ip()
  76. ]);
  77. if ($res === false) {
  78. return ['code' => 2, 'msg' => '未知错误'];
  79. } else {
  80. return ['code' => 0];
  81. }
  82. }
  83. /**
  84. * 标签列表
  85. */
  86. public function tag($name = null)
  87. {
  88. if (!$name) {
  89. throw new HttpException(404, '标签不可为空');
  90. }
  91. $tag = Tag::where('tagname', $name)->find();
  92. if (!$tag) {
  93. throw new HttpException(404, '标签不存在');
  94. }
  95. $list = TagArticle::queryList($tag->id);
  96. View::assign([
  97. 'list' => $list,
  98. 'tag' => $name
  99. ]);
  100. return View::fetch();
  101. }
  102. /**
  103. * 归档页面(时间)
  104. */
  105. public function archive($year = 0, $month = 0)
  106. {
  107. if ($year == 0 || $month == 0) {
  108. throw new HttpException(404, '日期格式不正确');
  109. }
  110. $yearMonth = $year . '-' . $month;
  111. $params = $this->app->request->param();
  112. $list = ArticleModel::queryPage($params);
  113. View::assign([
  114. 'list' => $list,
  115. 'yearMonth' => $yearMonth
  116. ]);
  117. return View::fetch();
  118. }
  119. /**
  120. * 时光轴
  121. */
  122. public function time()
  123. {
  124. $params = $this->app->request->param();
  125. $list = ArticleModel::queryPage($params);
  126. View::assign('list', $list);
  127. return View::fetch();
  128. }
  129. /**
  130. * 文章搜索
  131. */
  132. public function search()
  133. {
  134. $key = $this->app->request->has('key') ? $this->app->request->param('key') : "";
  135. if ($key != "") {
  136. $params = $this->app->request->param();
  137. $list = ArticleModel::queryPage($params);
  138. View::assign('list', $list);
  139. }
  140. View::assign("key", $key);
  141. return View::fetch();
  142. }
  143. }