initialize(); } protected function initialize() { $this->adminId = $this->request->adminId(); $this->adminInfo = $this->request->adminInfo(); $this->auth = $this->adminInfo['rule'] ?? []; } public function index() { if (!$this->service) { throw new AdminException('接口不存在'); } $where = $this->request->getMore($this->searchable, false, $this->searchDeal); list($page, $limit) = $this->service->getPageValue(); $list = $this->service->getList($where, '*', $page, $limit); $count = $this->service->getCount($where); return $this->success(compact('list', 'count')); } public function read($id) { if (!$this->service) { throw new AdminException('接口不存在'); } $info = $this->service->get($id); if (!$info) return $this->error('数据不存在'); return $this->success('ok', $info->toArray()); } public function save() { if (!$this->service) { throw new AdminException('接口不存在'); } $data = $this->request->postMore($this->createParams, false, $this->saveDeal); if ($this->validate) { $this->validate($data, $this->validate, 'save'); } $res = $this->service->create($data); if ($res) return $this->success('添加成功'); return $this->error('添加失败'); } public function validate($data, $validate, $s = '') { $scene = $validate->allScene() ?? []; if (in_array($s, $scene)) $res = $validate->scene($s)->check($data); else $res = $validate->check($data); if (!$res) throw new AdminException($validate->getError()); } public function update($id) { if (!$this->service) { throw new AdminException('接口不存在'); } $data = $this->service->get($id); if (!$data) return $this->error('数据不存在'); $data = $this->request->postMore($this->createParams, false, $this->updateDeal, $id); if ($this->validate) { $this->validate($data, $this->validate, 'update'); } $res = $this->service->update($id, $data); if ($res) return $this->success('修改成功'); return $this->error('修改失败'); } public function delete($id) { if (!$this->service) { throw new AdminException('接口不存在'); } $data = $this->service->get($id); if (!$data) return $this->error('数据不存在'); $res = $this->service->delete($id); if ($res) return $this->success('已删除'); return $this->error('删除失败'); } public function export() { if (!$this->service) { throw new AdminException('接口不存在'); } $where = $this->request->getMore($this->searchable, false, $this->searchDeal); $export_type = sys_config('export_type', 1); return $this->success($this->service->export($where, $export_type)); } }