<?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; } }