Article.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if ($this->html) {
  78. $this->makeHtmlFile($html);
  79. }
  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 tags($name = null)
  109. {
  110. if (!$name) {
  111. throw new HttpException(404, '标签不可为空');
  112. }
  113. $list = ArticleTagsModel::tagsList($name);
  114. View::assign([
  115. 'list' => $list,
  116. 'tag' => $name
  117. ]);
  118. return View::fetch();
  119. }
  120. /**
  121. * 归档页面(时间)
  122. */
  123. public function archive($year = 0, $month = 0)
  124. {
  125. $yearMonth = $year . '-' . $month;
  126. if ($year == 0 || $month == 0) {
  127. throw new HttpException(404, '日期格式不正确');
  128. }
  129. $list = ArticleModel::queryPage(['yearMonth' => $yearMonth]);
  130. View::assign([
  131. 'list' => $list->all(),
  132. 'total' => $list->total(),
  133. 'limit' => $list->listRows(),
  134. 'page' => $list->currentPage(),
  135. 'lastPage' => $list->lastPage(),
  136. 'pagelist' => $list->render(),
  137. 'yearMonth' => $yearMonth
  138. ]);
  139. $html = View::fetch();
  140. // if ($this->html) {
  141. // $this->makeHtmlFile($html);
  142. // }
  143. return $html;
  144. }
  145. /**
  146. * 时光轴
  147. */
  148. public function time()
  149. {
  150. $params = ['order' => 'update_time desc'];
  151. $list = ArticleModel::queryPage($params);
  152. View::assign([
  153. 'list' => $list->all(),
  154. 'total' => $list->total(),
  155. 'limit' => $list->listRows(),
  156. 'page' => $list->currentPage(),
  157. 'lastPage' => $list->lastPage(),
  158. 'pagelist' => $list->render(),
  159. ]);
  160. $html = View::fetch();
  161. if ($this->html) {
  162. $this->makeHtmlFile($html);
  163. }
  164. return $html;
  165. }
  166. }