get(); $role = $this->service->getList($param); return successTrans("success.data",$role); } catch (\Throwable $throwable) { echo $throwable->getFile()."\n"; echo $throwable->getLine()."\n"; return error($throwable->getMessage()); } } /** * 显示创建资源表单页. */ #[Route(path: "create",methods: "get")] public function create(Request $request): Response { try { $menus = $this->service->getmenus([]); return successTrans("success.data",$menus); } catch (\Throwable $throwable) { echo $throwable->getFile()."\n"; echo $throwable->getLine()."\n"; return error($throwable->getMessage()); } } /** * 更新/保存 * @param Request $request * @return Response */ #[Route(path: "save[/{id}]",methods: "post")] public function save(Request $request,$id = 0): Response { try { $data = $this->_valid([ "role_name.require" => '请输入身份名称', "status.default" => '1', "rules.default" => '1', ],"post"); if (!is_array($data)) return error($data); if (!is_array($data['rules']) || !count($data['rules'])) return errorTrans('请选择最少一个权限'); $data['rules'] = implode(',', $data['rules']); if ($id) { if (!$this->service->update($id, $data)) return errorTrans('修改失败!'); } else { if (!$this->service->save($data)) return errorTrans('添加身份失败!'); } return successTrans("success.data"); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 编辑. */ #[Route(path: "edit/{id}",methods: "get")] public function edit(Request $request,$id): Response { try { $role = $this->service->getOne(['id'=>$id]); if (!$role) { return $this->fail('修改的角色不存在'); } $menus = $this->service->getmenus([]); return successTrans("success.data",['role' => $role->toArray(), 'menus' => $menus]); } catch (\Throwable $throwable) { echo $throwable->getFile()."\n"; echo $throwable->getLine()."\n"; return error($throwable->getMessage()); } } /** * 删除. */ #[Route(path: "del/{id}",methods: "delete")] public function del(Request $request,$id): Response { try { if (!$this->service->delete($id)) return errorTrans('删除失败,请稍候再试!'); else { return successTrans('删除成功!'); } } catch (\Throwable $throwable) { echo $throwable->getFile()."\n"; echo $throwable->getLine()."\n"; return error($throwable->getMessage()); } } #[Route(path: "set_status/{id}/{status}",methods: "get")] public function set_status($id,$status): Response { try { $state = $this->service->update($id,['status'=>$status]); if (!$state) return errorTrans("empty.error"); return successTrans($status?'显示成功':'关闭成功'); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } }