diff --git a/ruoyi-ui/src/api/system/role.js b/ruoyi-ui/src/api/system/role.js index 736708c51..3e6fd55b1 100644 --- a/ruoyi-ui/src/api/system/role.js +++ b/ruoyi-ui/src/api/system/role.js @@ -3,7 +3,7 @@ import request from '@/utils/request' // 查询角色列表 export function listRole(query) { return request({ - url: '/system/role/list', + url: '/system/role/page', method: 'get', params: query }) @@ -12,7 +12,7 @@ export function listRole(query) { // 查询角色详细 export function getRole(roleId) { return request({ - url: '/system/role/' + roleId, + url: '/system/role/get?id=' + roleId, method: 'get' }) } @@ -20,7 +20,7 @@ export function getRole(roleId) { // 新增角色 export function addRole(data) { return request({ - url: '/system/role', + url: '/system/role/create', method: 'post', data: data }) @@ -29,8 +29,8 @@ export function addRole(data) { // 修改角色 export function updateRole(data) { return request({ - url: '/system/role', - method: 'put', + url: '/system/role/update', + method: 'post', data: data }) } @@ -45,14 +45,14 @@ export function dataScope(data) { } // 角色状态修改 -export function changeRoleStatus(roleId, status) { +export function changeRoleStatus(id, status) { const data = { - roleId, + id, status } return request({ - url: '/system/role/changeStatus', - method: 'put', + url: '/system/role/update-status', + method: 'post', data: data }) } @@ -60,8 +60,8 @@ export function changeRoleStatus(roleId, status) { // 删除角色 export function delRole(roleId) { return request({ - url: '/system/role/' + roleId, - method: 'delete' + url: '/system/role/delete?id=' + roleId, + method: 'post' }) } @@ -72,4 +72,4 @@ export function exportRole(query) { method: 'get', params: query }) -} \ No newline at end of file +} diff --git a/ruoyi-ui/src/utils/dict.js b/ruoyi-ui/src/utils/dict.js index 81694455f..acbe88c90 100644 --- a/ruoyi-ui/src/utils/dict.js +++ b/ruoyi-ui/src/utils/dict.js @@ -7,7 +7,8 @@ import store from '@/store' export const DICT_TYPE = { SYS_COMMON_STATUS: 'sys_common_status', - SYS_MENU_TYPE: 'sys_menu_type' + SYS_MENU_TYPE: 'sys_menu_type', + SYS_ROLE_TYPE: 'sys_role_type', } /** diff --git a/ruoyi-ui/src/views/system/menu/index.vue b/ruoyi-ui/src/views/system/menu/index.vue index e63ab150f..9a2d4b7ed 100644 --- a/ruoyi-ui/src/views/system/menu/index.vue +++ b/ruoyi-ui/src/views/system/menu/index.vue @@ -14,9 +14,9 @@ diff --git a/ruoyi-ui/src/views/system/role/index.vue b/ruoyi-ui/src/views/system/role/index.vue index d6202b577..601449d18 100644 --- a/ruoyi-ui/src/views/system/role/index.vue +++ b/ruoyi-ui/src/views/system/role/index.vue @@ -1,9 +1,9 @@ @@ -247,6 +241,8 @@ import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role"; import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu"; import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept"; +import {SysCommonStatusEnum, SysMenuTypeEnum} from "@/utils/constants"; +import {DICT_TYPE, getDictDataLabel, getDictDatas} from "@/utils/dict"; export default { name: "Role", @@ -254,12 +250,6 @@ export default { return { // 遮罩层 loading: true, - // 选中数组 - ids: [], - // 非单个禁用 - single: true, - // 非多个禁用 - multiple: true, // 显示搜索条件 showSearch: true, // 总条数 @@ -272,6 +262,9 @@ export default { open: false, // 是否显示弹出层(数据权限) openDataScope: false, + // 是否显示弹出层(菜单权限) + openMenu: false, + // TODO 需要简化下 menuExpand: false, menuNodeAll: false, deptExpand: true, @@ -309,10 +302,10 @@ export default { deptOptions: [], // 查询参数 queryParams: { - pageNum: 1, + pageNo: 1, pageSize: 10, - roleName: undefined, - roleKey: undefined, + name: undefined, + code: undefined, status: undefined }, // 表单参数 @@ -323,23 +316,26 @@ export default { }, // 表单校验 rules: { - roleName: [ + name: [ { required: true, message: "角色名称不能为空", trigger: "blur" } ], - roleKey: [ - { required: true, message: "权限字符不能为空", trigger: "blur" } + code: [ + { required: true, message: "角色标识不能为空", trigger: "blur" } ], - roleSort: [ + sort: [ { required: true, message: "角色顺序不能为空", trigger: "blur" } ] - } + }, + + // 枚举 + CommonStatusEnum: SysCommonStatusEnum, + // 数据字典 + roleTypeDictDatas: getDictDatas(DICT_TYPE.SYS_ROLE_TYPE), + statusDictDatas: getDictDatas(DICT_TYPE.SYS_COMMON_STATUS) }; }, created() { this.getList(); - this.getDicts("sys_normal_disable").then(response => { - this.statusOptions = response.data; - }); }, methods: { /** 查询角色列表 */ @@ -347,8 +343,8 @@ export default { this.loading = true; listRole(this.addDateRange(this.queryParams, this.dateRange)).then( response => { - this.roleList = response.rows; - this.total = response.total; + this.roleList = response.data.list; + this.total = response.data.total; this.loading = false; } ); @@ -384,34 +380,41 @@ export default { return checkedKeys; }, /** 根据角色ID查询菜单树结构 */ - getRoleMenuTreeselect(roleId) { - return roleMenuTreeselect(roleId).then(response => { + getRoleMenuTreeselect(id) { + return roleMenuTreeselect(id).then(response => { this.menuOptions = response.menus; return response; }); }, /** 根据角色ID查询部门树结构 */ - getRoleDeptTreeselect(roleId) { - return roleDeptTreeselect(roleId).then(response => { + getRoleDeptTreeselect(id) { + return roleDeptTreeselect(id).then(response => { this.deptOptions = response.depts; return response; }); }, // 角色状态修改 handleStatusChange(row) { - let text = row.status === "0" ? "启用" : "停用"; - this.$confirm('确认要"' + text + '""' + row.roleName + '"角色吗?', "警告", { + // 此时,row 已经变成目标状态了,所以可以直接提交请求和提示 + let text = row.status === SysCommonStatusEnum.ENABLE ? "启用" : "停用"; + this.$confirm('确认要"' + text + '""' + row.name + '"角色吗?', "警告", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" }).then(function() { - return changeRoleStatus(row.roleId, row.status); + return changeRoleStatus(row.id, row.status); }).then(() => { this.msgSuccess(text + "成功"); }).catch(function() { - row.status = row.status === "0" ? "1" : "0"; + // 异常时,需要将 row.status 状态重置回之前的 + row.status = row.status === SysCommonStatusEnum.ENABLE ? SysCommonStatusEnum.DISABLE + : SysCommonStatusEnum.ENABLE; }); }, + // 角色类型字典翻译 + typeFormat(row, column) { + return getDictDataLabel(DICT_TYPE.SYS_ROLE_TYPE, row.type) + }, // 取消按钮 cancel() { this.open = false; @@ -422,6 +425,11 @@ export default { this.openDataScope = false; this.reset(); }, + // 取消按钮(菜单权限) + cancelMenu() { + this.openMenu = false; + this.reset(); + }, // 表单重置 reset() { if (this.$refs.menu != undefined) { @@ -432,10 +440,10 @@ export default { this.deptExpand = true, this.deptNodeAll = false, this.form = { - roleId: undefined, - roleName: undefined, - roleKey: undefined, - roleSort: 0, + id: undefined, + name: undefined, + code: undefined, + sort: 0, status: "0", ids: [], deptIds: [], @@ -447,7 +455,7 @@ export default { }, /** 搜索按钮操作 */ handleQuery() { - this.queryParams.pageNum = 1; + this.queryParams.pageNo = 1; this.getList(); }, /** 重置按钮操作 */ @@ -456,12 +464,6 @@ export default { this.resetForm("queryForm"); this.handleQuery(); }, - // 多选框选中数据 - handleSelectionChange(selection) { - this.ids = selection.map(item => item.roleId) - this.single = selection.length!=1 - this.multiple = !selection.length - }, // 树权限(展开/折叠) handleCheckedTreeExpand(value, type) { if (type == 'menu') { @@ -495,31 +497,50 @@ export default { /** 新增按钮操作 */ handleAdd() { this.reset(); - this.getMenuTreeselect(); this.open = true; this.title = "添加角色"; }, /** 修改按钮操作 */ handleUpdate(row) { this.reset(); - const roleId = row.roleId || this.ids - const roleMenu = this.getRoleMenuTreeselect(roleId); - getRole(roleId).then(response => { + const id = row.id + getRole(id).then(response => { this.form = response.data; this.open = true; - this.$nextTick(() => { - roleMenu.then(res => { - this.$refs.menu.setCheckedKeys(res.checkedKeys); - }); - }); this.title = "修改角色"; }); }, + /** 分配菜单权限操作 */ + handleMenu(row) { + this.reset(); + const id = row.id + // TODO 优化 + this.getMenuTreeselect(); + const roleMenu = this.getRoleMenuTreeselect(id); + this.$nextTick(() => { + roleMenu.then(res => { + this.$refs.menu.setCheckedKeys(res.checkedKeys); + }); + }); + this.openMenu = true; + + // const roleDeptTreeselect = this.getRoleDeptTreeselect(row.id); + // getRole(row.id).then(response => { + // this.form = response.data; + // this.openDataScope = true; + // this.$nextTick(() => { + // roleDeptTreeselect.then(res => { + // this.$refs.dept.setCheckedKeys(res.checkedKeys); + // }); + // }); + // this.title = "分配数据权限"; + // }); + }, /** 分配数据权限操作 */ handleDataScope(row) { this.reset(); - const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId); - getRole(row.roleId).then(response => { + const roleDeptTreeselect = this.getRoleDeptTreeselect(row.id); + getRole(row.id).then(response => { this.form = response.data; this.openDataScope = true; this.$nextTick(() => { @@ -534,15 +555,17 @@ export default { submitForm: function() { this.$refs["form"].validate(valid => { if (valid) { - if (this.form.roleId != undefined) { - this.form.ids = this.getMenuAllCheckedKeys(); + if (this.form.id !== undefined) { + // TODO 芋艿:需要迁移 + // this.form.ids = this.getMenuAllCheckedKeys(); updateRole(this.form).then(response => { this.msgSuccess("修改成功"); this.open = false; this.getList(); }); } else { - this.form.ids = this.getMenuAllCheckedKeys(); + // TODO 芋艿:需要迁移 + // this.form.ids = this.getMenuAllCheckedKeys(); addRole(this.form).then(response => { this.msgSuccess("新增成功"); this.open = false; @@ -554,7 +577,19 @@ export default { }, /** 提交按钮(数据权限) */ submitDataScope: function() { - if (this.form.roleId != undefined) { + if (this.form.id != undefined) { + this.form.deptIds = this.getDeptAllCheckedKeys(); + dataScope(this.form).then(response => { + this.msgSuccess("修改成功"); + this.openDataScope = false; + this.getList(); + }); + } + }, + /** 提交按钮(菜单权限) */ + submitMenu: function() { + // TODO 需要改动下 + if (this.form.id != undefined) { this.form.deptIds = this.getDeptAllCheckedKeys(); dataScope(this.form).then(response => { this.msgSuccess("修改成功"); @@ -565,13 +600,13 @@ export default { }, /** 删除按钮操作 */ handleDelete(row) { - const roleIds = row.roleId || this.ids; - this.$confirm('是否确认删除角色编号为"' + roleIds + '"的数据项?', "警告", { + const ids = row.id || this.ids; + this.$confirm('是否确认删除角色编号为"' + ids + '"的数据项?', "警告", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" }).then(function() { - return delRole(roleIds); + return delRole(ids); }).then(() => { this.getList(); this.msgSuccess("删除成功"); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java index 748e82ec3..f95ac371b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java @@ -2,16 +2,12 @@ package cn.iocoder.dashboard.modules.system.controller.permission; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.PageResult; -import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO; -import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; -import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleRespVO; -import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.*; import cn.iocoder.dashboard.modules.system.convert.permission.SysRoleConvert; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -82,14 +78,10 @@ public class SysRoleController { @ApiOperation("修改角色状态") @PostMapping("/update-status") - @ApiImplicitParams(value = { - @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1024"), - @ApiImplicitParam(name = "status", value = "状态", required = true, example = "1") - }) // @PreAuthorize("@ss.hasPermi('system:role:edit')") // @Log(title = "角色管理", businessType = BusinessType.UPDATE) - public CommonResult updateRoleStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) { - roleService.updateRoleStatus(id, status); + public CommonResult updateRoleStatus(@Validated @RequestBody SysRoleUpdateStatusReqVO reqVO) { + roleService.updateRoleStatus(reqVO.getId(), reqVO.getStatus()); return success(true); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleBaseVO.java index 8f325065b..4fce82dac 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleBaseVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleBaseVO.java @@ -30,4 +30,7 @@ public class SysRoleBaseVO { @ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "见 RoleTypeEnum 枚举") private Integer type; + @ApiModelProperty(value = "备注", example = "我是一个角色") + private String remark; + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleRespVO.java index 4ae4fae88..7775360f4 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleRespVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleRespVO.java @@ -28,9 +28,6 @@ public class SysRoleRespVO extends SysRoleBaseVO { @ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "参见 RoleTypeEnum 枚举类") private Integer type; - @ApiModelProperty(value = "备注", example = "我是一个角色") - private String remark; - @ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式") private Date createTime; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleUpdateStatusReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleUpdateStatusReqVO.java new file mode 100644 index 000000000..6654c5a39 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleUpdateStatusReqVO.java @@ -0,0 +1,22 @@ +package cn.iocoder.dashboard.modules.system.controller.permission.vo.role; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@ApiModel("角色更新状态 Request VO") +@Data +public class SysRoleUpdateStatusReqVO { + + @ApiModelProperty(value = "用户编号", required = true, example = "1024") + @NotNull(message = "角色编号不能为空") + private Long id; + + @ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举") + @NotNull(message = "状态不能为空") +// @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}") + private Integer status; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java index d1875aafb..7ea607d40 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java @@ -105,15 +105,8 @@ public class SysRoleServiceImpl implements SysRoleService { @Override public void updateRole(SysRoleUpdateReqVO reqVO) { - // 校验更新的角色是否存在 - SysRoleDO role = roleMapper.selectById(reqVO.getId()); - if (roleMapper.selectById(reqVO.getId()) == null) { - throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); - } - // 内置角色,不允许修改 - if (RoleTypeEnum.SYSTEM.getType().equals(role.getType())) { - throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); - } + // 校验是否可以更新 + this.checkUpdateRole(reqVO.getId()); // 校验角色的唯一字段是否重复 checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); // 更新到数据库 @@ -123,15 +116,8 @@ public class SysRoleServiceImpl implements SysRoleService { @Override public void deleteRole(Long id) { - // 校验删除的角色是否存在 - SysRoleDO roleDO = roleMapper.selectById(id); - if (roleMapper.selectById(id) == null) { - throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); - } - // 内置角色,不允许删除 - if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) { - throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE); - } + // 校验是否可以更新 + this.checkUpdateRole(id); // 标记删除 roleMapper.deleteById(id); // 删除相关数据 @@ -152,11 +138,9 @@ public class SysRoleServiceImpl implements SysRoleService { @Override public void updateRoleStatus(Long id, Integer status) { - // 校验修改的角色是否存在 - SysRoleDO roleDO = roleMapper.selectById(id); - if (roleMapper.selectById(id) == null) { - throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); - } + // 校验是否可以更新 + this.checkUpdateRole(id); + // 更新状态 SysRoleDO updateObject = new SysRoleDO(); updateObject.setId(id); updateObject.setStatus(status); @@ -190,4 +174,20 @@ public class SysRoleServiceImpl implements SysRoleService { } } + /** + * 校验角色是否可以被更新 + * + * @param id 角色编号 + */ + private void checkUpdateRole(Long id) { + SysRoleDO roleDO = roleMapper.selectById(id); + if (roleDO == null) { + throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); + } + // 内置角色,不允许删除 + if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) { + throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE); + } + } + }