RoleController.php 4.4 KB

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