service = new AuthService(); parent::__construct($app); } public function index() { View::assign([ 'list' => $this->service->authGroupModel->findByPaginate($this->request->param()), ]); return view(); } public function add() { if ($this->request->isAjax()) { $params = $this->request->param(); $res = $this->service->authGroupModel->save([ 'name' => format_string($params['name'] ?? null), 'rules' => format_string($params['rule_ids'] ?? null), 'describe' => format_string($params['describe'] ?? null), ]); return $this->ok($res); } return view(); } public function edit() { $params = $this->request->param(); $id = $params['id']; $group = $this->service->authGroupModel->findById($id); if ($this->request->isAjax()) { $res = $this->service->authGroupModel->where('id', $params['id'])->save([ 'name' => format_string($params['name'] ?? null), 'rules' => format_string($params['rule_ids'] ?? null), 'describe' => format_string($params['describe'] ?? null), 'update_time' => time() ]); return $this->ok($res); } View::assign([ 'group' => $group, ]); return view(); } public function findAuthRuleByAdminId($admin_id = null) { $all_rules = $this->service->authRuleModel->findByIds(["*"]); $format_rules = array_map(function ($data) { return [ 'id' => $data['id'], 'title' => $data['name'], 'pid' => $data['pid'], 'field' => $data['name'], 'spread' => false, 'checked' => false ]; }, $all_rules); if(!isset($admin_id)) return $this->ok(recursion($format_rules)); $rules = $this->service->loadRuleByAdminId($admin_id); $in_rule_ids = array_map(function ($data) { return $data['id']; }, $rules); foreach ($format_rules as &$item) $item['checked'] = in_array($item['id'], $in_rule_ids); return $this->ok(recursion($format_rules)); } /** * @param null $group_id * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function findAuthRuleByGroupId($group_id = null) { $all_rules = $this->service->authRuleModel->findByIds(["*"]); $format_rules = array_map(function ($data) { return [ 'id' => $data['id'], 'title' => $data['name'], 'pid' => $data['pid'], 'field' => $data['name'], 'spread' => false, 'checked' => false ]; }, $all_rules); if(!isset($group_id)) return $this->ok(recursion($format_rules)); $rules = $this->service->loadRuleByGroupId($group_id); $in_rule_ids = array_map(function ($data) { return $data['id']; }, $rules); foreach ($format_rules as &$item) $item['checked'] = in_array($item['id'], $in_rule_ids); return $this->ok(recursion($format_rules)); } }