request->input('limit', 10); $list = $this->adminRoleModel->paginate($limit,$this->field)->toArray(); return $this->return->success('success',['list' => $list]); } public function add() { $name = $this->request->input('name'); if ($this->adminRoleModel->getInfoByName($name)) throw new AdminException('角色已存在'); Db::beginTransaction(); try { $model = new AdminRole(); $model->name = $name; $model->status = $this->request->input('status', 1); $model->remark = $this->request->input('remark', ''); if (!$model->save()) throw new Exception('添加失败'); //todo 添加角色权限 $menuIdArr = explode(',', $this->request->input('menu_ids')); Db::commit(); } catch (Exception $e) { Db::rollBack(); throw new AdminException($e->getMessage()); } return $this->return->success(); } /** * 修改 * @return array */ public function edit(): array { $id = $this->request->input('id'); if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); Db::beginTransaction(); try { $info->name = $this->request->input('name'); $info->status = $this->request->input('status', 1); $info->remark = $this->request->input('remark', ''); if (!$info->save()) throw new Exception('修改失败'); //todo 删除权限 添加角色权限 Db::commit(); } catch (Exception $e) { Db::rollBack(); throw new AdminException($e->getMessage()); } return $this->return->success(); } /** * 修改状态 * @return array * @throws Exception */ public function changeStatus(): array { $id = $this->request->input('id'); if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); $info->status = $this->request->input('status', 0); if (!$info->save()) throw new Exception('修改失败'); return $this->return->success(); } /** * 详情 * @return array */ public function details(): array { $roleId = $this->request->input('role_id'); $res = $this->adminRoleModel->where('id',$roleId)->first($this->field); if (!$res) throw new AdminException('角色不存在'); return $this->return->success('success',[ 'info' => $res->toArray() ]); } }