2026_07_21_121000_add_egame_admin_menus.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Schema;
  5. return new class extends Migration {
  6. private const MENU_URI = 'egame';
  7. private const MENU_PERMISSION_NAME = 'egame.catalog.menu';
  8. private const BUTTONS = [
  9. 'admin/egame/options' => '筛选选项',
  10. 'admin/egame/items' => '配置列表',
  11. 'admin/egame/update' => '保存配置',
  12. 'admin/egame/setStatus' => '切换状态',
  13. 'admin/egame/delete' => '删除配置',
  14. ];
  15. private const BUTTON_PERMISSION_NAMES = [
  16. 'admin/egame/options' => 'egame.catalog.options',
  17. 'admin/egame/items' => 'egame.catalog.items',
  18. 'admin/egame/update' => 'egame.catalog.update',
  19. 'admin/egame/setStatus' => 'egame.catalog.status',
  20. 'admin/egame/delete' => 'egame.catalog.delete',
  21. ];
  22. public function up(): void
  23. {
  24. if (!Schema::hasTable('menus')) {
  25. return;
  26. }
  27. $now = date('Y-m-d H:i:s');
  28. $menu = DB::table('menus')
  29. ->where('uri', self::MENU_URI)
  30. ->where('type', 1)
  31. ->first(['id', 'status']);
  32. if (!$menu) {
  33. $menuId = DB::table('menus')->insertGetId([
  34. 'parent_id' => 0,
  35. 'title' => '第三方游戏',
  36. 'icon' => null,
  37. 'uri' => self::MENU_URI,
  38. 'permission_name' => self::MENU_PERMISSION_NAME,
  39. 'sort' => 0,
  40. 'status' => 1,
  41. 'type' => 1,
  42. 'created_at' => $now,
  43. 'updated_at' => $now,
  44. ]);
  45. } else {
  46. $menuId = (int)$menu->id;
  47. if ((int)$menu->status !== 1) {
  48. DB::table('menus')->where('id', $menuId)->update([
  49. 'status' => 1,
  50. 'updated_at' => $now,
  51. ]);
  52. }
  53. }
  54. $sort = 0;
  55. foreach (self::BUTTONS as $uri => $title) {
  56. $button = DB::table('menus')
  57. ->where('uri', $uri)
  58. ->first(['id', 'parent_id', 'title', 'status', 'type']);
  59. if ($button) {
  60. $updates = [];
  61. if ((int)$button->parent_id !== $menuId) {
  62. $updates['parent_id'] = $menuId;
  63. }
  64. if ((int)$button->type !== 2) {
  65. $updates['type'] = 2;
  66. }
  67. if (trim((string)$button->title) === '') {
  68. $updates['title'] = $title;
  69. }
  70. if ((int)$button->status !== 1) {
  71. $updates['status'] = 1;
  72. }
  73. if ($updates !== []) {
  74. $updates['updated_at'] = $now;
  75. DB::table('menus')->where('id', $button->id)->update($updates);
  76. }
  77. } else {
  78. DB::table('menus')->insert([
  79. 'parent_id' => $menuId,
  80. 'title' => $title,
  81. 'icon' => null,
  82. 'uri' => $uri,
  83. 'permission_name' => self::BUTTON_PERMISSION_NAMES[$uri],
  84. 'sort' => $sort,
  85. 'status' => 1,
  86. 'type' => 2,
  87. 'created_at' => $now,
  88. 'updated_at' => $now,
  89. ]);
  90. }
  91. $sort++;
  92. }
  93. }
  94. public function down(): void
  95. {
  96. // This is an irreversible menu and permission data migration. Existing
  97. // rows may have been reused, reparented, and re-enabled, so their prior
  98. // parent and status state cannot be reconstructed safely on rollback.
  99. }
  100. };