argument('app_id'); // 验证应用是否存在 $app = OpenApiApp::where('app_id', $appId)->first(); if (!$app) { $this->error("应用 {$appId} 不存在"); return 1; } // 检查应用状态 if (!in_array($app->status, ['ACTIVE', 'APPROVED'])) { $this->error("应用 {$appId} 状态不允许生成密钥,当前状态: {$app->status}"); return 1; } // 获取参数 $name = $this->option('name') ?: 'Generated Key'; $description = $this->option('description') ?: ''; $scopes = $this->option('scopes') ?: $app->scopes; $expiresDays = $this->option('expires'); // 计算过期时间 $expiresAt = null; if ($expiresDays) { $expiresAt = now()->addDays((int)$expiresDays); } // 生成密钥 $keyId = 'ak_' . Str::random(32); $keySecret = 'sk_' . Str::random(64); $apiKey = OpenApiKey::create([ 'app_id' => $appId, 'key_id' => $keyId, 'key_secret' => hash('sha256', $keySecret), // 加密存储 'name' => $name, 'description' => $description, 'scopes' => $scopes, 'status' => 'ACTIVE', 'expires_at' => $expiresAt, ]); // 显示结果 $this->info('API密钥生成成功!'); $this->line(''); $this->line("应用ID: {$appId}"); $this->line("应用名称: {$app->name}"); $this->line("密钥ID: {$keyId}"); $this->line("密钥Secret: {$keySecret}"); $this->line("密钥名称: {$name}"); $this->line("权限范围: " . implode(', ', $scopes)); if ($expiresAt) { $this->line("过期时间: {$expiresAt->format('Y-m-d H:i:s')}"); } else { $this->line("过期时间: 永不过期"); } $this->line(''); $this->warn('请妥善保存密钥信息,系统不会再次显示完整的密钥Secret!'); return 0; } }