Article.php 5.6 KB

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