LikeController.class.php
3.66 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/11
* Time: 15:29
*/
namespace Api\Controller\News;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\DataCenter;
use Common\Common\IntegralStrategy;
use Common\Common\NewsHelper;
use Common\Service\ArticleService;
use Common\Service\LikeService;
use Common\Service\UserActionService;
class LikeController extends \Api\Controller\AbstractController
{
/**
* Like
* @author tangxingguo
* @desc 点赞
* @param Int article_id:true 新闻公告ID
* @return array 积分列表
array(
'integrals' => array(
array(
'number' => 100,
'content' => '',
),
),
);
*/
public function Index_post()
{
$user = $this->_login->user;
// 验证规则
$rules = [
'article_id' => 'require|integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$article_id = $validate->postData['article_id'];
// 新闻检查
$articleServ = new ArticleService();
$newsInfo = $articleServ->get($article_id);
if (empty($newsInfo)) {
E('_ERR_ARTICLE_NOT_FOUND');
}
// 数据库操作
$likeServ = new LikeService();
$likeCount = $likeServ->count_by_conds(['article_id' => $article_id, 'uid' => $user['memUid']]);
if ($likeCount) {
// 已经点过赞
E('_ERR_ARTICLE_ALREADY_LIKE');
}
$data = [
'article_id' => $article_id,
'uid' => $user['memUid'],
'username' => $user['memUsername'],
];
$likeServ->insert($data);
$articleServ->update($article_id, ['like_total = like_total + ?' => 1]);
// 数据中心点赞埋点
$dataCenter = &DataCenter::instance();
$dataCenter->addLike($user, $article_id);
// 数据中心点赞埋点
// 积分、学分埋点
$integrals = $this->_triggerIntegral($newsInfo);
$this->_result = [
'integrals' => $integrals,
];
}
/**
* 触发积分、学分策略
* @param array $article 新闻详情
* @return array
* array(
* 'integrals' => 1, // 获得的积分数
* )
*/
private function _triggerIntegral($article)
{
$actionKey = Constant::INT_ACT_LIKE;
// 记录当前用户的点赞行为
$userActionServ = new UserActionService();
$saveRes = $userActionServ->save($this->uid, $article['article_id'], $actionKey);
if ($saveRes) {
// 点赞次数
$likeTotal = $userActionServ->count_by_conds([
'uid' => $this->uid,
'action_key' => $actionKey,
]);
$triggersType = [
[
'triggerKey' => Constant::INT_TRIGGER_LIKE,
'value' => (int)$likeTotal,
],
];
// 构造策略触发参数
$newsServ = &NewsHelper::instance();
$triggers = $newsServ->buildTrigger($article, $actionKey, $triggersType);
// 触发策略
if (!empty($triggers)) {
$strategyServ = &IntegralStrategy::instance();
$integrals = $strategyServ->triggerPoint($this->uid, $article['article_id'], $actionKey, $triggers);
}
}
return isset($integrals) && !empty($integrals) ? $integrals : [];
}
}