| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace plugin\admin\app\controller;
- use plugin\admin\app\common\Auth;
- use plugin\admin\app\model\Admin;
- use plugin\admin\app\model\AdminRole;
- use plugin\admin\app\model\User;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- use Throwable;
- /**
- * 管理员列表
- */
- class AdminController extends Crud
- {
- /**
- * 不需要鉴权的方法
- * @var array
- */
- protected $noNeedAuth = ['select'];
- /**
- * @var Admin
- */
- protected $model = null;
- /**
- * 开启auth数据限制
- * @var string
- */
- protected $dataLimit = 'auth';
- /**
- * 以id为数据限制字段
- * @var string
- */
- protected $dataLimitField = 'id';
- /**
- * 构造函数
- * @return void
- */
- public function __construct()
- {
- $this->model = new Admin;
- }
- /**
- * 浏览
- * @return Response
- * @throws Throwable
- */
- public function index(): Response
- {
- return raw_view('admin/index');
- }
- /**
- * 查询
- * @param Request $request
- * @return Response
- * @throws BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $query = $this->doSelect($where, $field, $order);
- if ($format === 'select') {
- return $this->formatSelect($query->get());
- }
- $paginator = $query->paginate($limit);
- $items = $paginator->items();
- $admin_ids = array_column($items, 'id');
- $roles = AdminRole::whereIn('admin_id', $admin_ids)->get();
- $roles_map = [];
- foreach ($roles as $role) {
- $roles_map[$role['admin_id']][] = $role['role_id'];
- }
- $login_admin_id = admin_id();
- foreach ($items as $index => $item) {
- $admin_id = $item['id'];
- $items[$index]['roles'] = isset($roles_map[$admin_id]) ? implode(',', $roles_map[$admin_id]) : '';
- $items[$index]['show_toolbar'] = $admin_id != $login_admin_id;
- }
- return json(['code' => 0, 'msg' => 'ok', 'count' => $paginator->total(), 'data' => $items]);
- }
- /**
- * 格式化下拉列表
- * @param $items
- * @return Response
- */
- protected function formatSelect($items): Response
- {
- $formatted_items = [];
- foreach ($items as $item) {
- $formatted_items[] = [
- 'name' => $item->nickname,
- 'value' => $item->id
- ];
- }
- return $this->json(0, 'ok', $formatted_items);
- }
- /**
- * 插入
- * @param Request $request
- * @return Response
- * @throws BusinessException|Throwable
- */
- public function insert(Request $request): Response
- {
- if ($request->method() === 'POST') {
- $data = $this->insertInput($request);
- unset($data['id']);
- $admin_id = $this->doInsert($data);
- $role_ids = $request->post('roles');
- $role_ids = $role_ids ? explode(',', $role_ids) : [];
- if (!$role_ids) {
- return $this->json(1, '至少选择一个角色组');
- }
- if (!Auth::isSuperAdmin() && array_diff($role_ids, Auth::getScopeRoleIds())) {
- return $this->json(1, '角色超出权限范围');
- }
- AdminRole::where('admin_id', $admin_id)->delete();
- foreach ($role_ids as $id) {
- $admin_role = new AdminRole;
- $admin_role->admin_id = $admin_id;
- $admin_role->role_id = $id;
- $admin_role->save();
- }
- return $this->json(0, 'ok', ['id' => $admin_id]);
- }
- return raw_view('admin/insert');
- }
- /**
- * 删除
- * @param Request $request
- * @return Response
- */
- public function delete(Request $request): Response
- {
- $primary_key = $this->model->getKeyName();
- $ids = $request->post($primary_key);
- if (!$ids) {
- return $this->json(0);
- }
- $ids = (array)$ids;
- if (in_array(admin_id(), $ids)) {
- return $this->json(1, '不能删除自己');
- }
- if (!Auth::isSuperAdmin() && array_diff($ids, Auth::getScopeAdminIds())) {
- return $this->json(1, '无数据权限');
- }
- $this->model->whereIn($primary_key, $ids)->each(function (Admin $admin) {
- $admin->delete();
- });
- AdminRole::whereIn('admin_id', $ids)->each(function (AdminRole $admin_role) {
- $admin_role->delete();
- });
- return $this->json(0);
- }
- /**
- * 关联团队长
- * @param Request $request
- * @return Response
- * @throws BusinessException|Throwable
- */
- public function relevance(Request $request): Response
- {
- if ($request->method() === 'POST') {
- if (!empty($request->post('team_id'))) {
- Admin::query()->where('id', $request->post('id'))->update(['team_id' => $request->post('team_id')]);
- } else {
- Admin::query()->where('id', $request->post('id'))->update(['team_id' => 0]);
- }
- return $this->json(0);
- }
- return raw_view('admin/relevance');
- }
- /**
- * 更新
- * @param Request $request
- * @return Response
- * @throws BusinessException|Throwable
- */
- public function update(Request $request): Response
- {
- if ($request->method() === 'POST') {
- [$id, $data] = $this->updateInput($request);
- $admin_id = $request->post('id');
- if (!$admin_id) {
- return $this->json(1, '缺少参数');
- }
- // 不能禁用自己
- if (isset($data['status']) && $data['status'] == 1 && $id == admin_id()) {
- return $this->json(1, '不能禁用自己');
- }
- // 需要更新角色
- $role_ids = $request->post('roles');
- if ($role_ids !== null) {
- if (!$role_ids) {
- return $this->json(1, '至少选择一个角色组');
- }
- $role_ids = explode(',', $role_ids);
- $is_supper_admin = Auth::isSuperAdmin();
- $exist_role_ids = AdminRole::where('admin_id', $admin_id)->pluck('role_id')->toArray();
- $scope_role_ids = Auth::getScopeRoleIds();
- if (!$is_supper_admin && !array_intersect($exist_role_ids, $scope_role_ids)) {
- return $this->json(1, '无权限更改该记录');
- }
- if (!$is_supper_admin && array_diff($role_ids, $scope_role_ids)) {
- return $this->json(1, '角色超出权限范围');
- }
- // 删除账户角色
- $delete_ids = array_diff($exist_role_ids, $role_ids);
- AdminRole::whereIn('role_id', $delete_ids)->where('admin_id', $admin_id)->delete();
- // 添加账户角色
- $add_ids = array_diff($role_ids, $exist_role_ids);
- foreach ($add_ids as $role_id) {
- $admin_role = new AdminRole;
- $admin_role->admin_id = $admin_id;
- $admin_role->role_id = $role_id;
- $admin_role->save();
- }
- }
- $this->doUpdate($id, $data);
- return $this->json(0);
- }
- return raw_view('admin/update');
- }
- /**
- * 获取团队长信息
- * @param Request $request
- * @return Response
- * @throws BusinessException|Throwable
- */
- public function team(Request $request): Response
- {
- $data = User::query()->where('team_id', '>', 0)
- ->where('is_team', 1)
- ->selectRaw('id as value,name,mobile')->get();
- $arr = [['value'=>0,'name'=>'无']];
- foreach ($data as $k => $v) {
- $arr[] = [
- 'value' => $v['value'],
- 'name' => $v['name'] . '---' . $v['mobile'],
- ];
- }
- return json(['code' => 0, 'data' => $arr, 'msg' => 'ok']);
- }
- }
|