Article.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 app\common\model\Category as CategoryModel;
  14. use app\common\model\Article as ArticleModel;
  15. use app\common\model\ArticleTags as ArticleTagsModel;
  16. use think\exception\HttpException;
  17. /**
  18. * 文章管理
  19. */
  20. class Article extends Base
  21. {
  22. protected $modelName = "Article";
  23. public function index()
  24. {
  25. $params = $this->app->request->param();
  26. $list = ArticleModel::queryPage($params);
  27. $baseUrl = $this->request->baseUrl();
  28. View::assign([
  29. 'baseUrl' => $baseUrl,
  30. 'list' => $list->all(),
  31. 'total' => $list->total(),
  32. 'limit' => $list->listRows(),
  33. 'page' => $list->currentPage(),
  34. 'lastPage' => $list->lastPage(),
  35. 'pagelist' => $list->render()
  36. ]);
  37. return View::fetch();
  38. }
  39. /**
  40. * 阅读文章
  41. */
  42. public function read($id=null)
  43. {
  44. $data = ArticleModel::getOne($id);
  45. if (!$data) {
  46. throw new HttpException(404, '页面不存在');
  47. }
  48. $data->hits += 1;
  49. $data->save();
  50. $prev_next = ArticleModel::getNextPrev($id, $data->cid);
  51. View::assign('cid', $data->cid);
  52. View::assign('data', $data);
  53. View::assign('prev_next', $prev_next);
  54. if ($data->content_type==1) {
  55. $parsedownExtension = new \ParsedownExtension();
  56. $parsedownExtension->setTocEnabled(true);
  57. $res = $parsedownExtension->text($data->content);
  58. $data->content = $res['content'];
  59. $data->toc = $res['toc'];
  60. }
  61. return View::fetch();
  62. }
  63. /**
  64. * 文章点赞
  65. */
  66. public function dolike()
  67. {
  68. $id = input('post.id');
  69. $res = ArticleModel::where('id', $id)->inc('likes')->update();
  70. if ($res===false) {
  71. return ['code' => 2];
  72. } else {
  73. return ['code' => 0, 'msg'=>'未知错误'];
  74. }
  75. }
  76. /**
  77. * 标签列表
  78. */
  79. public function tags($name=null)
  80. {
  81. if (!$name) {
  82. throw new HttpException(404, '标签不可为空');
  83. }
  84. $list = ArticleTagsModel::tagsList($name);
  85. View::assign([
  86. 'list' => $list,
  87. 'tag' => $name
  88. ]);
  89. return View::fetch();
  90. }
  91. /**
  92. * 时光轴
  93. */
  94. public function time()
  95. {
  96. $list =ArticleModel::field('id,cid,title,titlepic,username,summary,hits,sorts,status,create_time')->with(['category'])->order('update_time desc')->paginate();
  97. View::assign('list', $list);
  98. return View::fetch();
  99. }
  100. }