Article.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. return View::fetch();
  64. }
  65. /**
  66. * 文章点赞
  67. */
  68. public function dolike()
  69. {
  70. $id = $this->request->post('id');
  71. $ip = $this->request->ip();
  72. $log = ArticleDolikeLog::where('aid', $id)->where('ip', $ip)->find();
  73. if ($log) {
  74. return ['code' => 1, 'msg'=>'多谢喜欢, 已经点过赞了'];
  75. }
  76. $res = ArticleModel::where('id', $id)->inc('likes')->update();
  77. // 记录 dolike 日志
  78. @ArticleDolikeLog::create([
  79. 'aid' => $id,
  80. 'ip' => $this->request->ip()
  81. ]);
  82. if ($res===false) {
  83. return ['code' => 2, 'msg'=>'未知错误'];
  84. } else {
  85. return ['code' => 0];
  86. }
  87. }
  88. /**
  89. * 标签列表
  90. */
  91. public function tags($name=null)
  92. {
  93. if (!$name) {
  94. throw new HttpException(404, '标签不可为空');
  95. }
  96. $list = ArticleTagsModel::tagsList($name);
  97. View::assign([
  98. 'list' => $list,
  99. 'tag' => $name
  100. ]);
  101. return View::fetch();
  102. }
  103. /**
  104. * 归档页面(时间)
  105. */
  106. public function archive($year=0, $month=0)
  107. {
  108. $yearMonth = $year . '-' . $month;
  109. if ($year==0 || $month==0) {
  110. throw new HttpException(404, '日期格式不正确');
  111. }
  112. $list = ArticleModel::queryPage(['yearMonth'=>$yearMonth]);
  113. View::assign([
  114. 'list' => $list,
  115. 'yearMonth' => $yearMonth
  116. ]);
  117. return View::fetch();
  118. }
  119. /**
  120. * 时光轴
  121. */
  122. public function time()
  123. {
  124. $list =ArticleModel::field('id,cid,title,titlepic,username,summary,hits,sort,status,create_time')->with(['category'])->order('update_time desc')->paginate();
  125. View::assign('list', $list);
  126. return View::fetch();
  127. }
  128. }