getType(); $mobile = $data->getMobile(); // 参数验证 if (empty($mobile)) { throw new LogicException("手机号不能为空"); } if (empty($type)) { throw new LogicException("验证码类型不能为空"); } // 验证手机号格式 if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) { throw new LogicException("手机号格式不正确"); } // 将type转换为CODE_TYPE枚举 $codeType = match ($type) { 1 => CODE_TYPE::REGISTER, 2 => CODE_TYPE::LOGIN, 3 => CODE_TYPE::RESET_PASSWORD, default => throw new LogicException("无效的验证码类型") }; // 生成临时令牌 $token = Str::random(32); // 发送验证码 $smsService = app(SmsService::class); $result = $smsService->sendCode($codeType, $mobile, $token); if (!$result) { throw new LogicException("验证码发送失败,请稍后再试"); } // 将手机号和验证码类型存储在会话中,供验证时使用 session(['verify_phone' => $mobile]); session(['verify_type' => $type]); // 设置响应 $this->response->setCode(RESPONSE_CODE::OK); $this->response->setMsg('验证码发送成功'); // 记录日志 Log::info('验证码发送成功', [ 'mobile' => $mobile, 'type' => $type, 'token' => $token ]); } catch (\Exception $e) { Log::error('验证码发送异常', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); // 重新抛出异常,交由框架处理 throw $e; } return $response; } }