model = new AdminModel(); $this->authGroupModel = new AuthGroupModel(); $this->authGroupAccessModel = new AuthGroupAccessModel(); parent::__construct($app); } /** * @param Request $request * @return \think\response\View * @throws \think\db\exception\DbException */ public function index(Request $request) { $list = $this->model->findByPaginate(); View::assign([ 'list' => $list, ]); 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.")); $res = $this->model->create([ 'account' => $params['account'], 'nickname' => $params['nickname'], 'password' => md5($params['password']), ]); $this->authGroupAccessModel->save([ 'admin_id' => $res->id, 'group_id' => $params['group_id'] ]); return $this->ok(true); } $groups = $this->authGroupModel->fetchAllGroups(); View::assign([ 'groups' => $groups ]); 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')); if ($this->request->isAjax()) { $admin = $this->model->doesItExist($params['account']); if($admin && $admin->id != $params['id']) return $this->fail(lang("The account already exists.")); $this->model->where('id',$params['id'])->update([ 'account' => $params['account'], 'nickname' => $params['nickname'], 'password' => $params['password'] == $admin->password ? $admin->password : md5($params['password']), '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); } $groups = $this->authGroupModel->fetchAllGroups(); View::assign([ 'groups' => $groups, 'admin' => $this->model->findById($params['id']), ]); return view(); } 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 test() { echo "test"; } }