RoleController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\service\sys_manage\RoleService;
  4. use app\admin\validate\sys_manage\RoleValidate;
  5. use app\common\Tree;
  6. use app\controller\Curd;
  7. use app\model\SysRole;
  8. use support\Request;
  9. use support\Response;
  10. class RoleController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new SysRole();
  15. }
  16. /**
  17. * @Desc 角色列表
  18. * @Author Gorden
  19. * @Date 2024/2/20 16:30
  20. *
  21. * @param Request $request
  22. * @return \support\Response
  23. */
  24. public function roleList(Request $request)
  25. {
  26. return RoleService::roleList($request);
  27. }
  28. public function select(Request $request): Response
  29. {
  30. $keyword = $request->get('keyword');
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $format = 'tree';
  33. $order = $request->get('order', 'desc');
  34. $field = $field ?? 'role_addtimes';
  35. if (!empty($keyword)){
  36. $where['role_name'] = ['like',$keyword];
  37. }
  38. $query = $this->doSelect($where, $field, $order);
  39. return $this->doFormat($query, $format, 1000);
  40. }
  41. protected function formatTree($items): Response
  42. {
  43. $format_items = [];
  44. foreach ($items as $item) {
  45. $format_items[] = [
  46. 'name' => $item->role_name,
  47. 'value' => (string)$item->role_id,
  48. 'id' => $item->id ?? $item->role_id,
  49. 'role_id' => $item->id ?? $item->role_id,
  50. 'pid' => $item->pid ?? $item->role_superior_id,
  51. 'role_status' => $item->role_status,
  52. 'role_permission' => $item->role_permission,
  53. 'role_category' => $item->role_category,
  54. 'role_addtimes' => $item->role_addtimes,
  55. ];
  56. }
  57. $tree = new Tree($format_items);
  58. return json_success('success', $tree->getTree());
  59. }
  60. public function afterQuery($items)
  61. {
  62. foreach ($items as &$item){
  63. if (!empty($item->role_permission) && is_json($item->role_permission)) {
  64. $item->role_permission = json_decode($item->role_permission);
  65. }
  66. }
  67. return $items;
  68. }
  69. /**
  70. * @Desc 角色详情
  71. * @Author Gorden
  72. * @Date 2024/2/20 16:36
  73. *
  74. * @param $id
  75. * @return \support\Response
  76. */
  77. public function roleInfo($id)
  78. {
  79. return RoleService::roleInfo($id);
  80. }
  81. public function save()
  82. {
  83. }
  84. /**
  85. * @Desc 添加角色
  86. * @Author Gorden
  87. * @Date 2024/2/20 15:38
  88. *
  89. * @param Request $request
  90. * @return \support\Response|null
  91. */
  92. public function addRole(Request $request)
  93. {
  94. $validate = new RoleValidate();
  95. if (!$validate->scene('add')->check($request->post())) {
  96. return json_fail($validate->getError());
  97. }
  98. return RoleService::insertRole($request->post());
  99. }
  100. /**
  101. * @Desc 修改角色
  102. * @Author Gorden
  103. * @Date 2024/2/20 16:07
  104. *
  105. * @param $id
  106. * @param Request $request
  107. * @return \support\Response
  108. */
  109. public function updateRole($id, Request $request)
  110. {
  111. $validate = new RoleValidate();
  112. if (!$validate->scene('update')->check($request->post())) {
  113. return json_fail($validate->getError());
  114. }
  115. return RoleService::updateRole(intval($id), $request->post());
  116. }
  117. public function updatePermission($id, Request $request)
  118. {
  119. $validate = new RoleValidate();
  120. if (!$validate->scene('permission')->check($request->post())) {
  121. return json_fail($validate->getError());
  122. }
  123. return RoleService::updatePermission(intval($id), $request->post());
  124. }
  125. /**
  126. * @Desc 删除角色
  127. * @Author Gorden
  128. * @Date 2024/2/20 16:40
  129. *
  130. * @param $id
  131. * @return \support\Response
  132. */
  133. public function delRole(Request $request)
  134. {
  135. return RoleService::delRole($request->post());
  136. }
  137. /**
  138. * @Desc 修改角色状态
  139. * @Author Gorden
  140. * @Date 2024/2/20 16:50
  141. *
  142. * @param $id
  143. * @param Request $request
  144. * @return \support\Response
  145. */
  146. public function updateStatus($id, Request $request)
  147. {
  148. $validate = new RoleValidate();
  149. if (!$validate->scene('update_status')->check(['role_status' => $request->post('role_status')])) {
  150. return json_fail($validate->getError());
  151. }
  152. return RoleService::updateStatus(intval($id), $request->post('role_status'));
  153. }
  154. }