CanImportMenu.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Dcat\Admin\Extend;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Facades\Validator;
  5. /**
  6. * @property \Symfony\Component\Console\Output\OutputInterface $output
  7. */
  8. trait CanImportMenu
  9. {
  10. protected $menu = [];
  11. protected $menuValidationRules = [
  12. 'parent' => 'nullable',
  13. 'title' => 'required',
  14. 'uri' => 'nullable',
  15. 'icon' => 'nullable',
  16. ];
  17. /**
  18. * 获取菜单节点.
  19. *
  20. * @return array
  21. */
  22. protected function menu()
  23. {
  24. return $this->menu;
  25. }
  26. /**
  27. * 添加菜单.
  28. *
  29. * @param array $menu
  30. *
  31. * @throws \Exception
  32. */
  33. protected function addMenu(array $menu = [])
  34. {
  35. $menu = $menu ?: $this->menu();
  36. if (! Arr::isAssoc($menu)) {
  37. foreach ($menu as $v) {
  38. $this->addMenu($v);
  39. }
  40. return;
  41. }
  42. if (! $this->validateMenu($menu)) {
  43. return;
  44. }
  45. if ($menuModel = $this->getMenuModel()) {
  46. $lastOrder = $menuModel::max('order');
  47. $menuModel::create([
  48. 'parent_id' => $this->getParentMenuId($menu['parent'] ?? 0),
  49. 'order' => $lastOrder + 1,
  50. 'title' => $menu['title'],
  51. 'icon' => (string) ($menu['icon'] ?? ''),
  52. 'uri' => (string) ($menu['uri'] ?? ''),
  53. 'extension' => $this->getName(),
  54. ]);
  55. }
  56. }
  57. /**
  58. * 刷新菜单.
  59. *
  60. * @throws \Exception
  61. */
  62. protected function refreshMenu()
  63. {
  64. $this->flushMenu();
  65. $this->addMenu();
  66. }
  67. /**
  68. * 根据名称获取菜单ID.
  69. *
  70. * @param int|string $parent
  71. *
  72. * @return int
  73. */
  74. protected function getParentMenuId($parent)
  75. {
  76. if (is_numeric($parent)) {
  77. return $parent;
  78. }
  79. $menuModel = $this->getMenuModel();
  80. return $menuModel::query()
  81. ->where('title', $parent)
  82. ->where('extension', $this->getName())
  83. ->value('id') ?: 0;
  84. }
  85. /**
  86. * 删除菜单.
  87. */
  88. protected function flushMenu()
  89. {
  90. $menuModel = $this->getMenuModel();
  91. if (! $menuModel) {
  92. return;
  93. }
  94. $menuModel::query()
  95. ->where('extension', $this->getName())
  96. ->delete();
  97. }
  98. /**
  99. * 验证菜单字段格式是否正确.
  100. *
  101. * @param array $menu
  102. *
  103. * @throws \Exception
  104. *
  105. * @return bool
  106. */
  107. public function validateMenu(array $menu)
  108. {
  109. /** @var \Illuminate\Validation\Validator $validator */
  110. $validator = Validator::make($menu, $this->menuValidationRules);
  111. if ($validator->passes()) {
  112. return true;
  113. }
  114. return false;
  115. }
  116. protected function getMenuModel()
  117. {
  118. return config('admin.database.menu_model');
  119. }
  120. }