123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 老猫 <thinkcmf@126.com>
- // +----------------------------------------------------------------------
- namespace app\portal\controller;
- use cmf\controller\HomeBaseController;
- use app\portal\model\PortalCategoryModel;
- use app\portal\service\PostService;
- use app\portal\model\PortalPostModel;
- use think\Db;
- class ArticleController extends HomeBaseController
- {
- /**
- * 文章详情
- * @return mixed
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function index()
- {
- $this->getTopNav();
- $portalCategoryModel = new PortalCategoryModel();
- $postService = new PostService();
- $articleId = $this->request->param('id', 0, 'intval');
- $categoryId = $this->request->param('cid', 0, 'intval');
- $article = $postService->publishedArticle($articleId, $categoryId);
- if (empty($article)) {
- abort(404, '文章不存在!');
- }
- $tplName = 'article';
- if (empty($categoryId)) {
- $categories = $article['categories'];
- if (count($categories) > 0) {
- $this->assign('category', $categories[0]);
- } else {
- abort(404, '文章未指定分类!');
- }
- } else {
- $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();
- if (empty($category)) {
- abort(404, '文章不存在!');
- }
- $this->assign('category', $category);
- $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
- }
- Db::name('portal_post')->where('id', $articleId)->setInc('post_hits');
- hook('portal_before_assign_article', $article);
- $this->assign('article', $article);
- $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
- //获取分类
- $id = $article['categories'][0]['id'];
- $portalCategoryModel = new PortalCategoryModel();
- $category = $portalCategoryModel->where(function ($query) use ($id) {
- $query->where('id', $id)->whereOr('parent_id', $id);
- })->where('status', 1)->select();
- $category_self = ['id' => 0];
- $category_child = [];
- foreach ($category as $v) {
- if ($v['id'] == $id) {
- $category_self = $v;
- } else {
- $category_child[] = $v;
- }
- }
- $this->assign('category_self', $category_self);
- $this->assign('category_child', $category_child);
- //导航
- $path_list = [];
- if (!empty($category_self)) {
- $path = explode('-', $category_self['path']);
- array_shift($path);
- $path_list = $portalCategoryModel->where('id', 'in', $path)->select();
- }
- $this->assign('path_list', $path_list);
- return $this->fetch("/" . $tplName);
- }
- // 文章点赞
- public function doLike()
- {
- $this->checkUserLogin();
- $articleId = $this->request->param('id', 0, 'intval');
- $canLike = cmf_check_user_action("posts$articleId", 1);
- if ($canLike) {
- Db::name('portal_post')->where('id', $articleId)->setInc('post_like');
- $this->success("赞好啦!");
- } else {
- $this->error("您已赞过啦!");
- }
- }
- }
|