AdminMenuTrait.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Models\Traits;
  3. use Illuminate\Support\Facades\Config;
  4. trait AdminMenuTrait
  5. {
  6. /**
  7. * 与角色的多对多关系
  8. *
  9. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  10. */
  11. public function roles()
  12. {
  13. return $this->belongsToMany(Config::get('admin.role'), Config::get('admin.menu_role_table'),
  14. Config::get('admin.menu_foreign_key'), Config::get('admin.role_foreign_key'));
  15. }
  16. /**
  17. * 与权限的多对多关系
  18. *
  19. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  20. */
  21. public function perms()
  22. {
  23. return $this->belongsToMany(Config::get('admin.permission'), Config::get('admin.permission_menu_table'),
  24. Config::get('admin.menu_foreign_key'), Config::get('admin.permission_foreign_key'));
  25. }
  26. /**
  27. * 把权限转成角色ID,并放到到数组的roleIds
  28. * @param $menu
  29. * @return mixed
  30. */
  31. private static function transRoleIds($menu) {
  32. if (!empty($menu['roles'])) {
  33. $menu['roleIds'] = array_map(function ($item){
  34. return $item['id'];
  35. }, $menu['roles']);
  36. }
  37. else
  38. {
  39. $menu['roleIds'] = [];
  40. }
  41. return $menu;
  42. }
  43. /**
  44. * 把权限转成权限ID,并放到到数组的permIds
  45. * @param $menu 菜单数组
  46. * @return mixed
  47. */
  48. private static function transPermIds($menu) {
  49. if (!empty($menu['perms'])) {
  50. $menu['permIds'] = array_map(function ($item){
  51. return $item['id'];
  52. }, $menu['perms']);
  53. }
  54. else
  55. {
  56. $menu['permIds'] = [];
  57. }
  58. return $menu;
  59. }
  60. /**
  61. * 查找菜单
  62. * @param $id 菜单ID
  63. * @return mixed
  64. */
  65. public static function find($id)
  66. {
  67. return static::with('roles')->find($id);
  68. }
  69. /**
  70. * 查找带权限的菜单
  71. * @param $id 菜单ID
  72. * @return array|mixed
  73. */
  74. public static function findByRoleId($id)
  75. {
  76. $menu = [];
  77. $menuObj= static::with('roles')->find($id);
  78. if (!empty($menuObj))
  79. {
  80. $menu = $menuObj->toArray();
  81. $menu = static::transRoleIds($menu);
  82. }
  83. return $menu;
  84. }
  85. /**
  86. * 转成树结构
  87. * @param array $elements 树数组
  88. * @param int $parentId 上级ID
  89. * @param array $roleIds 权限ID
  90. * @return array
  91. */
  92. public static function toTree(array $elements = [], $parentId = 0, $roleIds = [])
  93. {
  94. $branch = [];
  95. if (empty($elements)) {
  96. $elements = static::with('roles', 'perms')->orderByRaw('`order` = 0,`order`')->get()->toArray();
  97. }
  98. foreach ($elements as $element) {
  99. if ($element['parent_id'] == $parentId) {
  100. $element = static::transRoleIds($element);
  101. $element = static::transPermIds($element);
  102. if (!empty($element['roleIds']) && !empty($roleIds))
  103. {
  104. $_roles = array_intersect($element['roleIds'], $roleIds);
  105. if (empty($_roles)) continue;
  106. }
  107. $children = static::toTree($elements, $element['id'], $roleIds);
  108. if ($children) {
  109. $element['children'] = $children;
  110. }
  111. $branch[] = $element;
  112. }
  113. }
  114. return $branch;
  115. }
  116. /**
  117. * 设计分支排序
  118. *
  119. * @param array $order
  120. *
  121. * @return void
  122. */
  123. protected static function setBranchOrder(array $order)
  124. {
  125. static::$branchOrder = array_flip(array_flatten($order));
  126. static::$branchOrder = array_map(function ($item) {
  127. return ++$item;
  128. }, static::$branchOrder);
  129. }
  130. /**
  131. * 保存树类菜单
  132. * @param array $tree 树结构
  133. * @param int $parentId 上级ID
  134. */
  135. public static function saveTree($tree = [], $parentId = 0)
  136. {
  137. if (empty(static::$branchOrder)) {
  138. static::setBranchOrder($tree);
  139. }
  140. foreach ($tree as $branch) {
  141. $node = static::find($branch['id']);
  142. $node->parent_id = $parentId;
  143. $node->order = static::$branchOrder[$branch['id']];
  144. $node->save();
  145. if (isset($branch['children'])) {
  146. static::saveTree($branch['children'], $branch['id']);
  147. }
  148. }
  149. }
  150. /**
  151. * 保存角色
  152. * @param $roles
  153. */
  154. public function saveRoles($roles)
  155. {
  156. if (!empty($roles)) {
  157. $this->roles()->sync($roles);
  158. } else {
  159. $this->roles()->detach();
  160. }
  161. }
  162. /**
  163. * 删除菜单
  164. * @param array $options
  165. * @return mixed
  166. */
  167. public function delete(array $options = [])
  168. {
  169. $children = $this->where('parent_id', $this->id)->get();
  170. $this->where('parent_id', $this->id)->delete();
  171. if ($children) {
  172. foreach ($children as $child) {
  173. $child->roles()->detach();
  174. $child->perms()->detach();
  175. }
  176. }
  177. $this->roles()->detach();
  178. $this->perms()->detach();
  179. return parent::delete($options);
  180. }
  181. /**
  182. * 得到用户菜单
  183. * @param $user
  184. * @return array
  185. */
  186. public static function getUserMenu($user) {
  187. $isAdminHasAllRoles = true;
  188. $isAdmin = $user->hasRole('admin');
  189. $roles = $user->cachedRoles();
  190. $roleIds = [0];
  191. foreach ($roles as $role)
  192. {
  193. $roleIds[] = $role->id;
  194. }
  195. if ($isAdminHasAllRoles && $isAdmin) $roleIds = [];
  196. $menus = static::toTree([], 0, $roleIds);
  197. return $menus;
  198. }
  199. }