notfff 7 mēneši atpakaļ
vecāks
revīzija
2e4797cebe

+ 0 - 347
app/Module/ThirdParty/Commands/RestructureExternalManagementMenu.php

@@ -1,347 +0,0 @@
-<?php
-
-namespace App\Module\ThirdParty\Commands;
-
-use Illuminate\Console\Command;
-use Illuminate\Support\Facades\DB;
-
-/**
- * 重构外接管理菜单结构
- * 
- * 将外接管理菜单重新组织为两个二级模块:OpenAPI模块和ThirdParty模块
- * 
- * 使用方法: php artisan thirdparty:restructure-external-menu
- */
-class RestructureExternalManagementMenu extends Command
-{
-    /**
-     * 命令签名
-     *
-     * @var string
-     */
-    protected $signature = 'thirdparty:restructure-external-menu {--force : 强制重新创建菜单结构}';
-
-    /**
-     * 命令描述
-     *
-     * @var string
-     */
-    protected $description = '重构外接管理菜单结构,组织为OpenAPI模块和ThirdParty模块';
-
-    /**
-     * 外接管理父菜单ID
-     *
-     * @var int
-     */
-    protected int $parentMenuId = 533;
-
-    /**
-     * 新的菜单结构配置
-     *
-     * @var array
-     */
-    protected array $menuStructure = [
-        'openapi' => [
-            'title' => 'OpenAPI模块',
-            'icon' => 'fa-plug',
-            'order' => 10,
-            'children' => [
-                [
-                    'title' => 'OpenAPI应用管理',
-                    'uri' => 'openapi-apps',
-                    'icon' => 'fa-key',
-                    'order' => 10,
-                ],
-                [
-                    'title' => 'API密钥管理',
-                    'uri' => 'openapi-keys',
-                    'icon' => 'fa-key',
-                    'order' => 20,
-                ],
-                [
-                    'title' => 'API调用日志',
-                    'uri' => 'openapi-logs',
-                    'icon' => 'fa-list-alt',
-                    'order' => 30,
-                ],
-            ],
-        ],
-        'thirdparty' => [
-            'title' => 'ThirdParty模块',
-            'icon' => 'fa-server',
-            'order' => 20,
-            'children' => [
-                [
-                    'title' => '第三方服务管理',
-                    'uri' => 'thirdparty/services',
-                    'icon' => 'fa-server',
-                    'order' => 10,
-                ],
-                [
-                    'title' => '认证凭证管理',
-                    'uri' => 'thirdparty/credentials',
-                    'icon' => 'fa-shield-alt',
-                    'order' => 20,
-                ],
-                [
-                    'title' => '调用日志管理',
-                    'uri' => 'thirdparty/logs',
-                    'icon' => 'fa-file-text',
-                    'order' => 30,
-                ],
-                [
-                    'title' => '配额管理',
-                    'uri' => 'thirdparty/quotas',
-                    'icon' => 'fa-tachometer-alt',
-                    'order' => 40,
-                ],
-                [
-                    'title' => '监控记录',
-                    'uri' => 'thirdparty/monitors',
-                    'icon' => 'fa-heartbeat',
-                    'order' => 50,
-                ],
-                [
-                    'title' => '统计报告',
-                    'uri' => 'thirdparty/reports/overview',
-                    'icon' => 'fa-chart-bar',
-                    'order' => 60,
-                ],
-            ],
-        ],
-    ];
-
-    /**
-     * 执行命令
-     *
-     * @return int
-     */
-    public function handle(): int
-    {
-        try {
-            // 检查父菜单是否存在
-            if (!$this->checkParentMenu()) {
-                $this->error("父菜单 '外接管理' (ID: {$this->parentMenuId}) 不存在");
-                return 1;
-            }
-
-            $this->info("开始重构外接管理菜单结构...");
-
-            // 显示当前菜单结构
-            $this->displayCurrentStructure();
-
-            // 确认操作
-            if (!$this->option('force') && !$this->confirm('确定要重构菜单结构吗?这将删除现有的子菜单并重新创建。')) {
-                $this->info('操作已取消');
-                return 0;
-            }
-
-            // 备份现有菜单
-            $this->backupCurrentMenus();
-
-            // 删除现有子菜单
-            $this->deleteExistingSubMenus();
-
-            // 创建新的菜单结构
-            $this->createNewMenuStructure();
-
-            // 显示新的菜单结构
-            $this->displayNewStructure();
-
-            $this->info("✅ 菜单结构重构完成!");
-
-            return 0;
-
-        } catch (\Exception $e) {
-            $this->error("重构菜单失败: " . $e->getMessage());
-            return 1;
-        }
-    }
-
-    /**
-     * 检查父菜单是否存在
-     *
-     * @return bool
-     */
-    protected function checkParentMenu(): bool
-    {
-        return DB::table('admin_menu')
-            ->where('id', $this->parentMenuId)
-            ->where('title', '外接管理')
-            ->exists();
-    }
-
-    /**
-     * 显示当前菜单结构
-     *
-     * @return void
-     */
-    protected function displayCurrentStructure(): void
-    {
-        $this->info("\n当前菜单结构:");
-        $this->line("外接管理 (fa-plug)");
-
-        $currentMenus = DB::table('admin_menu')
-            ->where('parent_id', $this->parentMenuId)
-            ->orderBy('order')
-            ->get(['title', 'uri', 'icon']);
-
-        foreach ($currentMenus as $menu) {
-            $icon = $menu->icon ? "({$menu->icon})" : '';
-            $this->line("  ├── {$menu->title} {$icon} -> {$menu->uri}");
-        }
-    }
-
-    /**
-     * 备份现有菜单
-     *
-     * @return void
-     */
-    protected function backupCurrentMenus(): void
-    {
-        $timestamp = now()->format('Y_m_d_H_i_s');
-        $backupTable = "kku_admin_menu_backup_{$timestamp}";
-
-        // 创建备份表
-        DB::statement("CREATE TABLE {$backupTable} AS SELECT * FROM kku_admin_menu WHERE parent_id = {$this->parentMenuId}");
-
-        $this->info("✓ 已备份现有菜单到表: {$backupTable}");
-    }
-
-    /**
-     * 删除现有子菜单
-     *
-     * @return void
-     */
-    protected function deleteExistingSubMenus(): void
-    {
-        $deletedCount = DB::table('admin_menu')
-            ->where('parent_id', $this->parentMenuId)
-            ->delete();
-
-        $this->info("✓ 删除了 {$deletedCount} 个现有子菜单");
-    }
-
-    /**
-     * 创建新的菜单结构
-     *
-     * @return void
-     */
-    protected function createNewMenuStructure(): void
-    {
-        $now = now();
-
-        foreach ($this->menuStructure as $moduleKey => $module) {
-            // 创建模块菜单(二级菜单)
-            $moduleId = DB::table('admin_menu')->insertGetId([
-                'parent_id' => $this->parentMenuId,
-                'order' => $module['order'],
-                'title' => $module['title'],
-                'icon' => $module['icon'],
-                'uri' => '',
-                'extension' => '',
-                'show' => 1,
-                'created_at' => $now,
-                'updated_at' => $now,
-            ]);
-
-            $this->line("✓ 创建模块菜单: {$module['title']} (ID: {$moduleId})");
-
-            // 创建模块下的子菜单(三级菜单)
-            foreach ($module['children'] as $child) {
-                $childId = DB::table('admin_menu')->insertGetId([
-                    'parent_id' => $moduleId,
-                    'order' => $child['order'],
-                    'title' => $child['title'],
-                    'icon' => $child['icon'],
-                    'uri' => $child['uri'],
-                    'extension' => '',
-                    'show' => 1,
-                    'created_at' => $now,
-                    'updated_at' => $now,
-                ]);
-
-                $this->line("  ├── 创建子菜单: {$child['title']} (ID: {$childId}) -> {$child['uri']}");
-            }
-        }
-    }
-
-    /**
-     * 显示新的菜单结构
-     *
-     * @return void
-     */
-    protected function displayNewStructure(): void
-    {
-        $this->info("\n新的菜单结构:");
-        $this->line("外接管理 (fa-plug)");
-
-        // 获取二级菜单
-        $modules = DB::table('admin_menu')
-            ->where('parent_id', $this->parentMenuId)
-            ->orderBy('order')
-            ->get(['id', 'title', 'icon']);
-
-        foreach ($modules as $module) {
-            $icon = $module->icon ? "({$module->icon})" : '';
-            $this->line("├── {$module->title} {$icon}");
-
-            // 获取三级菜单
-            $children = DB::table('admin_menu')
-                ->where('parent_id', $module->id)
-                ->orderBy('order')
-                ->get(['title', 'uri', 'icon']);
-
-            foreach ($children as $child) {
-                $childIcon = $child->icon ? "({$child->icon})" : '';
-                $this->line("│   ├── {$child->title} {$childIcon} -> {$child->uri}");
-            }
-        }
-    }
-
-    /**
-     * 获取菜单统计信息
-     *
-     * @return array
-     */
-    protected function getMenuStats(): array
-    {
-        $modules = DB::table('admin_menu')
-            ->where('parent_id', $this->parentMenuId)
-            ->count();
-
-        $totalChildren = DB::table('admin_menu as parent')
-            ->join('admin_menu as child', 'parent.id', '=', 'child.parent_id')
-            ->where('parent.parent_id', $this->parentMenuId)
-            ->count();
-
-        return [
-            'modules' => $modules,
-            'total_children' => $totalChildren,
-        ];
-    }
-
-    /**
-     * 验证菜单结构
-     *
-     * @return bool
-     */
-    protected function validateMenuStructure(): bool
-    {
-        $stats = $this->getMenuStats();
-        $expectedModules = count($this->menuStructure);
-        $expectedChildren = array_sum(array_map(fn($module) => count($module['children']), $this->menuStructure));
-
-        if ($stats['modules'] !== $expectedModules) {
-            $this->warn("模块数量不匹配: 期望 {$expectedModules} 个,实际 {$stats['modules']} 个");
-            return false;
-        }
-
-        if ($stats['total_children'] !== $expectedChildren) {
-            $this->warn("子菜单数量不匹配: 期望 {$expectedChildren} 个,实际 {$stats['total_children']} 个");
-            return false;
-        }
-
-        return true;
-    }
-}

+ 41 - 40
app/Module/ThirdParty/Services/BaseRequest.php

@@ -14,7 +14,7 @@ use Illuminate\Http\Client\Response;
 
 /**
  * 第三方请求基类
- * 
+ *
  * 抽象化请求概念,不局限于HTTP请求,扩展到调用包的请求方法
  * 基类处理配置读取、配额管理、日志记录等通用功能
  */
@@ -24,30 +24,30 @@ abstract class BaseRequest
      * 服务代码
      */
     protected string $serviceCode;
-    
+
     /**
      * 服务配置
      */
     protected ?ServiceModel $service = null;
-    
+
     /**
      * 认证凭证
      */
     protected ?ThirdPartyCredential $credential = null;
-    
+
     /**
      * 请求ID
      */
     protected string $requestId;
-    
+
     /**
      * 开始时间
      */
     protected float $startTime;
-    
+
     /**
      * 构造函数
-     * 
+     *
      * @param string $serviceCode 服务代码
      */
     public function __construct(string $serviceCode)
@@ -55,38 +55,38 @@ abstract class BaseRequest
         $this->serviceCode = $serviceCode;
         $this->requestId = uniqid('req_', true);
         $this->startTime = microtime(true);
-        
+
         // 初始化服务配置
         $this->initializeService();
     }
-    
+
     /**
      * 初始化服务配置
-     * 
+     *
      * @throws \Exception
      */
     protected function initializeService(): void
     {
         $this->service = ServiceModel::where('code', $this->serviceCode)->first();
-        
+
         if (!$this->service) {
             throw new \Exception("服务 {$this->serviceCode} 不存在");
         }
-        
+
         if (!$this->service->canCallApi()) {
             throw new \Exception("服务 {$this->serviceCode} 当前不可用,状态:{$this->service->getStatusLabel()}");
         }
-        
+
         // 获取认证凭证
         $this->credential = $this->service->getActiveCredential();
         if (!$this->credential) {
             throw new \Exception("服务 {$this->serviceCode} 没有可用的认证凭证");
         }
     }
-    
+
     /**
      * 执行请求
-     * 
+     *
      * @param array $params 请求参数
      * @return array
      * @throws \Exception
@@ -97,48 +97,49 @@ abstract class BaseRequest
         if (!$this->checkQuota()) {
             throw new \Exception("服务 {$this->serviceCode} 配额已用完");
         }
-        
+
         try {
             // 执行具体的请求逻辑
             $result = $this->handler($params);
-            
+
             // 记录成功日志
             $this->logRequest($params, $result, true);
-            
+
             // 更新配额
             $this->updateQuota();
-            
+
             // 更新凭证使用统计
             $this->credential->updateUsageStats();
-            
+
             return $result;
-            
+
         } catch (\Exception $e) {
             // 记录失败日志
             $this->logRequest($params, ['error' => $e->getMessage()], false);
-            
+
             throw $e;
         }
     }
-    
+
     /**
      * 具体的请求处理逻辑(由子类实现)
-     * 
+     *
      * @param array $params 请求参数
      * @return array
      */
     abstract protected function handler(array $params): array;
-    
+
+
     /**
      * 检查配额
-     * 
+     *
      * @return bool
      */
     protected function checkQuota(): bool
     {
         return QuotaService::checkQuota($this->service);
     }
-    
+
     /**
      * 更新配额
      */
@@ -146,10 +147,10 @@ abstract class BaseRequest
     {
         QuotaService::updateQuota($this->service);
     }
-    
+
     /**
      * 记录请求日志
-     * 
+     *
      * @param array $params 请求参数
      * @param array $result 响应结果
      * @param bool $success 是否成功
@@ -157,7 +158,7 @@ abstract class BaseRequest
     protected function logRequest(array $params, array $result, bool $success): void
     {
         $responseTime = (int)((microtime(true) - $this->startTime) * 1000);
-        
+
         ThirdPartyLog::create([
             'service_id' => $this->service->id,
             'credential_id' => $this->credential->id,
@@ -178,47 +179,47 @@ abstract class BaseRequest
             'called_at' => now(),
         ]);
     }
-    
+
     /**
      * 获取服务配置
-     * 
+     *
      * @param string|null $key 配置键名,为空则返回全部配置
      * @return mixed
      */
     protected function getConfig(?string $key = null)
     {
         $config = $this->service->config ?? [];
-        
+
         if ($key === null) {
             return $config;
         }
-        
+
         return $config[$key] ?? null;
     }
-    
+
     /**
      * 获取服务信息
-     * 
+     *
      * @return ServiceModel
      */
     protected function getService(): ServiceModel
     {
         return $this->service;
     }
-    
+
     /**
      * 获取认证凭证
-     * 
+     *
      * @return ThirdPartyCredential
      */
     protected function getCredential(): ThirdPartyCredential
     {
         return $this->credential;
     }
-    
+
     /**
      * 获取请求ID
-     * 
+     *
      * @return string
      */
     protected function getRequestId(): string

+ 22 - 0
tests/Unit/ThirdParty/Urs/UrsGetUserInfoRequestTest.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Tests\Unit\ThirdParty\Urs;
+
+use Tests\TestCase;
+use ThirdParty\Urs\Request\UrsGetUserInfoRequest;
+
+/**
+ *
+ */
+class UrsGetUserInfoRequestTest extends TestCase
+{
+
+    public function test1()
+    {
+
+        $request = new UrsGetUserInfoRequest();
+        $request->request();
+
+    }
+
+}