Article.php 4.2 KB

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