request->input('limit', 10); $list = $this->adminRoleModel->paginate($limit,$this->field)->toArray(); return $this->return->success('success',['list' => $list]); } /** * 添加角色 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function add(): array { $name = $this->request->input('role_name'); if ($this->adminRoleModel->getInfoByName($name)) throw new AdminException('角色已存在'); Db::beginTransaction(); try { $model = new AdminRole(); $model->name = $name; $model->status = $this->request->input('role_status', 1); $model->remark = $this->request->input('role_remark', ''); if (!$model->save()) throw new Exception('添加失败-角色错误'); //添加角色权限 if (!empty($this->request->input('menu_ids'))) { $menuIdArr = explode(',', $this->request->input('menu_ids')); $insertArr = []; foreach ($menuIdArr as $menuId) { $insertArr[] = [ 'role_id' => $model->id, 'menu_id' => $menuId, ]; } if (!(new AdminRoleMenu)->insert($insertArr)) throw new Exception('添加失败-权限组错误'); } Db::commit(); $this->roleCache->getRoleCache($model->id); } catch (Exception $e) { Db::rollBack(); throw new AdminException($e->getMessage()); } return $this->return->success(); } /** * 修改角色 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function edit(): array { $id = (int)$this->request->input('role_id'); if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); $name = $this->request->input('role_name'); $oldInfo = $this->adminRoleModel->getInfoByName($name); if ($oldInfo->id != $id) throw new AdminException('角色已存在'); Db::beginTransaction(); try { $info->name = $name; $info->status = $this->request->input('role_status', 1); $info->remark = $this->request->input('role_remark', ''); if (!$info->save()) throw new Exception('修改失败-角色错误'); //删除权限 添加角色权限 if (!empty($this->request->input('menu_ids'))) { //todo 判断数据一致 是否修改 $this->adminRoleMenuModel->where('role_id', $info->id)->delete(); $menuIdArr = explode(',', $this->request->input('menu_ids')); $insertArr = []; foreach ($menuIdArr as $menuId) { $insertArr[] = [ 'role_id' => $info->id, 'menu_id' => $menuId, ]; } if (!(new AdminRoleMenu)->insert($insertArr)) throw new Exception('修改失败-权限组错误'); } Db::commit(); $this->roleCache->delRoleCache($info->id); $this->roleCache->getRoleCache($info->id); } catch (Exception $e) { Db::rollBack(); throw new AdminException($e->getMessage()); } return $this->return->success(); } /** * 修改状态 * @return array * @throws Exception */ public function changeStatus(): array { $id = (int)$this->request->input('role_id'); if ($id == AuthCode::SUPERADMIN) throw new AdminException('超级管理员不可关闭'); if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); $info->status = $this->request->input('role_status', 0); if (!$info->save()) throw new AdminException('修改失败'); return $this->return->success(); } /** * 详情角色 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws \RedisException */ public function details(): array { $roleId = $this->request->input('role_id'); $res = $this->adminRoleModel->where('id',$roleId)->first($this->field); if (!$res) throw new AdminException('角色不存在'); $data = $this->roleCache->getRoleCache((int)$roleId); $menuList = $this->menuCache->getMenu(); return $this->return->success('success',[ 'info' => $res->toArray(), 'role_arr' => $data['role_arr'], 'role_list' => $data['role_list'], ]); } }