authService = new AuthService(); $this->storeModel = new StoreModel(); } 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_groups = $this->authService->authGroupModel->fetchAllGroups(); if ($this->request->isAjax()) { $params = $this->request->param(); $res = $this->authService->authGroupModel->save([ 'pid' => $params['pid'] ?? 1, 'name' => format_string($params['name'] ?? null), 'rules' => format_string($params['rule_ids'] ?? null), "store_ids" => isset($params["store_ids"]) ? join(',', $params['store_ids']) : null, 'describe' => format_string($params['describe'] ?? null), ]); return $this->ok($res); } $stores = $this->storeModel->findAllStore(); View::assign([ "stores" => $stores, "all_groups" => recursion($all_groups, 0) ]); 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_groups = $this->authService->authGroupModel->fetchAllGroups(); $group = $this->authService->authGroupModel->findById($id); if ($this->request->isAjax()) { $res = $this->authService->authGroupModel->where('id', $params['id'])->save([ 'pid' => $params['pid'] ?? $group->pid, 'name' => $params['name'] ?? $group->name, 'rules' => !isset($params['rule_ids']) ? null : $params['rule_ids'], "store_ids" => isset($params["store_ids"]) ? join(',', $params['store_ids']) : null, 'describe' => $params['describe'] ?? $group->describe, 'update_time' => time() ]); return $this->ok($res); } $stores = $this->storeModel->findAllStore(); $store_ids = $group->store_ids ? explode(",",$group->store_ids) : []; foreach ($stores as &$item) $item->isChecked = in_array($item->id, $store_ids); View::assign([ 'group' => $group, "stores" => $stores, "all_groups" => recursion($all_groups, 0) ]); 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->authService->authGroupModel->deleteByIds(explode(',', $params['ids'])); return $this->ok(true); } public function findAllAuthGroups() { $groups = $this->authService->authGroupModel->fetchAllGroups()->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)); } }