-
Notifications
You must be signed in to change notification settings - Fork 4
/
MeritBehavior.php
159 lines (146 loc) · 5.49 KB
/
MeritBehavior.php
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
<?php
/**
* author : forecho <[email protected]>
* createTime : 16/3/20 下午5:42
* description:
*/
namespace yiier\merit;
use yii\base\Behavior;
use yii\db\Exception;
use yii\web\Controller;
use yiier\merit\models\Merit;
use yiier\merit\models\MeritLog;
use yiier\merit\models\MeritTemplate;
use Yii;
class MeritBehavior extends Behavior
{
public function events()
{
return [
Controller::EVENT_AFTER_ACTION => 'afterAction',
];
}
public function afterAction()
{
if (!Yii::$app->user->isGuest) {
if ($meritTemplates = $this->hasMeritTemplate()) {
/** @var MeritTemplate $meritTemplate */
foreach ($meritTemplates as $meritTemplate) {
switch ($meritTemplate->rule_key) {
case 2:
if ($meritTemplate->rule_value <= $this->getMeritLogTimes($meritTemplate, Yii::$app->user->id)) {
return false;
}
break;
case 1:
if ($meritTemplate->rule_value <= $this->getMeritLogDay($meritTemplate, Yii::$app->user->id)) {
return false;
}
break;
default:
# code...
break;
}
$this->update($meritTemplate);
}
}
}
return false;
}
public function hasMeritTemplate()
{
$uniqueId = Yii::$app->controller->action->uniqueId;
// 必须是 GET 或者 POST 请求
if (!in_array(Yii::$app->request->method, MeritTemplate::getMethods())) {
return false;
}
$method = array_flip(MeritTemplate::getMethods())[Yii::$app->request->method];
// 支持同一个 $uniqueId 不同 Type
$meritTemplates = MeritTemplate::find()
->where(['unique_id' => $uniqueId, 'status' => MeritTemplate::STATUS_ACTIVE, 'method' => $method])
->all();
if (!$meritTemplates) {
return false;
}
return $meritTemplates;
}
/**
* 查看次数
* @param MeritTemplate $meritTemplate
* @param $userId
* @return int|string
*/
public function getMeritLogTimes(MeritTemplate $meritTemplate, $userId)
{
return MeritLog::find()
->where(['merit_template_id' => $meritTemplate->id, 'type' => $meritTemplate->type, 'user_id' => $userId])
->count();
}
/**
* 按次查看
* @param MeritTemplate $meritTemplate
* @param $userId
* @return int|string
*/
public function getMeritLogDay(MeritTemplate $meritTemplate, $userId)
{
return MeritLog::find()
->where(['merit_template_id' => $meritTemplate->id, 'type' => $meritTemplate->type, 'user_id' => $userId])
->andWhere(['between', 'created_at', strtotime(date('Ymd') . '000000'), strtotime(date('Ymd') . '235959')])
->count();
}
/**
* @param MeritTemplate $meritTemplate
* @throws Exception
*/
public function update(MeritTemplate $meritTemplate)
{
$meritLog = new MeritLog();
$user = \Yii::$app->user->identity;
$transaction = \Yii::$app->db->beginTransaction();
try {
/** @var Merit $userMerit */
$userMerit = Merit::findOne(['user_id' => $user->getId(), 'type' => $meritTemplate->type]);
// is sub 判断是否是减法
$actionSub = ($meritTemplate->action_type == MeritTemplate::ACTIVE_TYPE_SUB);
if ($userMerit) {
$merit = call_user_func($actionSub ? 'bcsub' : 'bcadd', $userMerit->merit, $meritTemplate->increment);
$userMerit->setAttributes([
'merit' => (integer)$merit
]);
} else {
$userMerit = new Merit();
$userMerit->setAttributes([
'merit' => ($actionSub ? '-' : '') . $meritTemplate->increment,
'user_id' => $user->getId(),
'username' => $user->username,
'type' => $meritTemplate->type,
]);
}
if (!$userMerit->save()) {
Yii::error('Merit 操作失败' . json_encode(array_values($userMerit->getFirstErrors())), 'error');
throw new Exception(array_values($userMerit->getFirstErrors())[0]);
}
$description = $meritTemplate->title . ': '
. MeritTemplate::getActionTypes()[$meritTemplate->action_type]
. $meritTemplate->increment
. MeritTemplate::getTypes()[$meritTemplate->type];
$meritLog->setAttributes([
'user_id' => $user->getId(),
'username' => $user->username,
'merit_template_id' => $meritTemplate->id,
'type' => $meritTemplate->type,
'description' => $description,
'action_type' => $meritTemplate->action_type,
'increment' => $meritTemplate->increment,
'created_at' => time(),
]);
if (!$meritLog->save()) {
throw new Exception(array_values($meritLog->getFirstErrors())[0]);
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
}
}
}