'第三方服务管理', '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; } // 检查是否强制重新创建 if ($this->option('force')) { $this->deleteExistingMenus(); } // 插入菜单 $insertedCount = $this->insertMenus(); if ($insertedCount > 0) { $this->info("成功插入 {$insertedCount} 个ThirdParty模块菜单"); $this->displayMenuStructure(); } else { $this->warn("没有插入新菜单,可能菜单已存在"); $this->info("使用 --force 选项可以强制重新创建菜单"); } 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 deleteExistingMenus(): void { $uris = array_column($this->menus, 'uri'); $deletedCount = DB::table('admin_menu') ->where('parent_id', $this->parentMenuId) ->whereIn('uri', $uris) ->delete(); if ($deletedCount > 0) { $this->info("删除了 {$deletedCount} 个已存在的菜单"); } } /** * 插入菜单 * * @return int */ protected function insertMenus(): int { $insertedCount = 0; $now = now(); foreach ($this->menus as $menu) { // 检查菜单是否已存在 $exists = DB::table('admin_menu') ->where('parent_id', $this->parentMenuId) ->where('uri', $menu['uri']) ->exists(); if (!$exists) { DB::table('admin_menu')->insert([ 'parent_id' => $this->parentMenuId, 'order' => $menu['order'], 'title' => $menu['title'], 'icon' => $menu['icon'], 'uri' => $menu['uri'], 'extension' => '', 'show' => 1, 'created_at' => $now, 'updated_at' => $now, ]); $insertedCount++; $this->line("✓ 插入菜单: {$menu['title']} ({$menu['uri']})"); } else { $this->line("- 菜单已存在: {$menu['title']} ({$menu['uri']})"); } } return $insertedCount; } /** * 显示菜单结构 * * @return void */ protected function displayMenuStructure(): void { $this->info("\n当前ThirdParty模块菜单结构:"); $this->line("外接管理 (fa-plug)"); $menus = DB::table('admin_menu') ->where('parent_id', $this->parentMenuId) ->orderBy('order') ->get(['title', 'uri', 'icon']); foreach ($menus as $menu) { $icon = $menu->icon ? "({$menu->icon})" : ''; $this->line(" ├── {$menu->title} {$icon} -> {$menu->uri}"); } } /** * 获取菜单统计信息 * * @return array */ protected function getMenuStats(): array { $totalMenus = DB::table('admin_menu') ->where('parent_id', $this->parentMenuId) ->count(); $thirdPartyMenus = DB::table('admin_menu') ->where('parent_id', $this->parentMenuId) ->where('uri', 'like', 'thirdparty/%') ->count(); return [ 'total' => $totalMenus, 'thirdparty' => $thirdPartyMenus, 'others' => $totalMenus - $thirdPartyMenus, ]; } /** * 验证菜单完整性 * * @return bool */ protected function validateMenus(): bool { $stats = $this->getMenuStats(); $expectedCount = count($this->menus); if ($stats['thirdparty'] !== $expectedCount) { $this->warn("菜单数量不匹配: 期望 {$expectedCount} 个,实际 {$stats['thirdparty']} 个"); return false; } // 检查每个菜单是否存在 foreach ($this->menus as $menu) { $exists = DB::table('admin_menu') ->where('parent_id', $this->parentMenuId) ->where('uri', $menu['uri']) ->where('title', $menu['title']) ->exists(); if (!$exists) { $this->warn("菜单缺失: {$menu['title']} ({$menu['uri']})"); return false; } } return true; } /** * 显示帮助信息 * * @return void */ protected function showHelp(): void { $this->info("ThirdParty模块菜单管理命令"); $this->line(""); $this->line("用法:"); $this->line(" php artisan thirdparty:insert-admin-menu # 插入菜单"); $this->line(" php artisan thirdparty:insert-admin-menu --force # 强制重新创建菜单"); $this->line(""); $this->line("说明:"); $this->line(" - 菜单将插入到 '外接管理' 父菜单下"); $this->line(" - 如果菜单已存在,默认跳过插入"); $this->line(" - 使用 --force 选项可以删除已存在的菜单并重新创建"); } }