ReadController.class.php
3.2 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/8/1
* Time: 17:47
*/
namespace Api\Controller\News;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\IntegralStrategy;
use Common\Common\NewsHelper;
use Common\Service\ArticleService;
use Common\Service\UserActionService;
class ReadController extends \Api\Controller\AbstractController
{
/**
* Read
* @author liyifei
* @desc 已读新闻接口
* @param int article_id:true 新闻公告ID
* @return array 获得积分列表
array(
'integrals' => array( // 每次获得的积分数
array(
'content' => '阅读新闻获得积分',
'number' => 1,
),
),
)
*/
public function Index_post()
{
// 验证规则
$rules = [
'article_id' => 'require|integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$article_id = $validate->postData['article_id'];
// 新闻检查
$articleServ = new ArticleService();
$article = $articleServ->get($article_id);
if (empty($article)) {
E('_ERR_ARTICLE_NOT_FOUND');
}
// 积分、学分埋点
$integrals = $this->_triggerIntegral($article);
$this->_result = [
'integrals' => isset($integrals) ? $integrals : [],
];
}
/**
* 触发积分、学分策略
* @param array $article 新闻详情
* @return array
* array(
* 'integrals' => 1, // 获得的积分数
* )
*/
private function _triggerIntegral($article)
{
$actionKey = Constant::INT_ACT_READ;
// 记录阅读新闻
$actionServ = new UserActionService();
$saveRes = $actionServ->save($this->uid, $article['article_id'], $actionKey);
// 首次阅读 + 后台设置开启积分策略时,可进行积分埋点
if ($saveRes) {
// 触发积分策略:完成阅读
$triggersType = [
[
'triggerKey' => Constant::INT_TRIGGER_COMPLETE,
'value' => 1,
]
];
// 触发积分策略:完成阅读新闻篇数
$readTotal = $actionServ->count_by_conds([
'uid' => $this->uid,
'action_key' => $actionKey,
]);
$triggersType[] = [
'triggerKey' => Constant::INT_TRIGGER_COMPLETE_NUM,
'value' => $readTotal,
];
// 构造策略触发参数
$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 : [];
}
}