request->post("mobile"); $event = $this->request->post("event"); $event = $event ? $event : 'register'; if (!$mobile || !preg_match("/^1[3-9]\d{9}$/", $mobile)) { $this->error(__('手机号格式错误')); } if (!preg_match("/^[a-z0-9_\-]{3,30}$/i", $event)) { $this->error(__('事件名称错误')); } //发送前验证码 $captcha = $this->request->post('captcha'); if (config('site.sms_captcha')) { if (!preg_match("/^[a-z0-9]{4,6}$/i", $captcha)) { $this->error(__('图形验证码格式错误')); } if (!\think\Validate::is($captcha, 'captcha')) { $this->error("验证码不正确"); } } try { if ($event) { $userinfo = UserService::getOne(['mobile' => $mobile]); if ($event == 'register' && $userinfo) { //已被注册 $this->error(__('已被注册')); } elseif (in_array($event, ['changemobile']) && $userinfo) { //被占用 $this->error(__('已被占用')); } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) { //未注册 $this->error(__('未注册')); } } $result = \app\common\library\Sms::send($mobile, null, $event); if ($result) { $this->success(__('发送成功')); } else { $this->error(__('发送失败')); } } catch (\Exception $e) { if ($e instanceof HttpResponseException) { throw $e; } $this->error($e->getMessage()); } } /** * 检测验证码 * * @ApiMethod (POST) * @ApiParams (name="mobile", type="string", required=true, description="手机号") * @ApiParams (name="event", type="string", required=true, description="事件名称") * @ApiParams (name="captcha", type="string", required=true, description="验证码") */ public function check(SmsService $SmsService) { $mobile = $this->request->post("mobile"); $event = $this->request->post("event"); $event = $event ? $event : 'register'; $captcha = $this->request->post("code"); if (!$mobile || !preg_match("/^1[3-9]\d{9}$/", $mobile)) { $this->error(__('手机号格式错误')); } if (!preg_match("/^[a-z0-9_\-]{3,30}$/i", $event)) { $this->error(__('事件名称错误')); } if (!preg_match("/^[0-9]{4}$/", $captcha)) { $this->error(__('验证码格式错误')); } try { $ret = $SmsService->check($mobile, $captcha); if ($ret) { $this->success(__('成功')); } else { $this->error(__('验证码不正确')); } } catch (\Exception $e) { if ($e instanceof HttpResponseException) { throw $e; } $this->error($e->getMessage()); } } }