EnterController.class.php
8.18 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* 【手机端】10-报名接口
* Created by PhpStorm.
* @author:wanghuan
* @date:2017-09-01
*/
namespace Api\Controller\Education;
use Common\Common\Constant;
use Common\Common\Integral;
use Common\Service\RightUsersService;
use Common\Service\EducationService;
class EnterController extends \Api\Controller\AbstractController
{
// 兑换消耗
const INTEGRAL_EXCHANGE_CONSUMPTION = 6;
/** @var RightUsersService */
protected $right_users_s;
/** @var EducationService */
protected $edu_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化员工名单service
$this->right_users_s = new RightUsersService();
// 实例化培训service
$this->edu_s = new EducationService();
return true;
}
/**
* 报名
*/
public function Index_post()
{
// 培训id
$ed_id = I('post.ed_id', 0, 'intval');
// 培训id为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 培训详情
$education = $this->get_education($ed_id);
// 是否收费
$ed_is_charge = $education['ed_is_charge'];
// 是否审核
$ed_is_check = $education['ed_is_check'];
$result = 0;
// 消息提醒参数
$msg_params = [];
// 验证数据,验证通过返回当前用户报名数据查询条件
$users_cond = $this->validate_data($education);
try {
// 开始事务
$this->right_users_s->start_trans();
// 不收费、不审核或者审核(审核、收费只能二选一)
if ((EducationService::EDUCATION_UN_CHARGE == $ed_is_charge
&& EducationService::EDUCATION_UN_CHECK == $ed_is_check)
|| EducationService::EDUCATION_CHECK == $ed_is_check) {
// 报名状态更新数据
$users_update_data = [
'ru_sign_up_time' => MILLI_TIME,
'ru_sign_status' => RightUsersService::SIGN_STATUS_SIGNED
];
// 更新报名状态为已报名
$result = $this->right_users_s->update_by_conds($users_cond, $users_update_data);
}
// 收费
if (EducationService::EDUCATION_CHARGE == $ed_is_charge && $education['ed_charge_score'] > 0) {
$app_name = cfg('APPLICATION_NAME', null, '');
// 实例化积分勋章
$integralUtil = &Integral::instance();
// 积分参数
$integral_params = [
// 用户id
'memUid' => $this->uid,
// 变更的积分值
'integral' => $education['ed_charge_score'],
// 积分获得类型 (6:兑换消耗)
'milOptType' => self::INTEGRAL_EXCHANGE_CONSUMPTION,
// 奖品名称
'prizeName' => "{$app_name}-手机端培训报名",
// 备注
'remark' => '【培训报名】' . $education['ed_name'],
// 操作人id
'milCreateMemUid' => $this->_login->user['memUid'],
// 操作人名称
'milCreateMemUsername' => $this->_login->user['memUsername'],
// 业务ID (由业务平台生成唯一ID, 用于和积分流水做绑定)
'businessId' => $ed_id,
// 业务应用
'businessKey' => 'offline_train',
// 触发行为
'businessAct' => 'train_sign_up',
// 应用标识(消息推送必要)
'msgIdentifier' => APP_IDENTIFIER,
// 积分类型
'miType' => $education['ed_charge_type'] == Constant::ED_CHARGE_TYPE_IS_INTEGRAL ? Integral::MI_TYPE_INTEGRAL : Integral::MI_TYPE_CREDIT,
];
// 积分操作
$res = $integralUtil->integralExchange($integral_params);
// 初始化支付状态
$pay_status = '支付失败';
if ($res['milId']) {
// 更新报名状态为已报名,支付状态为已支付,更新积分兑换id
$users_update_data = [
'ru_sign_up_time' => MILLI_TIME,
'ru_sign_status' => RightUsersService::SIGN_STATUS_SIGNED,
'ru_pay_status' => RightUsersService::SIGN_CHARGE_PAY,
'milId' => $res['milId']
];
$result = $this->right_users_s->update_by_conds($users_cond, $users_update_data);
$pay_status = '支付成功';
}
// 组装消息提醒参数
$msg_params = [
'ed_id' => $ed_id,
'name' => $education['ed_name'],
'pay_status' => $pay_status,
'pay_time' => MILLI_TIME,
'uids' => [
'memID' => $this->uid
]
];
}
// 报名成功
if ($result) {
// 不是审核报名
if (EducationService::EDUCATION_CHECK != $ed_is_check) {
// 更新已参加人数
$this->edu_s->update_joined($ed_id, self::JOINED_INC);
}
} else { // 报名失败
$this->right_users_s->rollback();
}
$this->right_users_s->commit();
} catch (\Think\Exception $e) {
$this->right_users_s->rollback();
E('_ERR_SIGN_UP_FAIL');
return false;
}
// 收费并且消息提醒参数不为空:发消息
if (EducationService::EDUCATION_CHARGE == $ed_is_charge && !empty($msg_params)) {
// 发送消息提醒
$this->right_users_s->send_msg($msg_params, RightUsersService::MSG_EDUCATION_SIGN);
}
// 报名失败
if (!$result) {
E('_ERR_SIGN_UP_FAIL');
}
return true;
}
/**
* 培训数据验证
* @param array $education 培训详情数据
*
* @return array 当前用户报名数据查询条件
*/
protected function validate_data($education = [])
{
// 报名已截止
if ($education['ed_sign_deadline'] < MILLI_TIME) {
E('_ERR_ED_SIGN_END');
}
// 报名限制人数已满
if ($education['ed_sign_up_number'] && $education['ed_joined_count'] == $education['ed_sign_up_number']) {
E('_ERR_ED_SIGN_NUM_FULL');
}
// 当前用户报名数据查询条件
$users_cond = [
'ru_uid' => $this->uid,
'ed_id' => $education['ed_id']
];
// 当前用户报名状态
$sign_info = $this->right_users_s->get_by_conds($users_cond);
// 用户不在适用范围
if (empty($sign_info)) {
// 没有报名权限
E('_ERR_ED_NO_RIGHT');
}
// 用户报名状态
$sign_up_status = $this->right_users_s->get_sign_up_status($education, $sign_info);
// 报名成功或报名待审核:已报名
if (RightUsersService::SIGN_SUCCESS == $sign_up_status
|| RightUsersService::SIGN_WAIT_CHECK == $sign_up_status) {
E('_ERR_ED_SIGNED');
}
// 收费
if ($education['ed_is_charge'] == EducationService::EDUCATION_CHARGE) {
$user = $this->_login->user;
$integralServ = &Integral::instance();
$user_data = $integralServ->detail([
'memUid' => $user['memUid'],
'miType' => $education['ed_charge_type'] == Constant::ED_CHARGE_TYPE_IS_INTEGRAL ? Integral::MI_TYPE_INTEGRAL : Integral::MI_TYPE_CREDIT,
]);
// 余额不足
if ($user_data['total'] < $education['ed_charge_score']) {
E('_ERR_USER_BALANCE_NOT_ENOUGH');
}
}
return $users_cond;
}
}