SystemRoleLogic.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\cache\UserAuthCache;
  9. use plugin\saiadmin\app\model\system\SystemRole;
  10. use plugin\saiadmin\basic\BaseLogic;
  11. use plugin\saiadmin\exception\ApiException;
  12. use plugin\saiadmin\utils\Helper;
  13. use think\db\Query;
  14. use think\facade\Db;
  15. /**
  16. * 角色逻辑层
  17. */
  18. class SystemRoleLogic extends BaseLogic
  19. {
  20. /**
  21. * 构造函数
  22. */
  23. public function __construct()
  24. {
  25. $this->model = new SystemRole();
  26. }
  27. /**
  28. * 添加数据
  29. */
  30. public function add($data): mixed
  31. {
  32. $data = $this->handleData($data);
  33. $this->model->save($data);
  34. return $this->model->getKey();
  35. }
  36. /**
  37. * 修改数据
  38. */
  39. public function edit($id, $data): mixed
  40. {
  41. $oldLevel = $data['level'] . "," . $id;
  42. $data = $this->handleData($data);
  43. if ($data['parent_id'] == $id) {
  44. throw new ApiException('不能设置父级为自身');
  45. }
  46. if (in_array($id, explode(',', $data['level']))) {
  47. throw new ApiException('不能设置父级为下级角色');
  48. }
  49. $query = $this->model->where('id', $id);
  50. $query->auth([
  51. 'id' => $this->adminInfo['id'],
  52. 'roles' => $this->adminInfo['roleList']
  53. ]);
  54. $model = $query->findOrEmpty();
  55. if ($model->isEmpty() || in_array($id, array_column($this->adminInfo['roleList'], 'id'))) {
  56. throw new ApiException('没有权限操作该数据');
  57. }
  58. $newLevel = $data['level'].",".$id;
  59. $roleIds = SystemRole::whereRaw('FIND_IN_SET("'.$id.'", level) > 0')->column('id');
  60. SystemRole::whereIn('id', $roleIds)->exp('level', "REPLACE(level, '$oldLevel', '$newLevel')")->update();
  61. return $model->save($data);
  62. }
  63. /**
  64. * 删除数据
  65. */
  66. public function destroy($ids)
  67. {
  68. // 判断是否所属角色下的角色
  69. if ($this->adminInfo['id'] > 1) {
  70. $roleList = $this->adminInfo['roleList'];
  71. $roleIds = [];
  72. foreach ($roleList as $item) {
  73. $temp = SystemRole::whereRaw('FIND_IN_SET("'.$item['id'].'", level) > 0')->column('id');
  74. $roleIds = array_merge($roleIds, $temp);
  75. }
  76. if (count(array_diff($ids, $roleIds)) > 0) {
  77. throw new ApiException('删除角色不在当前角色下');
  78. }
  79. }
  80. $num = SystemRole::where('parent_id', 'in', $ids)->count();
  81. if ($num > 0) {
  82. throw new ApiException('该角色下存在子角色,请先删除子角色');
  83. } else {
  84. return $this->model->destroy($ids);
  85. }
  86. }
  87. /**
  88. * 数据处理
  89. */
  90. protected function handleData($data)
  91. {
  92. if ($this->adminInfo['id'] > 1) {
  93. // 判断parent_id是否允许使用
  94. $ids = [];
  95. foreach ($this->adminInfo['roleList'] as $item) {
  96. $ids[] = $item['id'];
  97. $temp = SystemRole::whereRaw('FIND_IN_SET("'.$item['id'].'", level) > 0')->column('id');
  98. $ids = array_merge($ids, $temp);
  99. }
  100. if (!in_array($data['parent_id'], array_unique($ids))) {
  101. throw new ApiException('父级角色不在当前角色下');
  102. }
  103. }
  104. if (empty($data['parent_id'])) {
  105. $data['level'] = '0';
  106. $data['parent_id'] = 0;
  107. } else {
  108. $parentMenu = SystemRole::findOrEmpty($data['parent_id']);
  109. $data['level'] = $parentMenu['level'] . ',' . $parentMenu['id'];
  110. }
  111. return $data;
  112. }
  113. /**
  114. * 数据树形化
  115. * @param array $where
  116. * @return array
  117. */
  118. public function tree(array $where = []): array
  119. {
  120. $query = $this->search($where);
  121. if (request()->input('tree', 'false') === 'true') {
  122. $query->field('id, id as value, name as label, parent_id');
  123. }
  124. $query->auth([
  125. 'id' => $this->adminInfo['id'],
  126. 'roles' => $this->adminInfo['roleList']
  127. ]);
  128. if ($this->adminInfo['id'] === 1) {
  129. $disabled = [1];
  130. } else {
  131. $disabled = array_column($this->adminInfo['roleList'], 'id');
  132. }
  133. $query->order('sort', 'desc');
  134. $data = $this->getAll($query);
  135. if (request()->input('filter', 'true') === 'true') {
  136. if (!empty($disabled)) {
  137. foreach ($data as &$item) {
  138. if (in_array($item['id'], $disabled)) {
  139. $item['disabled'] = true;
  140. } else {
  141. $item['disabled'] = false;
  142. }
  143. }
  144. }
  145. }
  146. return Helper::makeTree($data);
  147. }
  148. /**
  149. * 可操作角色
  150. * @param array $where
  151. * @return array
  152. */
  153. public function accessRole(array $where = []): array
  154. {
  155. $query = $this->search($where);
  156. $query->field('id, id as value, name as label, parent_id');
  157. $query->auth([
  158. 'id' => $this->adminInfo['id'],
  159. 'roles' => $this->adminInfo['roleList']
  160. ]);
  161. if ($this->adminInfo['id'] === 1) {
  162. $disabled = [1];
  163. } else {
  164. $disabled = array_column($this->adminInfo['roleList'], 'id');
  165. }
  166. $query->order('sort', 'desc');
  167. $data = $this->getAll($query);
  168. if (!empty($disabled)) {
  169. foreach ($data as &$item) {
  170. if (in_array($item['id'], $disabled)) {
  171. $item['disabled'] = true;
  172. } else {
  173. $item['disabled'] = false;
  174. }
  175. }
  176. }
  177. return Helper::makeTree($data);
  178. }
  179. /**
  180. * 根据角色数组获取菜单
  181. * @param $ids
  182. * @return array
  183. */
  184. public function getMenuIdsByRoleIds($ids) : array
  185. {
  186. if (empty($ids)) return [];
  187. return $this->model->where('id', 'in', $ids)->with(['menus' => function(Query $query) {
  188. $query->where('status', 1)->order('sort', 'desc');
  189. }])->select()->toArray();
  190. }
  191. /**
  192. * 根据角色获取菜单
  193. * @param $id
  194. * @return array
  195. */
  196. public function getMenuByRole($id): array
  197. {
  198. $role = $this->model->findOrEmpty($id);
  199. $menus = $role->menus ?: [];
  200. return [
  201. 'id' => $id,
  202. 'menus' => $menus
  203. ];
  204. }
  205. /**
  206. * 保存菜单权限
  207. * @param $id
  208. * @param $menu_ids
  209. * @return mixed
  210. */
  211. public function saveMenuPermission($id, $menu_ids): mixed
  212. {
  213. return $this->transaction(function () use ($id, $menu_ids) {
  214. $role = $this->model->findOrEmpty($id);
  215. if ($role) {
  216. $role->menus()->detach();
  217. $data = array_map(function($menu_id) use ($id) {
  218. return ['menu_id' => $menu_id, 'role_id' => $id];
  219. }, $menu_ids);
  220. Db::name('sa_system_role_menu')->limit(100)->insertAll($data);
  221. }
  222. (new UserAuthCache($this->adminInfo['id']))->clearAuthCache();
  223. return true;
  224. });
  225. }
  226. }