bianjunhui 3 недель назад
Родитель
Сommit
9ab0ecbf02

+ 154 - 0
packages/Longyi/DynamicMenu/src/Console/Commands/InitializeSettings.php

@@ -0,0 +1,154 @@
+<?php
+
+namespace Longyi\DynamicMenu\Console\Commands;
+
+use Illuminate\Console\Command;
+use Longyi\DynamicMenu\Models\MenuItem;
+use Illuminate\Support\Facades\Schema;
+
+class InitializeSettings extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'dynamic-menu:init-settings {--force : Force recreate menu items}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Initialize dynamic menu settings and create default menu items';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        $this->info('Initializing dynamic menu settings...');
+
+        try {
+            // 检查数据库表是否存在
+            if (!Schema::hasTable('dynamic_menu_items')) {
+                $this->error('Table "dynamic_menu_items" not found.');
+                $this->warn('Please run migrations first: php artisan migrate');
+                return 1;
+            }
+
+            // 创建默认菜单项
+            $this->createDefaultMenuItems();
+
+            $this->info('✓ Dynamic menu initialized successfully!');
+            $this->info('You can now manage menu items in admin panel.');
+
+            return 0;
+
+        } catch (\Exception $e) {
+            $this->error('Error initializing dynamic menu: ' . $e->getMessage());
+            \Log::error('DynamicMenu initialization error: ' . $e->getMessage() . "\n" . $e->getTraceAsString());
+            return 1;
+        }
+    }
+
+    /**
+     * 创建默认菜单项
+     */
+    protected function createDefaultMenuItems()
+    {
+        $this->info('Creating default menu items...');
+
+        // 检查是否已存在动态菜单的菜单项
+        $existingParent = MenuItem::where('key', 'dynamicmenu')->first();
+
+        if ($existingParent) {
+            $this->warn('Dynamic menu items already exist.');
+
+            if (!$this->option('force')) {
+                $answer = $this->ask('Do you want to recreate them? (y/n)', 'n');
+                if (strtolower($answer) !== 'y') {
+                    return;
+                }
+            }
+
+            // 删除已存在的菜单项
+            $this->deleteExistingMenuItems($existingParent);
+        }
+
+        // 获取当前管理员 ID
+        $adminId = auth()->guard('admin')->check() ? auth()->guard('admin')->id() : 1;
+
+        // 创建父级菜单项
+        $parentItem = MenuItem::create([
+            'name' => '自定义菜单',
+            'key' => 'dynamicmenu',
+            'route' => 'admin.dynamicmenu.index',
+            'icon' => 'icon-menu',
+            'sort_order' => 13,
+            'status' => true,
+            'created_by' => $adminId,
+        ]);
+
+        $this->info("✓ Parent menu created: {$parentItem->name}");
+
+        // 创建子菜单项
+        $childItems = [
+            [
+                'name' => '菜单列表',
+                'key' => 'dynamicmenu.menu',
+                'route' => 'admin.dynamicmenu.index',
+                'icon' => 'icon-list',
+                'sort_order' => 1,
+            ],
+            [
+                'name' => '权限管理',
+                'key' => 'dynamicmenu.permission',
+                'route' => 'admin.dynamicmenu.permission',
+                'icon' => 'icon-lock',
+                'sort_order' => 2,
+            ],
+        ];
+
+        foreach ($childItems as $item) {
+            MenuItem::create([
+                'name' => $item['name'],
+                'key' => $item['key'],
+                'route' => $item['route'],
+                'icon' => $item['icon'],
+                'sort_order' => $item['sort_order'],
+                'parent_id' => $parentItem->id,
+                'status' => true,
+                'created_by' => $adminId,
+            ]);
+            $this->info("  ✓ Child menu created: {$item['name']}");
+        }
+
+        $this->info('✓ All menu items created successfully!');
+    }
+
+    /**
+     * 删除已存在的菜单项
+     */
+    protected function deleteExistingMenuItems($menuItem)
+    {
+        $this->info('Removing existing menu items...');
+
+        try {
+            // 先删除所有子菜单
+            $children = MenuItem::where('parent_id', $menuItem->id)->get();
+            foreach ($children as $child) {
+                $child->delete();
+            }
+
+            // 删除父菜单
+            $menuItem->delete();
+
+            $this->info('✓ Existing menu items deleted.');
+
+        } catch (\Exception $e) {
+            $this->error('Failed to delete existing menu items: ' . $e->getMessage());
+            throw $e;
+        }
+    }
+}

+ 16 - 11
packages/Longyi/DynamicMenu/src/Providers/DynamicMenuServiceProvider.php

@@ -12,10 +12,10 @@ class DynamicMenuServiceProvider extends ServiceProvider
     {
         // 加载迁移
         $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
-        
+
         // 加载路由
         $this->loadRoutesFrom(__DIR__ . '/../Routes/admin-routes.php');
-        
+
         // 加载视图
         $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'dynamicmenu');
          // 加载语言文件
@@ -32,6 +32,11 @@ class DynamicMenuServiceProvider extends ServiceProvider
         $this->app->booted(function () {
             $this->mergeDynamicMenuConfig();
         });
+        if ($this->app->runningInConsole()) {
+            $this->commands([
+                \Longyi\DynamicMenu\Console\Commands\InitializeSettings::class,
+            ]);
+        }
     }
 
     /**
@@ -48,13 +53,13 @@ class DynamicMenuServiceProvider extends ServiceProvider
             $mergedConfig = array_merge($existingConfig, $menuConfig);
             // 设置配置
             $this->app['config']->set('menu.admin', $mergedConfig);
-            
+
         } catch (\Exception $e) {
             \Log::error('合并动态菜单配置失败: ' . $e->getMessage());
         }
     }
 
-  
+
     /**
      * 从数据库获取菜单配置
      */
@@ -66,10 +71,10 @@ class DynamicMenuServiceProvider extends ServiceProvider
                         ->where('status', 1)
                         ->orderBy('sort_order')
                         ->get();
-                    
+
             return $this->buildMenuConfig($menuItems);
         }
-        
+
         // 使用缓存
         return Cache::remember('dynamic_menu_config', 3600, function () {
             try {
@@ -77,9 +82,9 @@ class DynamicMenuServiceProvider extends ServiceProvider
                     ->where('status', 1)
                     ->orderBy('sort_order')
                     ->get();
-                
+
                 return $this->buildMenuConfig($menuItems);
-                
+
             } catch (\Exception $e) {
                 \Log::warning('无法从数据库获取菜单:' . $e->getMessage());
                 return [];
@@ -95,7 +100,7 @@ class DynamicMenuServiceProvider extends ServiceProvider
     protected function buildMenuConfig($menuItems)
     {
         $config = [];
-        
+
         foreach ($menuItems as $item) {
             $menuItem = [
                 'key'   => $item->key,
@@ -106,7 +111,7 @@ class DynamicMenuServiceProvider extends ServiceProvider
             ];
             $config[] = $menuItem;
         }
-        
+
         return $config;
     }
-}
+}

+ 15 - 15
packages/Longyi/RewardPoints/src/Console/Commands/InitializeSettings.php

@@ -23,39 +23,39 @@ class InitializeSettings extends Command
     public function handle()
     {
         $this->info('Initializing reward points settings...');
-        
+
         try {
             $this->settingRepository->initializeDefaultSettings();
-            
+
             $this->info('✓ Default settings initialized successfully!');
             $this->info('You can now configure reward points in admin panel.');
-            
+
             // 添加积分模块菜单项到动态菜单
             $this->addMenuItems();
-            
+
             return 0;
-            
+
         } catch (\Exception $e) {
             $this->error('Error initializing settings: ' . $e->getMessage());
             return 1;
         }
     }
-    
+
     /**
      * 添加积分模块的菜单项到动态菜单
      */
     protected function addMenuItems()
     {
         $this->info('Adding reward points menu items to dynamic menu...');
-        
+
         // 检查是否已存在积分模块的菜单项
         $existingParent = MenuItem::where('key', 'settings.reward-points')->first();
-        
+
         if ($existingParent) {
             $this->warn('Reward points menu items already exist in dynamic menu.');
             return;
         }
-        
+
         // 创建父级菜单项
         $parentItem = MenuItem::create([
             'name' => '积分管理',
@@ -66,7 +66,7 @@ class InitializeSettings extends Command
             'status' => 1,
             'created_by' => 1,
         ]);
-        
+
         // 创建子菜单项
         $childItems = [
             [
@@ -84,9 +84,9 @@ class InitializeSettings extends Command
                 'sort_order' => 2,
             ],
             [
-                'name' => '积分报表',
+                'name' => '积分记录',
                 'key' => 'rewardPoint.reports',
-                'route' => 'admin.reward-points.reports.index',
+                'route' => 'admin.reward-points.transactions.index',
                 'icon' => 'icon-chart',
                 'sort_order' => 3,
             ],
@@ -98,7 +98,7 @@ class InitializeSettings extends Command
                 'sort_order' => 4,
             ],
         ];
-        
+
         foreach ($childItems as $item) {
             MenuItem::create([
                 'name' => $item['name'],
@@ -111,7 +111,7 @@ class InitializeSettings extends Command
                 'created_by' => 1,
             ]);
         }
-        
+
         $this->info('✓ Reward points menu items added to dynamic menu successfully!');
     }
-}
+}