You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

131 lines
3.4 KiB

  1. <?php
  2. namespace app\common\library;
  3. use app\admin\model\AuthRule;
  4. use fast\Tree;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. class Menu
  8. {
  9. /**
  10. * 创建菜单
  11. * @param array $menu
  12. * @param mixed $parent 父类的name或pid
  13. */
  14. public static function create($menu, $parent = 0)
  15. {
  16. if (!is_numeric($parent)) {
  17. $parentRule = AuthRule::getByName($parent);
  18. $pid = $parentRule ? $parentRule['id'] : 0;
  19. } else {
  20. $pid = $parent;
  21. }
  22. $allow = array_flip(['file', 'name', 'title', 'icon', 'condition', 'remark', 'ismenu']);
  23. foreach ($menu as $k => $v) {
  24. $hasChild = isset($v['sublist']) && $v['sublist'] ? true : false;
  25. $data = array_intersect_key($v, $allow);
  26. $data['ismenu'] = isset($data['ismenu']) ? $data['ismenu'] : ($hasChild ? 1 : 0);
  27. $data['icon'] = isset($data['icon']) ? $data['icon'] : ($hasChild ? 'fa fa-list' : 'fa fa-circle-o');
  28. $data['pid'] = $pid;
  29. $data['status'] = 'normal';
  30. try {
  31. $menu = AuthRule::create($data);
  32. if ($hasChild) {
  33. self::create($v['sublist'], $menu->id);
  34. }
  35. } catch (PDOException $e) {
  36. throw new Exception($e->getMessage());
  37. }
  38. }
  39. }
  40. /**
  41. * 删除菜单
  42. * @param string $name 规则name
  43. * @return boolean
  44. */
  45. public static function delete($name)
  46. {
  47. $ids = self::getAuthRuleIdsByName($name);
  48. if (!$ids) {
  49. return false;
  50. }
  51. AuthRule::destroy($ids);
  52. return true;
  53. }
  54. /**
  55. * 启用菜单
  56. * @param string $name
  57. * @return boolean
  58. */
  59. public static function enable($name)
  60. {
  61. $ids = self::getAuthRuleIdsByName($name);
  62. if (!$ids) {
  63. return false;
  64. }
  65. AuthRule::where('id', 'in', $ids)->update(['status' => 'normal']);
  66. return true;
  67. }
  68. /**
  69. * 禁用菜单
  70. * @param string $name
  71. * @return boolean
  72. */
  73. public static function disable($name)
  74. {
  75. $ids = self::getAuthRuleIdsByName($name);
  76. if (!$ids) {
  77. return false;
  78. }
  79. AuthRule::where('id', 'in', $ids)->update(['status' => 'hidden']);
  80. return true;
  81. }
  82. /**
  83. * 导出指定名称的菜单规则
  84. * @param string $name
  85. * @return array
  86. */
  87. public static function export($name)
  88. {
  89. $ids = self::getAuthRuleIdsByName($name);
  90. if (!$ids) {
  91. return [];
  92. }
  93. $menuList = [];
  94. $menu = AuthRule::getByName($name);
  95. if ($menu) {
  96. $ruleList = collection(AuthRule::where('id', 'in', $ids)->select())->toArray();
  97. $menuList = Tree::instance()->init($ruleList)->getTreeArray($menu['id']);
  98. }
  99. return $menuList;
  100. }
  101. /**
  102. * 根据名称获取规则IDS
  103. * @param string $name
  104. * @return array
  105. */
  106. public static function getAuthRuleIdsByName($name)
  107. {
  108. $ids = [];
  109. $menu = AuthRule::getByName($name);
  110. if ($menu) {
  111. // 必须将结果集转换为数组
  112. $ruleList = collection(AuthRule::order('weigh', 'desc')->field('id,pid,name')->select())->toArray();
  113. // 构造菜单数据
  114. $ids = Tree::instance()->init($ruleList)->getChildrenIds($menu['id'], true);
  115. }
  116. return $ids;
  117. }
  118. }