Article.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. return View::fetch();
  55. }
  56. /**
  57. * 文章点赞
  58. */
  59. public function dolike()
  60. {
  61. $id = input('post.id');
  62. $res = ArticleModel::where('id', $id)->inc('likes')->update();
  63. if ($res===false) {
  64. return ['code' => 2];
  65. } else {
  66. return ['code' => 0, 'msg'=>'未知错误'];
  67. }
  68. }
  69. /**
  70. * 标签列表
  71. */
  72. public function tags($name=null)
  73. {
  74. if (!$name) {
  75. throw new HttpException(404, '标签不可为空');
  76. }
  77. $list = ArticleTagsModel::tagsList($name);
  78. View::assign([
  79. 'list' => $list,
  80. 'tag' => $name
  81. ]);
  82. return View::fetch();
  83. }
  84. /**
  85. * 时光轴
  86. */
  87. public function time()
  88. {
  89. $list =ArticleModel::field('id,cid,title,titlepic,username,summary,hits,sorts,status,create_time')->with(['category'])->order('update_time desc')->paginate();
  90. View::assign('list', $list);
  91. return View::fetch();
  92. }
  93. }