info('=== URS登录功能测试 ==='); $this->info('测试URS登录请求功能'); $this->newLine(); try { // 获取测试参数 $mobile = $this->argument('mobile') ?? '18600648353'; $password = $this->argument('password') ?? 'a123123'; $useDirect = $this->option('direct'); $this->info("📱 测试手机号: {$mobile}"); $this->info("🔑 测试密码: " . str_repeat('*', strlen($password))); $this->info("🔧 测试方式: " . ($useDirect ? '直接使用Request类' : '使用Service类')); $this->newLine(); // 检查类是否存在 $this->checkClassExists(); // 执行登录测试 if ($useDirect) { $result = $this->testDirectRequest($mobile, $password); } else { $result = $this->testServiceRequest($mobile, $password); } // 显示测试结果 $this->displayResult($result); $this->newLine(); $this->info('✅ URS登录功能测试完成'); return 0; } catch (\Exception $e) { $this->error('❌ 测试失败: ' . $e->getMessage()); $this->error('错误详情: ' . $e->getTraceAsString()); return 1; } } /** * 检查相关类是否存在 */ protected function checkClassExists(): void { $this->info('🔍 检查相关类是否存在...'); $classes = [ 'UrsLoginRequest' => UrsLoginRequest::class, 'UrsService' => UrsService::class, ]; foreach ($classes as $name => $class) { if (class_exists($class)) { $this->info(" ✅ {$name} 类存在"); } else { $this->error(" ❌ {$name} 类不存在"); throw new \Exception("{$name} 类不存在"); } } $this->newLine(); } /** * 测试直接使用Request类 * * @param string $mobile * @param string $password * @return array */ protected function testDirectRequest(string $mobile, string $password): array { $this->info('🚀 使用Request类直接测试...'); $request = new UrsLoginRequest(); return $request->request([ 'mobile' => $mobile, 'password' => $password ]); } /** * 测试使用Service类 * * @param string $mobile * @param string $password * @return array */ protected function testServiceRequest(string $mobile, string $password): array { $this->info('🚀 使用Service类测试...'); return UrsService::login($mobile, $password); } /** * 显示测试结果 * * @param array $result */ protected function displayResult(array $result): void { $this->newLine(); $this->info('📊 登录测试结果:'); if (isset($result['userId'])) { $this->info(" 👤 用户ID: {$result['userId']}"); } else { $this->warn(" ⚠️ 用户ID: 未返回"); } if (isset($result['userKey'])) { $userKeyLength = strlen($result['userKey']); $maskedUserKey = substr($result['userKey'], 0, 10) . '...' . substr($result['userKey'], -10); $this->info(" 🔑 用户密钥: {$maskedUserKey} (长度: {$userKeyLength})"); } else { $this->warn(" ⚠️ 用户密钥: 未返回"); } // 显示完整结果(调试用) if ($this->getOutput()->isVerbose()) { $this->newLine(); $this->info('🔍 完整响应数据:'); $this->line(json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); } // 验证必需字段 $requiredFields = ['userId', 'userKey']; $missingFields = []; foreach ($requiredFields as $field) { if (!isset($result[$field]) || empty($result[$field])) { $missingFields[] = $field; } } if (empty($missingFields)) { $this->info('✅ 所有必需字段都已返回'); } else { $this->warn('⚠️ 缺少必需字段: ' . implode(', ', $missingFields)); } } }