SystemMenuLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\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)
  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. if (empty($data['parent_id']) || $data['parent_id'] == 0) {
  64. $data['level'] = '0';
  65. $data['parent_id'] = 0;
  66. $data['type'] = $data['type'] === 'B' ? 'M' : $data['type'];
  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. if (request()->input('tree', 'false') === 'true') {
  82. $query->field('id, id as value, name as label, parent_id');
  83. }
  84. $query->order('sort', 'desc');
  85. $data = $this->getAll($query);
  86. return Helper::makeTree($data);
  87. }
  88. /**
  89. * 权限菜单
  90. * @return array
  91. */
  92. public function auth(): array
  93. {
  94. $roleLogic = new SystemRoleLogic();
  95. $role_ids = Arr::getArrayColumn($this->adminInfo['roleList'], 'id');
  96. $roles = $roleLogic->getMenuIdsByRoleIds($role_ids);
  97. $ids = $this->filterMenuIds($roles);
  98. $query = $this->model
  99. ->field('id, id as value, name as label, parent_id')
  100. ->where('status', 1)
  101. ->where('id', 'in', $ids)
  102. ->order('sort', 'desc');
  103. $data = $this->getAll($query);
  104. return Helper::makeTree($data);
  105. }
  106. /**
  107. * 获取全部菜单
  108. */
  109. public function getAllMenus(): array
  110. {
  111. $query = $this->search(['status' => 1, 'type' => ['M','I','L']])->order('sort', 'desc');
  112. $data = $this->getAll($query);
  113. return Helper::makeArcoMenus($data);
  114. }
  115. /**
  116. * 获取全部操作code
  117. */
  118. public function getAllCode(): array
  119. {
  120. $query = $this->search(['type' => 'B']);
  121. return $query->column('code');
  122. }
  123. /**
  124. * 根据ids获取权限
  125. * @param $ids
  126. * @return array
  127. */
  128. public function getMenuCode($ids): array
  129. {
  130. return $this->model
  131. ->where('status', 1)
  132. ->where('id', 'in', $ids)
  133. ->column('code');
  134. }
  135. /**
  136. * 根据管理员id获取权限
  137. * @param $id
  138. * @return array
  139. */
  140. public function getAuthByAdminId($id): array
  141. {
  142. $roleIds = SystemUserRole::where('user_id', $id)->column('role_id');
  143. $menuId = SystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
  144. return SystemMenu::distinct(true)
  145. ->where('type', 'B')
  146. ->where('status', 1)
  147. ->where('id', 'in', array_unique($menuId))
  148. ->column('code');
  149. }
  150. /**
  151. * 根据管理员id获取菜单
  152. * @param $id
  153. * @return array
  154. */
  155. public function getRoutersByAdminId($id): array
  156. {
  157. $roleIds = SystemUserRole::where('user_id', $id)->column('role_id');
  158. $menuId = SystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
  159. $data = SystemMenu::distinct(true)
  160. ->where('status', 1)
  161. ->where('type', 'in', ['M','I','L'])
  162. ->where('id', 'in', array_unique($menuId))
  163. ->order('sort', 'desc')
  164. ->select()
  165. ->toArray();
  166. return Helper::makeArcoMenus($data);
  167. }
  168. /**
  169. * 过滤通过角色查询出来的菜单id列表,并去重
  170. * @param array $roleData
  171. * @return array
  172. */
  173. public function filterMenuIds(array &$roleData): array
  174. {
  175. $ids = [];
  176. foreach ($roleData as $val) {
  177. foreach ($val['menus'] as $menu) {
  178. $ids[] = $menu['id'];
  179. }
  180. }
  181. unset($roleData);
  182. return array_unique($ids);
  183. }
  184. }