TestUrsLoginCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Module\ThirdParty\Commands;
  3. use Illuminate\Console\Command;
  4. use ThirdParty\Urs\Services\UrsService;
  5. use ThirdParty\Urs\Request\UrsLoginRequest;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * URS登录功能测试命令
  9. *
  10. * 用于测试URS登录请求的功能是否正常
  11. */
  12. class TestUrsLoginCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'thirdparty:test-urs-login
  20. {mobile? : 手机号码,默认使用测试账号}
  21. {password? : 登录密码,默认使用测试密码}
  22. {--direct : 直接使用Request类而不是Service类}';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = '测试URS登录功能';
  29. /**
  30. * 执行命令
  31. *
  32. * @return int
  33. */
  34. public function handle(): int
  35. {
  36. $this->info('=== URS登录功能测试 ===');
  37. $this->info('测试URS登录请求功能');
  38. $this->newLine();
  39. try {
  40. // 获取测试参数
  41. $mobile = $this->argument('mobile') ?? '18600648353';
  42. $password = $this->argument('password') ?? 'a123123';
  43. $useDirect = $this->option('direct');
  44. $this->info("📱 测试手机号: {$mobile}");
  45. $this->info("🔑 测试密码: " . str_repeat('*', strlen($password)));
  46. $this->info("🔧 测试方式: " . ($useDirect ? '直接使用Request类' : '使用Service类'));
  47. $this->newLine();
  48. // 检查类是否存在
  49. $this->checkClassExists();
  50. // 执行登录测试
  51. if ($useDirect) {
  52. $result = $this->testDirectRequest($mobile, $password);
  53. } else {
  54. $result = $this->testServiceRequest($mobile, $password);
  55. }
  56. // 显示测试结果
  57. $this->displayResult($result);
  58. $this->newLine();
  59. $this->info('✅ URS登录功能测试完成');
  60. return 0;
  61. } catch (\Exception $e) {
  62. $this->error('❌ 测试失败: ' . $e->getMessage());
  63. $this->error('错误详情: ' . $e->getTraceAsString());
  64. return 1;
  65. }
  66. }
  67. /**
  68. * 检查相关类是否存在
  69. */
  70. protected function checkClassExists(): void
  71. {
  72. $this->info('🔍 检查相关类是否存在...');
  73. $classes = [
  74. 'UrsLoginRequest' => UrsLoginRequest::class,
  75. 'UrsService' => UrsService::class,
  76. ];
  77. foreach ($classes as $name => $class) {
  78. if (class_exists($class)) {
  79. $this->info(" ✅ {$name} 类存在");
  80. } else {
  81. $this->error(" ❌ {$name} 类不存在");
  82. throw new \Exception("{$name} 类不存在");
  83. }
  84. }
  85. $this->newLine();
  86. }
  87. /**
  88. * 测试直接使用Request类
  89. *
  90. * @param string $mobile
  91. * @param string $password
  92. * @return array
  93. */
  94. protected function testDirectRequest(string $mobile, string $password): array
  95. {
  96. $this->info('🚀 使用Request类直接测试...');
  97. $request = new UrsLoginRequest();
  98. return $request->request([
  99. 'mobile' => $mobile,
  100. 'password' => $password
  101. ]);
  102. }
  103. /**
  104. * 测试使用Service类
  105. *
  106. * @param string $mobile
  107. * @param string $password
  108. * @return array
  109. */
  110. protected function testServiceRequest(string $mobile, string $password): array
  111. {
  112. $this->info('🚀 使用Service类测试...');
  113. return UrsService::login($mobile, $password);
  114. }
  115. /**
  116. * 显示测试结果
  117. *
  118. * @param array $result
  119. */
  120. protected function displayResult(array $result): void
  121. {
  122. $this->newLine();
  123. $this->info('📊 登录测试结果:');
  124. if (isset($result['userId'])) {
  125. $this->info(" 👤 用户ID: {$result['userId']}");
  126. } else {
  127. $this->warn(" ⚠️ 用户ID: 未返回");
  128. }
  129. if (isset($result['userKey'])) {
  130. $userKeyLength = strlen($result['userKey']);
  131. $maskedUserKey = substr($result['userKey'], 0, 10) . '...' . substr($result['userKey'], -10);
  132. $this->info(" 🔑 用户密钥: {$maskedUserKey} (长度: {$userKeyLength})");
  133. } else {
  134. $this->warn(" ⚠️ 用户密钥: 未返回");
  135. }
  136. // 显示完整结果(调试用)
  137. if ($this->getOutput()->isVerbose()) {
  138. $this->newLine();
  139. $this->info('🔍 完整响应数据:');
  140. $this->line(json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  141. }
  142. // 验证必需字段
  143. $requiredFields = ['userId', 'userKey'];
  144. $missingFields = [];
  145. foreach ($requiredFields as $field) {
  146. if (!isset($result[$field]) || empty($result[$field])) {
  147. $missingFields[] = $field;
  148. }
  149. }
  150. if (empty($missingFields)) {
  151. $this->info('✅ 所有必需字段都已返回');
  152. } else {
  153. $this->warn('⚠️ 缺少必需字段: ' . implode(', ', $missingFields));
  154. }
  155. }
  156. }