|
|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|