Article.php 4.0 KB

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