ArticleController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. class ArticleController extends HomeBaseController
  17. {
  18. /**
  19. * 文章详情
  20. * @return mixed
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public function index()
  27. {
  28. $portalCategoryModel = new PortalCategoryModel();
  29. $postService = new PostService();
  30. $articleId = $this->request->param('id', 0, 'intval');
  31. $categoryId = $this->request->param('cid', 0, 'intval');
  32. $article = $postService->publishedArticle($articleId, $categoryId);
  33. if (empty($article)) {
  34. abort(404, '文章不存在!');
  35. }
  36. $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
  37. $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);
  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. PortalPostModel::where('id', $articleId)->inc('post_hits')->update();
  55. hook('portal_before_assign_article', $article);
  56. $this->assign('article', $article);
  57. $this->assign('prev_article', $prevArticle);
  58. $this->assign('next_article', $nextArticle);
  59. $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
  60. return $this->fetch("/$tplName");
  61. }
  62. // 文章点赞
  63. public function doLike()
  64. {
  65. $this->checkUserLogin();
  66. $articleId = $this->request->param('id', 0, 'intval');
  67. $canLike = cmf_check_user_action("posts$articleId", 1);
  68. if ($canLike) {
  69. PortalPostModel::where('id', $articleId)->inc('post_like')->update();
  70. $this->success("赞好啦!");
  71. } else {
  72. $this->error("您已赞过啦!");
  73. }
  74. }
  75. }