InsertThirdPartyAdminMenu.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Module\ThirdParty\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 插入ThirdParty模块后台管理菜单
  7. *
  8. * 使用方法: php artisan thirdparty:insert-admin-menu
  9. */
  10. class InsertThirdPartyAdminMenu extends Command
  11. {
  12. /**
  13. * 命令签名
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'thirdparty:insert-admin-menu {--force : 强制重新创建菜单}';
  18. /**
  19. * 命令描述
  20. *
  21. * @var string
  22. */
  23. protected $description = '插入ThirdParty模块后台管理菜单';
  24. /**
  25. * 外接管理父菜单ID
  26. *
  27. * @var int
  28. */
  29. protected int $parentMenuId = 533;
  30. /**
  31. * 菜单配置
  32. *
  33. * @var array
  34. */
  35. protected array $menus = [
  36. [
  37. 'title' => '第三方服务管理',
  38. 'uri' => 'thirdparty/services',
  39. 'icon' => 'fa-server',
  40. 'order' => 10,
  41. ],
  42. [
  43. 'title' => '认证凭证管理',
  44. 'uri' => 'thirdparty/credentials',
  45. 'icon' => 'fa-shield-alt',
  46. 'order' => 20,
  47. ],
  48. [
  49. 'title' => '调用日志管理',
  50. 'uri' => 'thirdparty/logs',
  51. 'icon' => 'fa-file-text',
  52. 'order' => 30,
  53. ],
  54. [
  55. 'title' => '配额管理',
  56. 'uri' => 'thirdparty/quotas',
  57. 'icon' => 'fa-tachometer-alt',
  58. 'order' => 40,
  59. ],
  60. [
  61. 'title' => '监控记录',
  62. 'uri' => 'thirdparty/monitors',
  63. 'icon' => 'fa-heartbeat',
  64. 'order' => 50,
  65. ],
  66. [
  67. 'title' => '统计报告',
  68. 'uri' => 'thirdparty/reports/overview',
  69. 'icon' => 'fa-chart-bar',
  70. 'order' => 60,
  71. ],
  72. ];
  73. /**
  74. * 执行命令
  75. *
  76. * @return int
  77. */
  78. public function handle(): int
  79. {
  80. try {
  81. // 检查父菜单是否存在
  82. if (!$this->checkParentMenu()) {
  83. $this->error("父菜单 '外接管理' (ID: {$this->parentMenuId}) 不存在");
  84. return 1;
  85. }
  86. // 检查是否强制重新创建
  87. if ($this->option('force')) {
  88. $this->deleteExistingMenus();
  89. }
  90. // 插入菜单
  91. $insertedCount = $this->insertMenus();
  92. if ($insertedCount > 0) {
  93. $this->info("成功插入 {$insertedCount} 个ThirdParty模块菜单");
  94. $this->displayMenuStructure();
  95. } else {
  96. $this->warn("没有插入新菜单,可能菜单已存在");
  97. $this->info("使用 --force 选项可以强制重新创建菜单");
  98. }
  99. return 0;
  100. } catch (\Exception $e) {
  101. $this->error("插入菜单失败: " . $e->getMessage());
  102. return 1;
  103. }
  104. }
  105. /**
  106. * 检查父菜单是否存在
  107. *
  108. * @return bool
  109. */
  110. protected function checkParentMenu(): bool
  111. {
  112. return DB::table('admin_menu')
  113. ->where('id', $this->parentMenuId)
  114. ->where('title', '外接管理')
  115. ->exists();
  116. }
  117. /**
  118. * 删除已存在的菜单
  119. *
  120. * @return void
  121. */
  122. protected function deleteExistingMenus(): void
  123. {
  124. $uris = array_column($this->menus, 'uri');
  125. $deletedCount = DB::table('admin_menu')
  126. ->where('parent_id', $this->parentMenuId)
  127. ->whereIn('uri', $uris)
  128. ->delete();
  129. if ($deletedCount > 0) {
  130. $this->info("删除了 {$deletedCount} 个已存在的菜单");
  131. }
  132. }
  133. /**
  134. * 插入菜单
  135. *
  136. * @return int
  137. */
  138. protected function insertMenus(): int
  139. {
  140. $insertedCount = 0;
  141. $now = now();
  142. foreach ($this->menus as $menu) {
  143. // 检查菜单是否已存在
  144. $exists = DB::table('admin_menu')
  145. ->where('parent_id', $this->parentMenuId)
  146. ->where('uri', $menu['uri'])
  147. ->exists();
  148. if (!$exists) {
  149. DB::table('admin_menu')->insert([
  150. 'parent_id' => $this->parentMenuId,
  151. 'order' => $menu['order'],
  152. 'title' => $menu['title'],
  153. 'icon' => $menu['icon'],
  154. 'uri' => $menu['uri'],
  155. 'extension' => '',
  156. 'show' => 1,
  157. 'created_at' => $now,
  158. 'updated_at' => $now,
  159. ]);
  160. $insertedCount++;
  161. $this->line("✓ 插入菜单: {$menu['title']} ({$menu['uri']})");
  162. } else {
  163. $this->line("- 菜单已存在: {$menu['title']} ({$menu['uri']})");
  164. }
  165. }
  166. return $insertedCount;
  167. }
  168. /**
  169. * 显示菜单结构
  170. *
  171. * @return void
  172. */
  173. protected function displayMenuStructure(): void
  174. {
  175. $this->info("\n当前ThirdParty模块菜单结构:");
  176. $this->line("外接管理 (fa-plug)");
  177. $menus = DB::table('admin_menu')
  178. ->where('parent_id', $this->parentMenuId)
  179. ->orderBy('order')
  180. ->get(['title', 'uri', 'icon']);
  181. foreach ($menus as $menu) {
  182. $icon = $menu->icon ? "({$menu->icon})" : '';
  183. $this->line(" ├── {$menu->title} {$icon} -> {$menu->uri}");
  184. }
  185. }
  186. /**
  187. * 获取菜单统计信息
  188. *
  189. * @return array
  190. */
  191. protected function getMenuStats(): array
  192. {
  193. $totalMenus = DB::table('admin_menu')
  194. ->where('parent_id', $this->parentMenuId)
  195. ->count();
  196. $thirdPartyMenus = DB::table('admin_menu')
  197. ->where('parent_id', $this->parentMenuId)
  198. ->where('uri', 'like', 'thirdparty/%')
  199. ->count();
  200. return [
  201. 'total' => $totalMenus,
  202. 'thirdparty' => $thirdPartyMenus,
  203. 'others' => $totalMenus - $thirdPartyMenus,
  204. ];
  205. }
  206. /**
  207. * 验证菜单完整性
  208. *
  209. * @return bool
  210. */
  211. protected function validateMenus(): bool
  212. {
  213. $stats = $this->getMenuStats();
  214. $expectedCount = count($this->menus);
  215. if ($stats['thirdparty'] !== $expectedCount) {
  216. $this->warn("菜单数量不匹配: 期望 {$expectedCount} 个,实际 {$stats['thirdparty']} 个");
  217. return false;
  218. }
  219. // 检查每个菜单是否存在
  220. foreach ($this->menus as $menu) {
  221. $exists = DB::table('admin_menu')
  222. ->where('parent_id', $this->parentMenuId)
  223. ->where('uri', $menu['uri'])
  224. ->where('title', $menu['title'])
  225. ->exists();
  226. if (!$exists) {
  227. $this->warn("菜单缺失: {$menu['title']} ({$menu['uri']})");
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. /**
  234. * 显示帮助信息
  235. *
  236. * @return void
  237. */
  238. protected function showHelp(): void
  239. {
  240. $this->info("ThirdParty模块菜单管理命令");
  241. $this->line("");
  242. $this->line("用法:");
  243. $this->line(" php artisan thirdparty:insert-admin-menu # 插入菜单");
  244. $this->line(" php artisan thirdparty:insert-admin-menu --force # 强制重新创建菜单");
  245. $this->line("");
  246. $this->line("说明:");
  247. $this->line(" - 菜单将插入到 '外接管理' 父菜单下");
  248. $this->line(" - 如果菜单已存在,默认跳过插入");
  249. $this->line(" - 使用 --force 选项可以删除已存在的菜单并重新创建");
  250. }
  251. }