SystemMenuLogic.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\app\model\system\SystemMenu;
  9. use plugin\saiadmin\app\model\system\SystemRoleMenu;
  10. use plugin\saiadmin\app\model\system\SystemUserRole;
  11. use plugin\saiadmin\basic\think\BaseLogic;
  12. use plugin\saiadmin\exception\ApiException;
  13. use plugin\saiadmin\utils\Arr;
  14. use plugin\saiadmin\utils\Helper;
  15. /**
  16. * 菜单逻辑层
  17. */
  18. class SystemMenuLogic extends BaseLogic
  19. {
  20. /**
  21. * 构造函数
  22. */
  23. public function __construct()
  24. {
  25. $this->model = new SystemMenu();
  26. }
  27. /**
  28. * 数据添加
  29. */
  30. public function add($data): mixed
  31. {
  32. $data = $this->handleData($data);
  33. return $this->model->save($data);
  34. }
  35. /**
  36. * 数据修改
  37. */
  38. public function edit($id, $data): mixed
  39. {
  40. $data = $this->handleData($data);
  41. if ($data['parent_id'] == $id) {
  42. throw new ApiException('不能设置父级为自身');
  43. }
  44. return $this->model->update($data, ['id' => $id]);
  45. }
  46. /**
  47. * 数据删除
  48. */
  49. public function destroy($ids): bool
  50. {
  51. $num = $this->model->where('parent_id', 'in', $ids)->count();
  52. if ($num > 0) {
  53. throw new ApiException('该菜单下存在子菜单,请先删除子菜单');
  54. } else {
  55. return $this->model->destroy($ids);
  56. }
  57. }
  58. /**
  59. * 数据处理
  60. */
  61. protected function handleData($data)
  62. {
  63. // 处理上级菜单
  64. if (empty($data['parent_id']) || $data['parent_id'] == 0) {
  65. $data['level'] = '0';
  66. $data['parent_id'] = 0;
  67. } else {
  68. $parentMenu = $this->model->findOrEmpty($data['parent_id']);
  69. $data['level'] = $parentMenu['level'] . $parentMenu['id'] . ',';
  70. }
  71. return $data;
  72. }
  73. /**
  74. * 数据树形化
  75. * @param $where
  76. * @return array
  77. */
  78. public function tree($where = []): array
  79. {
  80. $query = $this->search($where);
  81. $request = request();
  82. if ($request && $request->input('tree', 'false') === 'true') {
  83. $query->field('id, id as value, name as label, parent_id, type');
  84. }
  85. $query->order('sort', 'desc');
  86. $data = $this->getAll($query);
  87. return Helper::makeTree($data);
  88. }
  89. /**
  90. * 权限菜单
  91. * @return array
  92. */
  93. public function auth(): array
  94. {
  95. $roleLogic = new SystemRoleLogic();
  96. $role_ids = Arr::getArrayColumn($this->adminInfo['roleList'], 'id');
  97. $roles = $roleLogic->getMenuIdsByRoleIds($role_ids);
  98. $ids = $this->filterMenuIds($roles);
  99. $query = $this->model
  100. ->field('id, id as value, name as label, parent_id, type')
  101. ->where('status', 1)
  102. ->where('id', 'in', $ids)
  103. ->order('sort', 'desc');
  104. $data = $this->getAll($query);
  105. return Helper::makeTree($data);
  106. }
  107. /**
  108. * 获取全部菜单
  109. */
  110. public function getAllMenus(): array
  111. {
  112. $query = $this->search(['status' => 1, 'type' => [1, 2, 4]])->order('sort', 'desc');
  113. $data = $this->getAll($query);
  114. return Helper::makeArtdMenus($data);
  115. }
  116. /**
  117. * 获取全部权限
  118. * @return array
  119. */
  120. public function getAllAuth(): array
  121. {
  122. return SystemMenu::where('type', 3)
  123. ->where('status', 1)
  124. ->column('slug');
  125. }
  126. /**
  127. * 根据角色获取权限
  128. * @param $roleIds
  129. * @return array
  130. */
  131. public function getAuthByRole($roleIds): array
  132. {
  133. $menuId = SystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
  134. return SystemMenu::distinct(true)
  135. ->where('type', 3)
  136. ->where('status', 1)
  137. ->where('id', 'in', array_unique($menuId))
  138. ->column('slug');
  139. }
  140. /**
  141. * 根据角色获取菜单
  142. * @param $roleIds
  143. * @return array
  144. */
  145. public function getMenuByRole($roleIds): array
  146. {
  147. $menuId = SystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
  148. $data = SystemMenu::distinct(true)
  149. ->where('status', 1)
  150. ->where('type', 'in', [1, 2, 4])
  151. ->where('id', 'in', array_unique($menuId))
  152. ->order('sort', 'desc')
  153. ->select()
  154. ->toArray();
  155. return Helper::makeArtdMenus($data);
  156. }
  157. /**
  158. * 过滤通过角色查询出来的菜单id列表,并去重
  159. * @param array $roleData
  160. * @return array
  161. */
  162. public function filterMenuIds(array &$roleData): array
  163. {
  164. $ids = [];
  165. foreach ($roleData as $val) {
  166. foreach ($val['menus'] as $menu) {
  167. $ids[] = $menu['id'];
  168. }
  169. }
  170. unset($roleData);
  171. return array_unique($ids);
  172. }
  173. }