service = $service; $this->request->filter(['addslashes', 'trim']); $this->createParams = [ ['role_name', ''], ['status', 0], ['checked_menus', []] ]; $this->saveDeal = $this->updateDeal = function (&$data) { if (!$data['role_name']) throw new ValidateException('请输入身份名称'); if (!is_array($data['checked_menus']) || !count($data['checked_menus'])) throw new ValidateException('请选择最少一个权限'); $data['rules'] = implode(',', $data['checked_menus']); }; $this->searchable = [ ['status', ''], ['role_name', ''], ]; } public function index() { $where = $this->request->getMore($this->searchable); $where['type'] = 0; $where['level'] = $this->adminInfo['level'] + 1; return $this->success($this->service->getRoleList($where)); } public function read($id) { $info = $this->service->get($id); if (!$info) return $this->error('数据不存在'); /** @var SystemMenusServices $services */ $services = app()->make(SystemMenusServices::class); $info['menus'] = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0); return $this->success('ok', $info->toArray()); } public function save() { $data = $this->request->postMore($this->createParams); $data['level'] = $this->adminInfo['level'] + 1; $res = $this->service->create($data); if ($res) return $this->success('添加成功'); return $this->error('添加失败'); } /** * 修改状态 * @param $id * @param $status * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function setStatus($id, $status) { if (!$id) { return $this->error('缺少参数'); } $role = $this->service->get($id); if (!$role) { return $this->error('没有查到此身份'); } $role->status = $status; if ($role->save()) { CacheService::clear(); return $this->success('修改成功'); } else { return $this->error('修改失败'); } } }