ArticleController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\portal\controller;
  12. use cmf\controller\HomeBaseController;
  13. use app\portal\model\PortalCategoryModel;
  14. use app\portal\service\PostService;
  15. use app\portal\model\PortalPostModel;
  16. use think\Db;
  17. class ArticleController extends HomeBaseController
  18. {
  19. /**
  20. * 文章详情
  21. * @return mixed
  22. * @throws \think\Exception
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. */
  27. public function index()
  28. {
  29. $this->getTopNav();
  30. $portalCategoryModel = new PortalCategoryModel();
  31. $postService = new PostService();
  32. $articleId = $this->request->param('id', 0, 'intval');
  33. $categoryId = $this->request->param('cid', 0, 'intval');
  34. $article = $postService->publishedArticle($articleId, $categoryId);
  35. if (empty($article)) {
  36. abort(404, '文章不存在!');
  37. }
  38. $tplName = 'article';
  39. if (empty($categoryId)) {
  40. $categories = $article['categories'];
  41. if (count($categories) > 0) {
  42. $this->assign('category', $categories[0]);
  43. } else {
  44. abort(404, '文章未指定分类!');
  45. }
  46. } else {
  47. $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();
  48. if (empty($category)) {
  49. abort(404, '文章不存在!');
  50. }
  51. $this->assign('category', $category);
  52. $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
  53. }
  54. Db::name('portal_post')->where('id', $articleId)->setInc('post_hits');
  55. hook('portal_before_assign_article', $article);
  56. $this->assign('article', $article);
  57. $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
  58. //获取分类
  59. $id = $article['categories'][0]['id'];
  60. $portalCategoryModel = new PortalCategoryModel();
  61. $category = $portalCategoryModel->where(function ($query) use ($id) {
  62. $query->where('id', $id)->whereOr('parent_id', $id);
  63. })->where('status', 1)->select();
  64. $category_self = ['id' => 0];
  65. $category_child = [];
  66. foreach ($category as $v) {
  67. if ($v['id'] == $id) {
  68. $category_self = $v;
  69. } else {
  70. $category_child[] = $v;
  71. }
  72. }
  73. $this->assign('category_self', $category_self);
  74. $this->assign('category_child', $category_child);
  75. //导航
  76. $path_list = [];
  77. if (!empty($category_self)) {
  78. $path = explode('-', $category_self['path']);
  79. array_shift($path);
  80. $path_list = $portalCategoryModel->where('id', 'in', $path)->select();
  81. }
  82. $this->assign('path_list', $path_list);
  83. return $this->fetch("/" . $tplName);
  84. }
  85. // 文章点赞
  86. public function doLike()
  87. {
  88. $this->checkUserLogin();
  89. $articleId = $this->request->param('id', 0, 'intval');
  90. $canLike = cmf_check_user_action("posts$articleId", 1);
  91. if ($canLike) {
  92. Db::name('portal_post')->where('id', $articleId)->setInc('post_like');
  93. $this->success("赞好啦!");
  94. } else {
  95. $this->error("您已赞过啦!");
  96. }
  97. }
  98. }