Menu.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\common\library;
  3. use app\admin\model\MenuRule;
  4. use app\admin\model\UserRule;
  5. use think\db\exception\DbException;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\ModelNotFoundException;
  8. /**
  9. * 后台菜单类
  10. */
  11. class Menu
  12. {
  13. /**
  14. * @param array $menu
  15. * @param int|string $parent 父级规则name或id
  16. * @param string $mode 添加模式(规则重复时):cover=覆盖旧菜单,rename=重命名新菜单,ignore=忽略
  17. * @param string $position 位置:backend=后台,frontend=前台
  18. * @throws DataNotFoundException
  19. * @throws DbException
  20. * @throws ModelNotFoundException
  21. */
  22. public static function create(array $menu, $parent = 0, string $mode = 'cover', string $position = 'backend')
  23. {
  24. $pid = 0;
  25. $model = $position == 'backend' ? new MenuRule() : new UserRule();
  26. $parentRule = $model->where((is_numeric($parent) ? 'id' : 'name'), $parent)->find();
  27. if ($parentRule) {
  28. $pid = $parentRule['id'];
  29. }
  30. foreach ($menu as $item) {
  31. if (!self::requiredAttrCheck($item)) {
  32. continue;
  33. }
  34. // 属性
  35. $item['status'] = '1';
  36. if (!isset($item['pid']) && $pid) {
  37. $item['pid'] = $pid;
  38. }
  39. $oldMenu = $model->where('name', $item['name'])->find();
  40. if ($oldMenu) {
  41. // 存在相关名称的菜单规则
  42. if ($mode == 'cover') {
  43. $oldMenu->save($item);
  44. } elseif ($mode == 'rename') {
  45. $count = $model->where('name', $item['name'])->count();
  46. $item['name'] = $item['name'] . '-conflicting-' . $count;
  47. $item['title'] = $item['title'] . '-conflicting-' . $count;
  48. $oldMenu = $model->create($item);
  49. } elseif ($mode == 'ignore') {
  50. $oldMenu = $menu;
  51. }
  52. } else {
  53. $oldMenu = $model->create($item);
  54. }
  55. if (isset($item['children']) && $item['children']) {
  56. self::create($item['children'], $oldMenu['name'], $mode, $position);
  57. }
  58. }
  59. }
  60. /**
  61. * 删菜单
  62. * @param string|int $id 规则name或id
  63. * @param bool $recursion 是否递归删除子级菜单、是否删除自身,是否删除上级空菜单
  64. * @param string $position 位置:backend=后台,frontend=前台
  65. * @return bool
  66. * @throws DataNotFoundException
  67. * @throws DbException
  68. * @throws ModelNotFoundException
  69. */
  70. public static function delete($id, bool $recursion = false, string $position = 'backend'): bool
  71. {
  72. if (!$id) {
  73. return true;
  74. }
  75. $model = $position == 'backend' ? new MenuRule() : new UserRule();
  76. $menuRule = $model->where((is_numeric($id) ? 'id' : 'name'), $id)->find();
  77. if (!$menuRule) {
  78. return true;
  79. }
  80. $children = $model->where('pid', $menuRule['id'])->select()->toArray();
  81. if ($recursion && $children) {
  82. foreach ($children as $child) {
  83. self::delete($child['id'], true, $position);
  84. }
  85. }
  86. if (!$children || $recursion) {
  87. $menuRule->delete();
  88. self::delete($menuRule->pid, false, $position);
  89. }
  90. return true;
  91. }
  92. /**
  93. * 启用菜单
  94. * @param string|int $id 规则name或id
  95. * @param string $position 位置:backend=后台,frontend=前台
  96. * @return bool
  97. * @throws DataNotFoundException
  98. * @throws DbException
  99. * @throws ModelNotFoundException
  100. */
  101. public static function enable($id, string $position = 'backend'): bool
  102. {
  103. $model = $position == 'backend' ? new MenuRule() : new UserRule();
  104. $menuRule = $model->where((is_numeric($id) ? 'id' : 'name'), $id)->find();
  105. if (!$menuRule) {
  106. return false;
  107. }
  108. $menuRule->status = '1';
  109. $menuRule->save();
  110. return true;
  111. }
  112. /**
  113. * 禁用菜单
  114. * @param string|int $id 规则name或id
  115. * @param string $position 位置:backend=后台,frontend=前台
  116. * @return bool
  117. * @throws DataNotFoundException
  118. * @throws DbException
  119. * @throws ModelNotFoundException
  120. */
  121. public static function disable($id, string $position = 'backend'): bool
  122. {
  123. $model = $position == 'backend' ? new MenuRule() : new UserRule();
  124. $menuRule = $model->where((is_numeric($id) ? 'id' : 'name'), $id)->find();
  125. if (!$menuRule) {
  126. return false;
  127. }
  128. $menuRule->status = '0';
  129. $menuRule->save();
  130. return true;
  131. }
  132. public static function requiredAttrCheck($menu): bool
  133. {
  134. $attrs = ['type', 'title', 'name'];
  135. foreach ($attrs as $attr) {
  136. if (!array_key_exists($attr, $menu)) {
  137. return false;
  138. }
  139. if (!$menu[$attr]) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. }