ArticleController.php
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 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
{
public function index()
{
$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, '文章不存在!');
}
$prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
$nextArticle = $postService->publishedNextArticle($articleId, $categoryId);
$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);
$this->assign('prev_article', $prevArticle);
$this->assign('next_article', $nextArticle);
$tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
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("您已赞过啦!");
}
}
}