FavoriteController.class.php
5.63 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/21
* Time: 10:19
*/
namespace Api\Controller\News;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\RpcFavoriteHelper;
use Common\Common\IntegralStrategy;
use Common\Common\NewsHelper;
use Common\Service\ArticleService;
use Common\Service\AttachService;
use Common\Service\UserActionService;
class FavoriteController extends \Api\Controller\AbstractController
{
/**
* Favorite
* @author liyifei
* @desc 收藏、取消收藏
* @param int article_id:true 新闻公告ID
* @return array 获得积分列表
array(
'integrals' => array( // 每次获得的积分数
array(
'content' => '收藏新闻获得积分',
'number' => 1,
),
),
)
*/
public function Index_post()
{
$user = $this->_login->user;
// 验证规则
$rules = [
'article_id' => 'require|integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 新闻详情
$articleServ = new ArticleService();
$article = $articleServ->get($postData['article_id']);
if (empty($article)) {
E('_ERR_ARTICLE_NOT_FOUND');
}
// 新闻附件详情
$attachServ = new AttachService();
$list = $attachServ->list_by_conds(['article_id' => $article['article_id']]);
// 附件类型:无
$coverType = RpcFavoriteHelper::COVER_TYPE_NONE;
// 附件类型:图文
if ($article['is_show_cover'] == Constant::NEWS_IS_SHOW_COVER_TRUE) {
$coverType = RpcFavoriteHelper::COVER_TYPE_IMAGE;
}
if (!empty($list)) {
$types = array_column($list, 'at_type');
// 附件类型:音频
if (in_array(Constant::ATTACH_TYPE_AUDIO, $types)) {
$coverType = RpcFavoriteHelper::COVER_TYPE_RADIO;
}
// 附件类型:视频
if (in_array(Constant::ATTACH_TYPE_VIDEO, $types)) {
$coverType = RpcFavoriteHelper::COVER_TYPE_VIDEO;
}
}
// RPC查询收藏结果
$data = [
'uid' => $user['memUid'],
'dataId' => $article['article_id'],
];
$rpcFavorite = &RpcFavoriteHelper::instance();
$status = $rpcFavorite->getStatus($data);
if (empty($status) || !isset($status['collection'])) {
E('_ERR_FAVORITE_STATUS_EMPTY');
}
// 根据收藏结果,决定新增/取消收藏
switch ($status['collection']) {
// 未收藏,RPC执行收藏动作
case $rpcFavorite::COLLECTION_NO:
$appName = cfg('IDENTIFIER_NAME');
$data = [
'uid' => $user['memUid'],
'icon_title' => $appName[strtolower(APP_DIR)],
'dataId' => $article['article_id'],
'title' => $article['title'],
'cover_type' => $coverType,
'cover_id' => $article['cover_id'],
'cover_url' => $article['cover_url'],
'url' => APP_DIR . '/Frontend/Index/Detail?article_id=' . $article['article_id'],
];
$res = $rpcFavorite->addFavorite($data);
// 触发积分、学分埋点
$integrals = $this->_triggerIntegral($article);
break;
// 已收藏,执行取消收藏动作
case $rpcFavorite::COLLECTION_YES:
$data = [
'uid' => $user['memUid'],
'dataId' => $article['article_id'],
];
$res = $rpcFavorite->cancelFavorite($data);
break;
default:
$res = false;
break;
}
if (!$res) {
E('_ERR_FAVORITE_OPERATE_FAIL');
}
$this->_result = [
'integrals' => isset($integrals) ? $integrals : [],
];
}
/**
* 触发积分、学分策略
* @param array $article 新闻详情
* @return array
* array(
* 'integrals' => 1, // 获得的积分数
* )
*/
private function _triggerIntegral($article)
{
$actionKey = Constant::INT_ACT_COLLECT;
// 记录用户收藏记录(仅记录首次收藏)
$actionServ = new UserActionService();
$saveRes = $actionServ->save($this->uid, $article['article_id'], $actionKey);
if ($saveRes) {
// 触发积分策略:收藏新闻
$favTotal = $actionServ->count_by_conds([
'uid' => $this->uid,
'action_key' => $actionKey,
]);
$triggersType = [
[
'triggerKey' => Constant::INT_TRIGGER_COLLECT,
'value' => $favTotal,
]
];
// 构造策略触发参数
$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 : [];
}
}