Ejemplo n.º 1
0
 public function actionCheckSendCaptcha()
 {
     // $this->layout = 'modify_common';
     $request = Yii::$app->getRequest();
     $util = Yii::$app->util;
     $session = Yii::$app->session;
     if (!$request->getIsAjax()) {
         $util->formatResData(1301, 'illegal_request', ['msg' => '非法请求']);
     }
     // 业务不同要求是否具有登录状也不同
     /*
     if (!$this->isLogin) {
         $util->formatResData(1301, 'illegal_request', ['msg' => '非法请求']);
     }
     */
     $params = $request->post();
     $params = array_map('htmlEntityString', $params);
     $code = $params['captcha'];
     $route = trim($util->routeDecrypt($params['ca']));
     $session->open();
     $user = UserBaseInfo::getLoginUser();
     $curStep = StepFlow::getCurStep($route);
     $curStepConfig = StepFlow::getStepConfig($curStep, $route);
     if (!$curStepConfig) {
         $util->formatResData(1501, 'error_config', ['msg' => '非法请求']);
     }
     if (isset($curStepConfig['use_mobile_param']) && $curStepConfig['use_mobile_param'] && isset($params['tool'])) {
         $session[$route . '_tool'] = $params['tool'];
     }
     $supportRoutes = ['user/modify-mobile', 'user/bind-email', 'user/modify-email', 'user/bind-new-user'];
     if (isset($params['tool']) && in_array($route, $supportRoutes)) {
         if (!$params['tool']) {
             if (!isset($curStepConfig['support_type'])) {
                 $promptMsg = '手机号码或邮箱不能为空';
             } elseif ($curStepConfig['support_type'] == 'mobile') {
                 $promptMsg = '手机号码不能为空';
             } elseif ($curStepConfig['support_type'] == 'email') {
                 $promptMsg = '邮箱不能为空';
             } else {
                 $promptMsg = '手机号或邮箱不能为空';
             }
             $util->formatResData(3001, 'missing_tool_param', ['msg' => $promptMsg]);
         }
         $tool = $params['tool'];
         $type = $this->getBusiDataType($tool);
         $session[$route . '_tool'] = $tool;
         if (!$type) {
             if (!isset($curStepConfig['support_type'])) {
                 $promptMsg = '手机号码或邮箱格式不正确';
             } elseif ($curStepConfig['support_type'] == 'mobile') {
                 $promptMsg = '手机号码格式不正确';
             } elseif ($curStepConfig['support_type'] == 'email') {
                 $promptMsg = '邮箱格式不正确';
             } else {
                 $promptMsg = '手机号码或邮箱格式不正确';
             }
             $util->formatResData(3001, 'missing_tool_param', ['msg' => $promptMsg]);
         } else {
             if (isset($curStepConfig['support_type']) && $curStepConfig['support_type'] != 'mixed' && $type != $curStepConfig['support_type']) {
                 if ($curStepConfig['support_type'] == 'mobile') {
                     $promptMsg = '手机号码格式不正确';
                 } elseif ($curStepConfig['support_type'] == 'email') {
                     $promptMsg = '邮箱格式不正确';
                 }
                 $util->formatResData(3001, 'missing_tool_param', ['msg' => $promptMsg]);
             }
             if (!isset($curStepConfig['ignore_mobile_status']) || !$curStepConfig['ignore_mobile_status']) {
                 $mUser = new UserBaseInfo();
                 $user = $mUser->isUser($tool);
                 if ($user) {
                     if ($type == 'mobile') {
                         $promptMsg = '手机号码已经注册';
                     } else {
                         $promptMsg = '邮箱已经被绑定';
                     }
                     $util->formatResData(3100, 'missing_tool_param', ['msg' => $promptMsg]);
                 }
             }
         }
     } else {
         $tool = $session[$route . '_tool'];
         $type = $this->getBusiDataType($tool);
     }
     // 由&&修改为||
     if (!isset($user['mobile']) && !$user['mobile'] && !isset($session[$route . '_tool'])) {
         $util->formatResData(1401, 'error_illegal_user', ['msg' => '非法请求']);
     }
     if (!$code) {
         $util->formatResData(1100, 'error_captcha_empty', ['msg' => '验证码不能为空']);
     }
     if (!$route) {
         $util->formatResData(1301, 'illegal_request', ['msg' => '非法请求']);
     }
     if (isset($curStepConfig['support_type']) && $tool != $curStepConfig['support_type']) {
         switch ($curStepConfig['support_type']) {
             case 'mobile':
                 $promptMsg = '手机号码格式不正确';
                 break;
             case 'email':
                 $promptMsg = '邮箱格式不正确';
                 break;
             default:
         }
     }
     $sessCodeKey = 'captcha_' . $route . '_' . $type . '_' . $curStep;
     $sessCode = $session[$sessCodeKey]['code'];
     $codeExpiredTime = $session[$sessCodeKey]['time'];
     $now = time();
     if (isset($session[$sessCodeKey]['tool']) && $session[$sessCodeKey]['tool'] != $tool) {
         if ($type == 'mobile') {
             $promptMsg = '手机号码与发送验证码的不一致(' . $util->starMobile($session[$sessCodeKey]['tool']) . ')';
         } else {
             $promptMsg = '邮箱与发送验证码的不一致(' . $session[$sessCodeKey]['tool'] . ')';
         }
         $util->formatResData(3000, 'error_diff_sender', ['msg' => $promptMsg]);
     }
     if (!$sessCode) {
         $util->formatResData(3100, 'error_sesscode_null', ['msg' => '请先获取验证码']);
     }
     if ($code != $sessCode) {
         $util->formatResData(2000, 'error_code_incorret', ['msg' => '验证码不正确']);
     }
     if ($now > $codeExpiredTime) {
         $util->formatResData(1901, 'error_code_expired', ['msg' => '您的验证码已经过期,请重新获取']);
     }
     // 针对第三方绑定单独进行处理
     if ($route == 'user/bind-new-user') {
         $mUser = new UserBaseInfo();
         $user = $mUser->getByMobileOrEmail($tool);
         $nextBranchKey = 'default';
         if ($user) {
             $nextBranchKey = 'exists_user';
             $session['tpb_user'] = $user;
         }
         $curStepConfig = StepFlow::getStepConfig($curStep, $route, false, $nextBranchKey);
     }
     if (isset($curStepConfig['next_step_tpl']) && $curStepConfig['next_step_tpl']) {
         try {
             $content = $this->renderAjax($curStepConfig['next_step_tpl'], ['config' => $curStepConfig, 'tool' => $tool, 'route' => $route, 'action' => StepFlow::getStepExtraConfig($route, 'action')]);
         } catch (\Exception $e) {
             // TODO: 对存在模板情况进行处理
         }
     }
     if (isset($curStepConfig['handle'])) {
         $methodName = $curStepConfig['handle'];
         if ($methodName) {
             $handleService = new HandleService();
             if (method_exists($handleService, $methodName)) {
                 $user = UserBaseInfo::getLoginUser();
                 $uid = $user ? $user['id'] : false;
                 $handleService->{$methodName}(['mobile' => $tool, 'id' => $uid]);
             }
         }
     }
     // TODO: 临时设置
     $curStep = StepFlow::setCurStep($curStepConfig['next_step']);
     $session[$route] = $curStep;
     if (isset($session[$sessCodeKey])) {
         unset($session[$sessCodeKey]);
     }
     $util->formatResData(0, 'success', ['content' => $content]);
 }