model = new AdminModel(); $this->authGroupModel = new AuthGroupModel(); $this->authGroupAccessModel = new AuthGroupAccessModel(); $this->storeModel = new StoreModel(); $this->departmentModel = new DepartmentModel(); parent::__construct($app); } /** * @param Request $request * @return \think\response\View * @throws \think\db\exception\DbException */ public function index(Request $request) { $params = $request->param(); $format_params = [ 'group_id' => format_string($params['group_id'] ?? null), 'department_id' => format_string($params['department_id'] ?? null), ]; $list = $this->model->findByPaginate($format_params); $all_groups = $this->authGroupModel->fetchAllGroups(); View::assign([ 'all_department' => recursion($this->departmentModel->findAllDepartment()->toArray(),0), 'list' => $list, 'all_groups' => recursion($all_groups, 0), 'params' => $format_params ]); return view(); } /** * @return \think\response\Json|\think\response\View * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function add() { if ($this->request->isAjax()) { $params = $this->request->param(); $admin = $this->model->doesItExist($params['account']); if($admin) return $this->fail(lang("The account already exists.")); $department = $this->departmentModel->findById($params['department_id']); if(!$department) return $this->fail('部门不存在!'); $res = $this->model->create([ 'account' => $params['account'], 'nickname' => $params['nickname'], 'mobile' => $params['mobile'], 'password' => md5($params['password']), 'department_id' => $params['department_id'], 'store_ids' => isset($params['store_ids']) ? join(',', $params['store_ids']) : $department->store_id, 'is_login_backstage' => $params['is_login_backstage'], ]); $this->authGroupAccessModel->save([ 'admin_id' => $res->id, 'group_id' => $params['group_id'] ]); return $this->ok(true); } $all_groups = $this->authGroupModel->fetchAllGroups(); $all_department = recursion($this->departmentModel->findAllDepartment()->toArray(),0); View::assign([ 'stores' => $this->storeModel->findAllStore()->toArray(), 'all_department' => $all_department, 'all_group' => recursion($all_groups, 0), 'now_group' => $this->authGroupModel->fetchByDepartmentId($all_department[0] ? $all_department[0]['id'] : -1 ), ]); return view(); } /** * @return \think\response\Json|\think\response\View * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit() { $params = $this->request->param(); if(!isset($params['id'])) return $this->fail(lang('ID not exist')); $admin = $this->model->findById($params['id']); if ($this->request->isAjax()) { $admin = $this->model->doesItExist($params['account']); if($admin && $admin->id != $params['id']) return $this->fail(lang("The account already exists.")); $department = $this->departmentModel->findById($params['department_id']); if(!$department) return $this->fail('部门不存在!'); $this->model->where('id',$params['id'])->update([ 'account' => $params['account'], 'nickname' => $params['nickname'], 'mobile' => $params['mobile'], 'password' => isset($params['password']) && $params['password'] != $admin->password ? md5($params['password']) : $admin['password'], 'department_id' => $params['department_id'], 'store_ids' => isset($params['store_ids']) ? join(',', $params['store_ids']) : $department->store_id, 'is_login_backstage' => $params['is_login_backstage'], 'update_time' => time() ]); $relation = $this->authGroupAccessModel->findByAdminId($admin->id); if($relation && $relation->group_id != $params['group_id']) { $relation->where('id', $relation->id)->update([ 'group_id' => $params['group_id'] ]); } return $this->ok(true); } $all_department = recursion($this->departmentModel->findAllDepartment()->toArray(),0); $stores = $this->storeModel->findAllStore(); foreach ($stores as $store) $store->isChecked = in_array($store->id, explode(',', $admin->store_ids ?? '')); View::assign([ 'stores' => $stores, 'all_department' => $all_department, 'admin' => $admin, 'now_group' => $this->authGroupModel->fetchByDepartmentId($admin->department_id ?? -1), ]); return view(); } public function getBindRules($department_id = null) { return $this->ok( $this->authGroupModel ->fetchByDepartmentId($department_id > 0 ? $department_id : -1 ) ); } public function delete(\think\Request $request) { $params = $request->param(); if(!isset($params['ids'])) return $this->fail(lang("Please select the data you want to delete")); $this->model->deleteByIds(explode(',', $params['ids'])); return $this->ok(true); } public function total_data() { $params = $this->request->param(); if(!isset($params['id'])) return $this->fail(lang('ID not exist')); View::assign([ 'stores' => $this->storeModel->findAllStore(), 'all_groups' => recursion($this->authGroupModel->fetchAllGroups(), 0), 'admin' => $this->model->findById($params['id']), ]); return view(); } public function findAdmins(Request $request) { $params = $request->param(); if(!isset($params['text'])) return $this->fail(lang('Data not exist')); return $this->ok($this->model->findAdmins($params['text'])); } }