Article.php 4.8 KB

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