Article.php 5.0 KB

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