AdminController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace plugin\admin\app\controller;
  3. use plugin\admin\app\common\Auth;
  4. use plugin\admin\app\model\Admin;
  5. use plugin\admin\app\model\AdminRole;
  6. use plugin\admin\app\model\User;
  7. use support\exception\BusinessException;
  8. use support\Request;
  9. use support\Response;
  10. use Throwable;
  11. /**
  12. * 管理员列表
  13. */
  14. class AdminController extends Crud
  15. {
  16. /**
  17. * 不需要鉴权的方法
  18. * @var array
  19. */
  20. protected $noNeedAuth = ['select'];
  21. /**
  22. * @var Admin
  23. */
  24. protected $model = null;
  25. /**
  26. * 开启auth数据限制
  27. * @var string
  28. */
  29. protected $dataLimit = 'auth';
  30. /**
  31. * 以id为数据限制字段
  32. * @var string
  33. */
  34. protected $dataLimitField = 'id';
  35. /**
  36. * 构造函数
  37. * @return void
  38. */
  39. public function __construct()
  40. {
  41. $this->model = new Admin;
  42. }
  43. /**
  44. * 浏览
  45. * @return Response
  46. * @throws Throwable
  47. */
  48. public function index(): Response
  49. {
  50. return raw_view('admin/index');
  51. }
  52. /**
  53. * 查询
  54. * @param Request $request
  55. * @return Response
  56. * @throws BusinessException
  57. */
  58. public function select(Request $request): Response
  59. {
  60. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  61. $query = $this->doSelect($where, $field, $order);
  62. if ($format === 'select') {
  63. return $this->formatSelect($query->get());
  64. }
  65. $paginator = $query->paginate($limit);
  66. $items = $paginator->items();
  67. $admin_ids = array_column($items, 'id');
  68. $roles = AdminRole::whereIn('admin_id', $admin_ids)->get();
  69. $roles_map = [];
  70. foreach ($roles as $role) {
  71. $roles_map[$role['admin_id']][] = $role['role_id'];
  72. }
  73. $login_admin_id = admin_id();
  74. foreach ($items as $index => $item) {
  75. $admin_id = $item['id'];
  76. $items[$index]['roles'] = isset($roles_map[$admin_id]) ? implode(',', $roles_map[$admin_id]) : '';
  77. $items[$index]['show_toolbar'] = $admin_id != $login_admin_id;
  78. }
  79. return json(['code' => 0, 'msg' => 'ok', 'count' => $paginator->total(), 'data' => $items]);
  80. }
  81. /**
  82. * 格式化下拉列表
  83. * @param $items
  84. * @return Response
  85. */
  86. protected function formatSelect($items): Response
  87. {
  88. $formatted_items = [];
  89. foreach ($items as $item) {
  90. $formatted_items[] = [
  91. 'name' => $item->nickname,
  92. 'value' => $item->id
  93. ];
  94. }
  95. return $this->json(0, 'ok', $formatted_items);
  96. }
  97. /**
  98. * 插入
  99. * @param Request $request
  100. * @return Response
  101. * @throws BusinessException|Throwable
  102. */
  103. public function insert(Request $request): Response
  104. {
  105. if ($request->method() === 'POST') {
  106. $data = $this->insertInput($request);
  107. unset($data['id']);
  108. $admin_id = $this->doInsert($data);
  109. $role_ids = $request->post('roles');
  110. $role_ids = $role_ids ? explode(',', $role_ids) : [];
  111. if (!$role_ids) {
  112. return $this->json(1, '至少选择一个角色组');
  113. }
  114. if (!Auth::isSuperAdmin() && array_diff($role_ids, Auth::getScopeRoleIds())) {
  115. return $this->json(1, '角色超出权限范围');
  116. }
  117. AdminRole::where('admin_id', $admin_id)->delete();
  118. foreach ($role_ids as $id) {
  119. $admin_role = new AdminRole;
  120. $admin_role->admin_id = $admin_id;
  121. $admin_role->role_id = $id;
  122. $admin_role->save();
  123. }
  124. return $this->json(0, 'ok', ['id' => $admin_id]);
  125. }
  126. return raw_view('admin/insert');
  127. }
  128. /**
  129. * 删除
  130. * @param Request $request
  131. * @return Response
  132. */
  133. public function delete(Request $request): Response
  134. {
  135. $primary_key = $this->model->getKeyName();
  136. $ids = $request->post($primary_key);
  137. if (!$ids) {
  138. return $this->json(0);
  139. }
  140. $ids = (array)$ids;
  141. if (in_array(admin_id(), $ids)) {
  142. return $this->json(1, '不能删除自己');
  143. }
  144. if (!Auth::isSuperAdmin() && array_diff($ids, Auth::getScopeAdminIds())) {
  145. return $this->json(1, '无数据权限');
  146. }
  147. $this->model->whereIn($primary_key, $ids)->each(function (Admin $admin) {
  148. $admin->delete();
  149. });
  150. AdminRole::whereIn('admin_id', $ids)->each(function (AdminRole $admin_role) {
  151. $admin_role->delete();
  152. });
  153. return $this->json(0);
  154. }
  155. /**
  156. * 关联团队长
  157. * @param Request $request
  158. * @return Response
  159. * @throws BusinessException|Throwable
  160. */
  161. public function relevance(Request $request): Response
  162. {
  163. if ($request->method() === 'POST') {
  164. if (!empty($request->post('team_id'))) {
  165. Admin::query()->where('id', $request->post('id'))->update(['team_id' => $request->post('team_id')]);
  166. } else {
  167. Admin::query()->where('id', $request->post('id'))->update(['team_id' => 0]);
  168. }
  169. return $this->json(0);
  170. }
  171. return raw_view('admin/relevance');
  172. }
  173. /**
  174. * 更新
  175. * @param Request $request
  176. * @return Response
  177. * @throws BusinessException|Throwable
  178. */
  179. public function update(Request $request): Response
  180. {
  181. if ($request->method() === 'POST') {
  182. [$id, $data] = $this->updateInput($request);
  183. $admin_id = $request->post('id');
  184. if (!$admin_id) {
  185. return $this->json(1, '缺少参数');
  186. }
  187. // 不能禁用自己
  188. if (isset($data['status']) && $data['status'] == 1 && $id == admin_id()) {
  189. return $this->json(1, '不能禁用自己');
  190. }
  191. // 需要更新角色
  192. $role_ids = $request->post('roles');
  193. if ($role_ids !== null) {
  194. if (!$role_ids) {
  195. return $this->json(1, '至少选择一个角色组');
  196. }
  197. $role_ids = explode(',', $role_ids);
  198. $is_supper_admin = Auth::isSuperAdmin();
  199. $exist_role_ids = AdminRole::where('admin_id', $admin_id)->pluck('role_id')->toArray();
  200. $scope_role_ids = Auth::getScopeRoleIds();
  201. if (!$is_supper_admin && !array_intersect($exist_role_ids, $scope_role_ids)) {
  202. return $this->json(1, '无权限更改该记录');
  203. }
  204. if (!$is_supper_admin && array_diff($role_ids, $scope_role_ids)) {
  205. return $this->json(1, '角色超出权限范围');
  206. }
  207. // 删除账户角色
  208. $delete_ids = array_diff($exist_role_ids, $role_ids);
  209. AdminRole::whereIn('role_id', $delete_ids)->where('admin_id', $admin_id)->delete();
  210. // 添加账户角色
  211. $add_ids = array_diff($role_ids, $exist_role_ids);
  212. foreach ($add_ids as $role_id) {
  213. $admin_role = new AdminRole;
  214. $admin_role->admin_id = $admin_id;
  215. $admin_role->role_id = $role_id;
  216. $admin_role->save();
  217. }
  218. }
  219. $this->doUpdate($id, $data);
  220. return $this->json(0);
  221. }
  222. return raw_view('admin/update');
  223. }
  224. /**
  225. * 获取团队长信息
  226. * @param Request $request
  227. * @return Response
  228. * @throws BusinessException|Throwable
  229. */
  230. public function team(Request $request): Response
  231. {
  232. $data = User::query()->where('team_id', '>', 0)
  233. ->where('is_team', 1)
  234. ->selectRaw('id as value,name,mobile')->get();
  235. $arr = [['value'=>0,'name'=>'无']];
  236. foreach ($data as $k => $v) {
  237. $arr[] = [
  238. 'value' => $v['value'],
  239. 'name' => $v['name'] . '---' . $v['mobile'],
  240. ];
  241. }
  242. return json(['code' => 0, 'data' => $arr, 'msg' => 'ok']);
  243. }
  244. }