ArticleController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. $portalCategoryModel = new PortalCategoryModel();
  30. $postService = new PostService();
  31. $articleId = $this->request->param('id', 0, 'intval');
  32. $categoryId = $this->request->param('cid', 0, 'intval');
  33. $article = $postService->publishedArticle($articleId, $categoryId);
  34. if (empty($article)) {
  35. abort(404, '文章不存在!');
  36. }
  37. $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
  38. $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);
  39. $tplName = 'article';
  40. if (empty($categoryId)) {
  41. $categories = $article['categories'];
  42. if (count($categories) > 0) {
  43. $this->assign('category', $categories[0]);
  44. } else {
  45. abort(404, '文章未指定分类!');
  46. }
  47. } else {
  48. $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();
  49. if (empty($category)) {
  50. abort(404, '文章不存在!');
  51. }
  52. $this->assign('category', $category);
  53. $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
  54. }
  55. Db::name('portal_post')->where('id', $articleId)->setInc('post_hits');
  56. hook('portal_before_assign_article', $article);
  57. $this->assign('article', $article);
  58. $this->assign('prev_article', $prevArticle);
  59. $this->assign('next_article', $nextArticle);
  60. $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
  61. return $this->fetch("/$tplName");
  62. }
  63. // 文章点赞
  64. public function doLike()
  65. {
  66. $this->checkUserLogin();
  67. $articleId = $this->request->param('id', 0, 'intval');
  68. $canLike = cmf_check_user_action("posts$articleId", 1);
  69. if ($canLike) {
  70. Db::name('portal_post')->where('id', $articleId)->setInc('post_like');
  71. $this->success("赞好啦!");
  72. } else {
  73. $this->error("您已赞过啦!");
  74. }
  75. }
  76. }