|
|
@@ -1,403 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace App\Console\Commands;
|
|
|
-
|
|
|
-use App\Module\System\Models\AdminMenu;
|
|
|
-use Illuminate\Console\Command;
|
|
|
-use Illuminate\Support\Str;
|
|
|
-
|
|
|
-class FixAdminMenus extends Command
|
|
|
-{
|
|
|
- /**
|
|
|
- * The name and signature of the console command.
|
|
|
- *
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected $signature = 'admin:fix-menus {module? : 指定要修复的模块,不指定则修复所有模块}';
|
|
|
-
|
|
|
- /**
|
|
|
- * The console command description.
|
|
|
- *
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected $description = '修复AdminMenu表中的菜单项';
|
|
|
-
|
|
|
- /**
|
|
|
- * 游戏系统设置菜单ID
|
|
|
- *
|
|
|
- * @var int
|
|
|
- */
|
|
|
- protected $systemMenuId = 259;
|
|
|
-
|
|
|
- /**
|
|
|
- * 游戏运营管理菜单ID
|
|
|
- *
|
|
|
- * @var int
|
|
|
- */
|
|
|
- protected $operationMenuId = 260;
|
|
|
-
|
|
|
- /**
|
|
|
- * 模块配置菜单映射
|
|
|
- *
|
|
|
- * @var array
|
|
|
- */
|
|
|
- protected $moduleConfigMenus = [];
|
|
|
-
|
|
|
- /**
|
|
|
- * 模块管理菜单映射
|
|
|
- *
|
|
|
- * @var array
|
|
|
- */
|
|
|
- protected $moduleManageMenus = [];
|
|
|
-
|
|
|
- /**
|
|
|
- * Execute the console command.
|
|
|
- *
|
|
|
- * @return int
|
|
|
- */
|
|
|
- public function handle()
|
|
|
- {
|
|
|
- $module = $this->argument('module');
|
|
|
-
|
|
|
- // 初始化模块菜单映射
|
|
|
- $this->initModuleMenus();
|
|
|
-
|
|
|
- if ($module) {
|
|
|
- $this->info("开始修复{$module}模块的菜单项...");
|
|
|
- $this->fixModuleMenus($module);
|
|
|
- } else {
|
|
|
- $this->info("开始修复所有模块的菜单项...");
|
|
|
- $modules = [
|
|
|
- 'Team', 'Pet', 'Farm', 'GameItems', 'Task', 'Activity', 'Article',
|
|
|
- 'Fund', 'Game', 'OAuth', 'Sms', 'System', 'User'
|
|
|
- ];
|
|
|
-
|
|
|
- foreach ($modules as $mod) {
|
|
|
- $this->fixModuleMenus($mod);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $this->info("菜单项修复完成!");
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化模块菜单映射
|
|
|
- *
|
|
|
- * @return void
|
|
|
- */
|
|
|
- protected function initModuleMenus()
|
|
|
- {
|
|
|
- // 获取游戏系统设置下的子菜单
|
|
|
- $systemSubMenus = AdminMenu::where('parent_id', $this->systemMenuId)->get();
|
|
|
- foreach ($systemSubMenus as $menu) {
|
|
|
- $moduleName = $this->guessModuleName($menu->title);
|
|
|
- if ($moduleName) {
|
|
|
- $this->moduleConfigMenus[$moduleName] = $menu->id;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 获取游戏运营管理下的子菜单
|
|
|
- $operationSubMenus = AdminMenu::where('parent_id', $this->operationMenuId)->get();
|
|
|
- foreach ($operationSubMenus as $menu) {
|
|
|
- $moduleName = $this->guessModuleName($menu->title);
|
|
|
- if ($moduleName) {
|
|
|
- $this->moduleManageMenus[$moduleName] = $menu->id;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据菜单标题猜测模块名称
|
|
|
- *
|
|
|
- * @param string $title
|
|
|
- * @return string|null
|
|
|
- */
|
|
|
- protected function guessModuleName(string $title): ?string
|
|
|
- {
|
|
|
- $moduleMap = [
|
|
|
- '团队' => 'Team',
|
|
|
- '宠物' => 'Pet',
|
|
|
- '农场' => 'Farm',
|
|
|
- '游戏物品' => 'GameItems',
|
|
|
- '任务' => 'Task',
|
|
|
- '活动' => 'Activity',
|
|
|
- '文章' => 'Article',
|
|
|
- '资金' => 'Fund',
|
|
|
- '游戏' => 'Game',
|
|
|
- '认证' => 'OAuth',
|
|
|
- '短信' => 'Sms',
|
|
|
- '系统' => 'System',
|
|
|
- '用户' => 'User'
|
|
|
- ];
|
|
|
-
|
|
|
- foreach ($moduleMap as $keyword => $module) {
|
|
|
- if (Str::contains($title, $keyword)) {
|
|
|
- return $module;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 修复指定模块的菜单项
|
|
|
- *
|
|
|
- * @param string $module
|
|
|
- * @return void
|
|
|
- */
|
|
|
- protected function fixModuleMenus(string $module)
|
|
|
- {
|
|
|
- $this->info("修复{$module}模块的菜单项...");
|
|
|
-
|
|
|
- // 获取模块的后台控制器
|
|
|
- $controllerDir = app_path("Module/{$module}/AdminControllers");
|
|
|
- if (!is_dir($controllerDir)) {
|
|
|
- $this->warn(" - 未找到AdminControllers目录");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- $controllers = array_filter(scandir($controllerDir), function($file) {
|
|
|
- return !in_array($file, ['.', '..']) &&
|
|
|
- !is_dir(app_path("Module/{$this->argument('module')}/AdminControllers/{$file}")) &&
|
|
|
- pathinfo($file, PATHINFO_EXTENSION) === 'php';
|
|
|
- });
|
|
|
-
|
|
|
- // 获取最大的order值
|
|
|
- $maxOrder = AdminMenu::max('order');
|
|
|
- $nextOrder = $maxOrder + 1;
|
|
|
-
|
|
|
- // 创建或获取模块配置菜单
|
|
|
- $configMenuId = $this->getOrCreateModuleConfigMenu($module, $nextOrder);
|
|
|
- $nextOrder++;
|
|
|
-
|
|
|
- // 创建或获取模块管理菜单
|
|
|
- $manageMenuId = $this->getOrCreateModuleManageMenu($module, $nextOrder);
|
|
|
- $nextOrder++;
|
|
|
-
|
|
|
- // 分类控制器
|
|
|
- $configControllers = [];
|
|
|
- $manageControllers = [];
|
|
|
-
|
|
|
- foreach ($controllers as $controller) {
|
|
|
- $controllerName = pathinfo($controller, PATHINFO_FILENAME);
|
|
|
-
|
|
|
- // 跳过Helper目录和文件
|
|
|
- if (Str::contains($controllerName, 'Helper')) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 根据控制器名称判断是配置还是管理
|
|
|
- if (Str::contains($controllerName, ['Config', 'Type', 'Category', 'Rule'])) {
|
|
|
- $configControllers[] = $controllerName;
|
|
|
- } else {
|
|
|
- $manageControllers[] = $controllerName;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 添加配置菜单项
|
|
|
- foreach ($configControllers as $controller) {
|
|
|
- $this->addMenuItemForController($module, $controller, $configMenuId, $nextOrder);
|
|
|
- $nextOrder++;
|
|
|
- }
|
|
|
-
|
|
|
- // 添加管理菜单项
|
|
|
- foreach ($manageControllers as $controller) {
|
|
|
- $this->addMenuItemForController($module, $controller, $manageMenuId, $nextOrder);
|
|
|
- $nextOrder++;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取或创建模块配置菜单
|
|
|
- *
|
|
|
- * @param string $module
|
|
|
- * @param int $order
|
|
|
- * @return int
|
|
|
- */
|
|
|
- protected function getOrCreateModuleConfigMenu(string $module, int $order): int
|
|
|
- {
|
|
|
- if (isset($this->moduleConfigMenus[$module])) {
|
|
|
- return $this->moduleConfigMenus[$module];
|
|
|
- }
|
|
|
-
|
|
|
- $title = $this->getModuleTitle($module) . '配置';
|
|
|
- $icon = $this->getModuleIcon($module);
|
|
|
-
|
|
|
- $menu = AdminMenu::firstOrCreate(
|
|
|
- ['title' => $title, 'uri' => ''],
|
|
|
- [
|
|
|
- 'parent_id' => $this->systemMenuId,
|
|
|
- 'order' => $order,
|
|
|
- 'icon' => $icon,
|
|
|
- 'uri' => '',
|
|
|
- 'show' => 1
|
|
|
- ]
|
|
|
- );
|
|
|
-
|
|
|
- $this->info(" - 创建{$module}模块配置菜单: {$menu->title}, ID: {$menu->id}");
|
|
|
- $this->moduleConfigMenus[$module] = $menu->id;
|
|
|
-
|
|
|
- return $menu->id;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取或创建模块管理菜单
|
|
|
- *
|
|
|
- * @param string $module
|
|
|
- * @param int $order
|
|
|
- * @return int
|
|
|
- */
|
|
|
- protected function getOrCreateModuleManageMenu(string $module, int $order): int
|
|
|
- {
|
|
|
- if (isset($this->moduleManageMenus[$module])) {
|
|
|
- return $this->moduleManageMenus[$module];
|
|
|
- }
|
|
|
-
|
|
|
- $title = $this->getModuleTitle($module) . '管理';
|
|
|
- $icon = $this->getModuleIcon($module);
|
|
|
-
|
|
|
- $menu = AdminMenu::firstOrCreate(
|
|
|
- ['title' => $title, 'uri' => ''],
|
|
|
- [
|
|
|
- 'parent_id' => $this->operationMenuId,
|
|
|
- 'order' => $order,
|
|
|
- 'icon' => $icon,
|
|
|
- 'uri' => '',
|
|
|
- 'show' => 1
|
|
|
- ]
|
|
|
- );
|
|
|
-
|
|
|
- $this->info(" - 创建{$module}模块管理菜单: {$menu->title}, ID: {$menu->id}");
|
|
|
- $this->moduleManageMenus[$module] = $menu->id;
|
|
|
-
|
|
|
- return $menu->id;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 为控制器添加菜单项
|
|
|
- *
|
|
|
- * @param string $module
|
|
|
- * @param string $controllerName
|
|
|
- * @param int $parentId
|
|
|
- * @param int $order
|
|
|
- * @return void
|
|
|
- */
|
|
|
- protected function addMenuItemForController(string $module, string $controllerName, int $parentId, int $order)
|
|
|
- {
|
|
|
- // 获取控制器类
|
|
|
- $controllerClass = "App\\Module\\{$module}\\AdminControllers\\{$controllerName}";
|
|
|
-
|
|
|
- // 获取URI
|
|
|
- $uri = $this->getControllerUri($controllerName);
|
|
|
-
|
|
|
- // 获取标题
|
|
|
- $title = $this->getControllerTitle($controllerName);
|
|
|
-
|
|
|
- // 检查菜单项是否存在
|
|
|
- $menuExists = AdminMenu::where('uri', $uri)->exists();
|
|
|
-
|
|
|
- if (!$menuExists) {
|
|
|
- $menu = AdminMenu::create([
|
|
|
- 'parent_id' => $parentId,
|
|
|
- 'order' => $order,
|
|
|
- 'title' => $title,
|
|
|
- 'icon' => '',
|
|
|
- 'uri' => $uri,
|
|
|
- 'show' => 1
|
|
|
- ]);
|
|
|
-
|
|
|
- $this->info(" - 创建菜单项: {$menu->title}, URI: {$menu->uri}");
|
|
|
- } else {
|
|
|
- $this->info(" - 菜单项已存在: {$title}, URI: {$uri}");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取控制器的URI
|
|
|
- *
|
|
|
- * @param string $controllerName
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getControllerUri(string $controllerName): string
|
|
|
- {
|
|
|
- // 移除Controller后缀
|
|
|
- $name = str_replace('Controller', '', $controllerName);
|
|
|
-
|
|
|
- // 转换为kebab-case
|
|
|
- $uri = Str::kebab($name);
|
|
|
-
|
|
|
- return $uri;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取控制器的标题
|
|
|
- *
|
|
|
- * @param string $controllerName
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getControllerTitle(string $controllerName): string
|
|
|
- {
|
|
|
- // 移除Controller后缀
|
|
|
- $name = str_replace('Controller', '', $controllerName);
|
|
|
-
|
|
|
- // 转换为标题
|
|
|
- $title = Str::title(Str::snake($name, ' '));
|
|
|
-
|
|
|
- return $title;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取模块标题
|
|
|
- *
|
|
|
- * @param string $module
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getModuleTitle(string $module): string
|
|
|
- {
|
|
|
- $titleMap = [
|
|
|
- 'Team' => '👥 团队',
|
|
|
- 'Pet' => '🐾 宠物',
|
|
|
- 'Farm' => '🌱 农场',
|
|
|
- 'GameItems' => '🎁 游戏物品',
|
|
|
- 'Task' => '📋 任务',
|
|
|
- 'Activity' => '🎯 活动',
|
|
|
- 'Article' => '📝 文章',
|
|
|
- 'Fund' => '💰 资金',
|
|
|
- 'Game' => '🎮 游戏',
|
|
|
- 'OAuth' => '🔑 认证',
|
|
|
- 'Sms' => '📱 短信',
|
|
|
- 'System' => '⚙️ 系统',
|
|
|
- 'User' => '👤 用户'
|
|
|
- ];
|
|
|
-
|
|
|
- return $titleMap[$module] ?? $module;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取模块图标
|
|
|
- *
|
|
|
- * @param string $module
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function getModuleIcon(string $module): string
|
|
|
- {
|
|
|
- $iconMap = [
|
|
|
- 'Team' => 'fa-users',
|
|
|
- 'Pet' => 'fa-paw',
|
|
|
- 'Farm' => 'fa-leaf',
|
|
|
- 'GameItems' => 'fa-gift',
|
|
|
- 'Task' => 'fa-tasks',
|
|
|
- 'Activity' => 'fa-bullseye',
|
|
|
- 'Article' => 'fa-file-text',
|
|
|
- 'Fund' => 'fa-money',
|
|
|
- 'Game' => 'fa-gamepad',
|
|
|
- 'OAuth' => 'fa-key',
|
|
|
- 'Sms' => 'fa-mobile',
|
|
|
- 'System' => 'fa-cogs',
|
|
|
- 'User' => 'fa-user'
|
|
|
- ];
|
|
|
-
|
|
|
- return $iconMap[$module] ?? 'fa-circle';
|
|
|
- }
|
|
|
-}
|