RoleService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\admin\service\sys_manage;
  3. use app\model\SysRole;
  4. use support\Db;
  5. use support\Request;
  6. class RoleService
  7. {
  8. /**
  9. * @Desc 角色列表
  10. * @Author Gorden
  11. * @Date 2024/2/20 16:29
  12. *
  13. * @param $page
  14. * @param $limit
  15. * @param $keyword
  16. * @return \support\Response
  17. */
  18. public static function roleList(Request $request)
  19. {
  20. $page = intval($request->get('page', 1));
  21. $pageSize = intval($request->get('pageSize', 10));
  22. $keyword = format_string($request->get('keyword', ''));
  23. $roleStatus = $request->get('role_status', '');
  24. $rows = SysRole::select('*')
  25. ->when($keyword != '', function ($query) use ($keyword) {
  26. $query->where('role_name', 'like', '%' . $keyword . '%');
  27. })
  28. ->when($roleStatus != '', function ($query) use ($roleStatus) {
  29. $query->where('role_status', $roleStatus);
  30. })
  31. ->orderBy('role_addtimes', 'DESC')
  32. // ->forPage($page, $pageSize)
  33. ->get()
  34. ->toArray();
  35. $total = SysRole::when($keyword != '', function ($query) use ($keyword) {
  36. $query->where('role_name', 'like', '%' . $keyword . '%');
  37. })->count();
  38. foreach ($rows as &$row) {
  39. if (!empty($row['role_permission']) && is_json($row['role_permission'])) {
  40. $row['role_permission'] = json_decode($row['role_permission']);
  41. }
  42. }
  43. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  44. }
  45. /**
  46. * @Desc 角色详情
  47. * @Author Gorden
  48. * @Date 2024/2/20 16:37
  49. *
  50. * @param $id
  51. * @return \support\Response
  52. */
  53. public static function roleInfo($id)
  54. {
  55. $role = SysRole::find($id);
  56. if (!$role) {
  57. return json_fail('角色不存在');
  58. }
  59. $role = $role->toArray();
  60. return json_success('', $role);
  61. }
  62. /**
  63. * @Desc 添加角色
  64. * @Author Gorden
  65. * @Date 2024/2/20 16:02
  66. *
  67. * @param $params
  68. * @return \support\Response
  69. */
  70. public static function insertRole($params)
  71. {
  72. try {
  73. Db::beginTransaction();
  74. $data = [
  75. 'role_superior_id' => $params['role_superior_id'] ?? 0,
  76. 'role_status' => $params['role_status'],
  77. 'role_category' => $params['role_category'],
  78. 'role_name' => $params['role_name'],
  79. 'role_remark' => $params['role_remark'] ? format_string($params['role_remark']) : null,
  80. 'role_extend_json' => $params['role_extend_json'] ?? null,
  81. 'role_permission' => '[]',
  82. 'role_addtimes' => time()
  83. ];
  84. $roleId = SysRole::insertGetId($data);
  85. $path = '/0/';
  86. if (!empty($params['role_superior_id'])) {
  87. $superior = SysRole::find($params['role_superior_id']);
  88. dump($superior);
  89. if (!isset($superior->role_path)) {
  90. Db::rollBack();
  91. return json_fail("上级角色不存在");
  92. }
  93. $path = $superior->role_path;
  94. }
  95. SysRole::where('role_id', $roleId)->update(['role_path' => $path . $roleId . '/']);
  96. Db::commit();
  97. } catch (\Exception $e) {
  98. Db::rollBack();
  99. dump($e->getMessage());
  100. return json_fail('角色添加失败');
  101. }
  102. return json_success('角色添加成功');
  103. }
  104. /**
  105. * @Desc 修改角色
  106. * @Author Gorden
  107. * @Date 2024/2/20 16:02
  108. *
  109. * @param $id
  110. * @param $params
  111. * @return \support\Response
  112. */
  113. public static function updateRole($id, $params)
  114. {
  115. $role = SysRole::find($id);
  116. $oldPath = $role->role_path;
  117. if (!$role) {
  118. return json_fail('角色不存在');
  119. }
  120. try {
  121. Db::beginTransaction();
  122. $data = [
  123. 'role_superior_id' => $params['role_superior_id'] ?? 0,
  124. 'role_status' => $params['role_status'],
  125. 'role_category' => $params['role_category'],
  126. 'role_name' => $params['role_name'],
  127. 'role_remark' => $params['role_remark'] ? format_string($params['role_remark']) : null,
  128. 'role_extend_json' => $params['role_extend_json'] ?? '{}',
  129. ];
  130. if ($data['role_superior_id'] == 0) {
  131. $data['role_path'] = '/0/' . $id . '/';
  132. } else if ($data['role_superior_id'] != $role->role_superior_id) {
  133. $superior = SysRole::find($data['role_superior_id']);
  134. $data['role_path'] = $superior->role_path . $id . '/';
  135. $child = SysRole::where('role_path', 'like', $oldPath . '%')->get();
  136. dump($child);
  137. foreach ($child as $item) {
  138. $childPath = str_replace($oldPath, $data['role_path'], $item->role_path);
  139. SysRole::where('role_id', $item->role_id)->update(['role_path'=>$childPath]);
  140. }
  141. }
  142. dump($data);
  143. if (!SysRole::where('role_id', $id)->update($data)) {
  144. Db::rollBack();
  145. throw new \Exception('角色修改失败');
  146. }
  147. Db::commit();
  148. } catch (\Exception $e) {
  149. Db::rollBack();
  150. return json_fail($e->getMessage());
  151. }
  152. return json_success('角色修改成功');
  153. }
  154. public static function updatePermission($id, $params)
  155. {
  156. $role = SysRole::find($id);
  157. if (!$role) {
  158. return json_fail('角色不存在');
  159. }
  160. try {
  161. $data = [
  162. 'role_permission' => $params['role_permission'],
  163. ];
  164. SysRole::where('role_id', $id)->update($data);
  165. } catch (\Exception $e) {
  166. return json_fail($e->getMessage());
  167. }
  168. return json_success('角色修改成功');
  169. }
  170. /**
  171. * @Desc 删除角色
  172. * @Author Gorden
  173. * @Date 2024/2/20 16:40
  174. *
  175. * @param $id
  176. * @return \support\Response
  177. */
  178. public static function delRole($params)
  179. {
  180. try {
  181. if (!SysRole::whereIn('role_id', $params['role_id'])->delete()) {
  182. throw new \Exception('角色删除失败');
  183. }
  184. } catch (\Exception $e) {
  185. return json_fail('角色删除失败');
  186. }
  187. return json_success('角色删除成功');
  188. }
  189. /**
  190. * @Desc 修改角色状态
  191. * @Author Gorden
  192. * @Date 2024/2/20 16:50
  193. *
  194. * @param $id
  195. * @param $roleStatus
  196. * @return \support\Response
  197. */
  198. public static function updateStatus($id, $roleStatus)
  199. {
  200. try {
  201. if (!SysRole::where('role_id', $id)->update(['role_status' => $roleStatus])) {
  202. throw new \Exception('角色状态修改失败');
  203. }
  204. } catch (\Exception $e) {
  205. return json_fail('角色状态修改失败');
  206. }
  207. return json_success('角色状态修改成功');
  208. }
  209. /**
  210. * @Desc 检查角色是否存在
  211. * @Author Gorden
  212. * @Date 2024/2/21 13:36
  213. *
  214. * @param $roleId
  215. * @return bool
  216. */
  217. public static function checkRoleExist($roleId)
  218. {
  219. return SysRole::where('role_id', $roleId)->exists();
  220. }
  221. }