UserMenuService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Services;
  3. use App\Model\Menu;
  4. use App\Model\UserMenu;
  5. use App\Model\UserGroup;
  6. use App\Model\UserUserMenu;
  7. use Illuminate\Support\Facades\Log;
  8. class UserMenuService
  9. {
  10. public static function list($adminGroupId)
  11. {
  12. $data = UserGroup::findRole($adminGroupId);
  13. if (!$data) {
  14. return [];
  15. }
  16. if ($data["role"] == "all") {
  17. $list = UserMenu::getMenuAll(true);
  18. } else {
  19. $list = UserMenu::getUserMenu(json_decode($data["role"], true));
  20. }
  21. return self::listData($list);
  22. }
  23. public static function groupMenus($adminGroupId)
  24. {
  25. $data = UserGroup::findRole($adminGroupId);
  26. if (!$data) {
  27. return [];
  28. }
  29. if ($data['role'] == 'all') {
  30. $menuIds = [];
  31. } else {
  32. $menuIds = json_decode($data["role"], true);
  33. }
  34. return UserMenu::getList($menuIds);
  35. }
  36. public static function groupMenuTree($adminGroupId)
  37. {
  38. $data = UserGroup::findRole($adminGroupId);
  39. if (!$data) {
  40. return [];
  41. }
  42. if ($data['role'] == 'all') {
  43. $menuIds = [];
  44. } else {
  45. $menuIds = json_decode($data["role"], true);
  46. }
  47. $list = UserMenu::getList($menuIds);
  48. if(empty($list)) {
  49. return [];
  50. }
  51. return self::menuTreeFrontData($list, 0, true);
  52. }
  53. static function menuTreeFrontData( $menus, $pid = 0, $notMenuHidden = false, $deep = 3, $level = 0){
  54. $tree = [];
  55. $array = array_values($menus);
  56. foreach ($array as $key => $val) {
  57. if($deep == $level) {
  58. break;
  59. }
  60. if( $val['parent_id'] == $pid ) {
  61. if($notMenuHidden && $val['type'] != 1){
  62. continue;
  63. }
  64. if($level == 0){
  65. $newVal = [
  66. 'path'=> $val['path'],
  67. 'component'=> 'Layout',
  68. 'redirect'=> $val['redirect'],
  69. 'name'=> $val['name'],
  70. 'alwaysShow'=> $val['always_show']==1 ?? true,
  71. 'meta'=> ['title'=> $val['title'], 'icon'=> $val['icon']]
  72. ];
  73. } else {
  74. $newVal = [
  75. 'path'=> $val['path'],
  76. 'name'=> $val['name'],
  77. 'component'=> $val['component'],
  78. 'hidden'=> $val['hidden']==1 ?? true,
  79. 'meta'=> [
  80. 'title'=> $val['title'],
  81. 'icon'=> $val['icon'],
  82. 'keepAlive'=> $val['keep_alive'] ?? 1
  83. ]
  84. ];
  85. }
  86. if($val['parent_id'] == 0) {
  87. if(empty($newVal['name']) || $val['path'] == '/' || $val['path'] == $val['redirect']){
  88. unset($newVal['name']);
  89. }
  90. $newVal['children'] = [
  91. [
  92. 'path'=> $val['path'] != $val['redirect'] ? $val['redirect'] : $val['path'],
  93. 'name'=> $val['name'],
  94. 'component'=> $val['component'],
  95. 'hidden'=> $val['hidden']==1 ?? true,
  96. 'meta'=> [
  97. 'title'=> $val['title'],
  98. 'icon'=> $val['icon'],
  99. 'keepAlive'=> $val['keep_alive'] ?? 1
  100. ]
  101. ]
  102. ];
  103. }
  104. $children = self::menuTreeFrontData($array, $val['id'], $notMenuHidden, $deep, $level+1);
  105. if (!empty($children)) {
  106. $newVal['children'] = $children;
  107. }
  108. $tree[] = $newVal;
  109. }
  110. }
  111. return $tree;
  112. }
  113. static function menuTreeData( $menus, $pid = 0, $notMenuHidden = false, $deep = 3, $level = 0){
  114. $tree = [];
  115. $array = array_values($menus);
  116. foreach ($array as $key => $val) {
  117. if($deep == $level) {
  118. break;
  119. }
  120. if( $val['parent_id'] == $pid ) {
  121. if($notMenuHidden && $val['type'] != 1){
  122. continue;
  123. }
  124. $children = self::menuTreeData($array, $val['id'], $notMenuHidden, $deep, $level+1);
  125. $val['children'] = $children;
  126. $tree[] = $val;
  127. }
  128. }
  129. return $tree;
  130. }
  131. // 权限列表
  132. public static function listData($list)
  133. {
  134. $menu = Menu::getMenu(array_unique(array_column($list, "menu_id")));
  135. $menuData = ToolsService::arrayColumns($menu, "icon,name", 'id');
  136. $list = ToolsService::arrayColumns($list, "id,name,url,is_show,menu_id");
  137. foreach ($list as $k => $v) {
  138. $menuId = $v["menu_id"];
  139. unset($v["menu_id"]);
  140. $menuData[$menuId]["child"][] = $v;
  141. }
  142. return array_values($menuData);
  143. }
  144. // 全部权限返回
  145. public static function rbacList()
  146. {
  147. $list = UserUserMenu::getMenuAll(false);
  148. return self::listData($list);
  149. }
  150. // 全部权限返回
  151. public static function menuList()
  152. {
  153. // 这个条件,暂时先这样,后面确定一下
  154. $where = [
  155. ['hidden', '=', 0]
  156. ];
  157. $list = UserMenu::getListByWhere($where, ['id', 'parent_id', 'title', 'type']);
  158. if(empty($list)) {
  159. return [];
  160. }
  161. return self::menuTreeData($list, 0, false);
  162. }
  163. }