getCode(); // 参数验证 if (empty($code)) { throw new LogicException("验证码不能为空"); } // 从会话中获取手机号和验证码类型 $phone = session('verify_phone'); $type = session('verify_type'); if (empty($phone)) { throw new LogicException("请先发送验证码"); } if (empty($type)) { throw new LogicException("验证码类型无效"); } // 将type转换为CODE_TYPE枚举 $codeType = match ((int)$type) { 1 => CODE_TYPE::REGISTER, 2 => CODE_TYPE::LOGIN, 3 => CODE_TYPE::RESET_PASSWORD, default => throw new LogicException("无效的验证码类型") }; // 验证验证码 $smsService = app(SmsService::class); $isValid = $smsService->verifyCode($codeType, $phone, $code); if (!$isValid) { throw new LogicException("验证码无效或已过期"); } // 生成临时令牌 $token = Str::random(32); // 将令牌与手机号关联并存储在会话中 session(['verify_token' => $token]); session(['verified_phone' => $phone]); // 设置响应 $response->setToken($token); $this->response->setCode(RESPONSE_CODE::OK); $this->response->setMsg('验证码验证成功'); // 记录日志 Log::info('验证码验证成功', [ 'phone' => $phone, 'type' => $type, 'token' => $token ]); } catch (\Exception $e) { Log::error('验证码验证异常', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); // 重新抛出异常,交由框架处理 throw $e; } return $response; } }