| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace App\Module\ThirdParty\Commands;
- use Illuminate\Console\Command;
- use ThirdParty\Urs\Services\UrsService;
- use ThirdParty\Urs\Request\UrsLoginRequest;
- use Illuminate\Support\Facades\Log;
- /**
- * URS登录功能测试命令
- *
- * 用于测试URS登录请求的功能是否正常
- */
- class TestUrsLoginCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'thirdparty:test-urs-login
- {mobile? : 手机号码,默认使用测试账号}
- {password? : 登录密码,默认使用测试密码}
- {--direct : 直接使用Request类而不是Service类}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试URS登录功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $this->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));
- }
- }
- }
|