authGroupModel = new AuthGroupModel(); $this->departmentModel = new DepartmentModel(); } public function index() { 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() { $all_department = $this->departmentModel->fetchAllDepartment(); $all_group = $this->authGroupModel->fetchAllGroups(); if ($this->request->isAjax()) { $params = $this->request->param(); $db_department = $this->departmentModel->findByName(format_string($params['name'] ?? null)); if ($db_department) return $this->fail('部门已经存在!'); $department = [ 'pid' => $params['pid'] ?? 0, 'name' => format_string($params['name'] ?? null), 'describe' => format_string($params['describe'] ?? null), ]; if($params['pid'] == 0) { $auth_group_ids = array_merge($params['auth_group_ids'] ?? []); $department['auth_group_ids'] = count($auth_group_ids) > 0 ? join(',', $auth_group_ids) : null; } $res = $this->departmentModel->save($department); return $this->ok($res); } View::assign([ "all_department" => recursion($all_department, 0), "all_group" => $all_group ]); 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(); $id = $params['id']; $all_department = $this->departmentModel->fetchAllDepartment(); $all_group = $this->authGroupModel->fetchAllGroups(); $department = $this->departmentModel->findById($id); if (!$department) { return $this->fail(lang("Data not exist")); } if ($this->request->isAjax()) { $res = $this->departmentModel->where('id', $params['id'])->save([ 'pid' => $params['pid'] ?? $department->pid, 'name' => $params['name'] ?? $department->name, "auth_group_ids" => isset($params["auth_group_ids"]) ? join(',', array_merge($params['auth_group_ids'])) : null, 'describe' => $params['describe'] ?? $department->describe, 'update_time' => time() ]); return $this->ok($res); } $group_ids = $department->auth_group_ids ? explode(',',$department->auth_group_ids) : []; foreach ($all_group as &$item) $item->isChecked = in_array($item->id, $group_ids); View::assign([ 'department' => $department, "all_department" => recursion($all_department, 0), "all_group" => $all_group ]); 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->departmentModel->deleteByIds(explode(',', $params['ids'])); return $this->ok(true); } /** * @return \think\response\Json */ public function fetchAllDepartment() { $groups = $this->departmentModel->fetchAllDepartment()->toArray(); $format_groups = array_map(function ($data) { return [ 'id' => $data['id'], 'title' => $data['name'], 'pid' => $data['pid'], 'field' => $data['name'], 'spread' => true, 'checked' => false, 'disabled' => $data['id'] == 1 ]; }, $groups); return $this->ok(recursion($format_groups)); } }