InsertSystemLogAdminMenu.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Module\System\Commands;
  3. use App\Module\System\Models\AdminMenu;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. /**
  7. * 添加系统日志管理菜单命令
  8. */
  9. class InsertSystemLogAdminMenu extends Command
  10. {
  11. /**
  12. * 命令签名
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'system:insert-log-menu {--force : 强制重新创建菜单}';
  17. /**
  18. * 命令描述
  19. *
  20. * @var string
  21. */
  22. protected $description = '添加系统日志管理菜单到后台管理系统';
  23. /**
  24. * 调试工具父菜单ID
  25. *
  26. * @var int
  27. */
  28. protected $debugToolsParentId = 205;
  29. /**
  30. * 执行命令
  31. *
  32. * @return int
  33. */
  34. public function handle(): int
  35. {
  36. try {
  37. // 检查父菜单是否存在
  38. if (!$this->checkParentMenu()) {
  39. $this->error("调试工具父菜单 (ID: {$this->debugToolsParentId}) 不存在");
  40. return 1;
  41. }
  42. // 检查是否强制重新创建
  43. if ($this->option('force')) {
  44. $this->deleteExistingMenu();
  45. }
  46. // 创建系统日志管理菜单
  47. $this->createSystemLogMenu();
  48. $this->info('系统日志管理菜单添加成功!');
  49. return 0;
  50. } catch (\Exception $e) {
  51. $this->error('添加系统日志管理菜单失败: ' . $e->getMessage());
  52. return 1;
  53. }
  54. }
  55. /**
  56. * 检查父菜单是否存在
  57. *
  58. * @return bool
  59. */
  60. protected function checkParentMenu(): bool
  61. {
  62. return AdminMenu::where('id', $this->debugToolsParentId)->exists();
  63. }
  64. /**
  65. * 删除现有菜单
  66. *
  67. * @return void
  68. */
  69. protected function deleteExistingMenu(): void
  70. {
  71. $existingMenu = AdminMenu::where('title', '系统日志')->first();
  72. if ($existingMenu) {
  73. $existingMenu->delete();
  74. $this->info("删除现有系统日志菜单 (ID: {$existingMenu->id})");
  75. }
  76. }
  77. /**
  78. * 创建系统日志管理菜单
  79. *
  80. * @return void
  81. */
  82. protected function createSystemLogMenu(): void
  83. {
  84. // 获取当前最大order值
  85. $maxOrder = AdminMenu::where('parent_id', $this->debugToolsParentId)->max('order');
  86. $nextOrder = $maxOrder ? $maxOrder + 1 : 163;
  87. // 创建系统日志菜单
  88. $systemLogMenu = AdminMenu::firstOrCreate(
  89. [
  90. 'title' => '系统日志',
  91. 'parent_id' => $this->debugToolsParentId
  92. ],
  93. [
  94. 'uri' => 'system-logs',
  95. 'icon' => 'fa-file-text',
  96. 'order' => $nextOrder,
  97. 'show' => 1,
  98. 'extension' => '',
  99. 'created_at' => now(),
  100. 'updated_at' => now()
  101. ]
  102. );
  103. $this->info("✓ 添加系统日志菜单: {$systemLogMenu->title}, ID: {$systemLogMenu->id}, Order: {$systemLogMenu->order}");
  104. }
  105. }