1. 完成 Role 简单的 CRUD 的迁移

This commit is contained in:
YunaiV 2021-01-09 13:32:27 +08:00
parent 9e59a6c1ae
commit ad1d1978d2
9 changed files with 229 additions and 179 deletions

View File

@ -3,7 +3,7 @@ import request from '@/utils/request'
// 查询角色列表 // 查询角色列表
export function listRole(query) { export function listRole(query) {
return request({ return request({
url: '/system/role/list', url: '/system/role/page',
method: 'get', method: 'get',
params: query params: query
}) })
@ -12,7 +12,7 @@ export function listRole(query) {
// 查询角色详细 // 查询角色详细
export function getRole(roleId) { export function getRole(roleId) {
return request({ return request({
url: '/system/role/' + roleId, url: '/system/role/get?id=' + roleId,
method: 'get' method: 'get'
}) })
} }
@ -20,7 +20,7 @@ export function getRole(roleId) {
// 新增角色 // 新增角色
export function addRole(data) { export function addRole(data) {
return request({ return request({
url: '/system/role', url: '/system/role/create',
method: 'post', method: 'post',
data: data data: data
}) })
@ -29,8 +29,8 @@ export function addRole(data) {
// 修改角色 // 修改角色
export function updateRole(data) { export function updateRole(data) {
return request({ return request({
url: '/system/role', url: '/system/role/update',
method: 'put', method: 'post',
data: data data: data
}) })
} }
@ -45,14 +45,14 @@ export function dataScope(data) {
} }
// 角色状态修改 // 角色状态修改
export function changeRoleStatus(roleId, status) { export function changeRoleStatus(id, status) {
const data = { const data = {
roleId, id,
status status
} }
return request({ return request({
url: '/system/role/changeStatus', url: '/system/role/update-status',
method: 'put', method: 'post',
data: data data: data
}) })
} }
@ -60,8 +60,8 @@ export function changeRoleStatus(roleId, status) {
// 删除角色 // 删除角色
export function delRole(roleId) { export function delRole(roleId) {
return request({ return request({
url: '/system/role/' + roleId, url: '/system/role/delete?id=' + roleId,
method: 'delete' method: 'post'
}) })
} }

View File

@ -7,7 +7,8 @@ import store from '@/store'
export const DICT_TYPE = { export const DICT_TYPE = {
SYS_COMMON_STATUS: 'sys_common_status', SYS_COMMON_STATUS: 'sys_common_status',
SYS_MENU_TYPE: 'sys_menu_type' SYS_MENU_TYPE: 'sys_menu_type',
SYS_ROLE_TYPE: 'sys_role_type',
} }
/** /**

View File

@ -14,9 +14,9 @@
<el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small"> <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small">
<el-option <el-option
v-for="dict in statusDictDatas" v-for="dict in statusDictDatas"
:key="dict.value" :key="parseInt(dict.value)"
:label="dict.label" :label="dict.label"
:value="dict.value" :value="parseInt(dict.value)"
/> />
</el-select> </el-select>
</el-form-item> </el-form-item>

View File

@ -1,9 +1,9 @@
<template> <template>
<div class="app-container"> <div class="app-container">
<el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true"> <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
<el-form-item label="角色名称" prop="roleName"> <el-form-item label="角色名称" prop="name">
<el-input <el-input
v-model="queryParams.roleName" v-model="queryParams.name"
placeholder="请输入角色名称" placeholder="请输入角色名称"
clearable clearable
size="small" size="small"
@ -11,10 +11,10 @@
@keyup.enter.native="handleQuery" @keyup.enter.native="handleQuery"
/> />
</el-form-item> </el-form-item>
<el-form-item label="权限字符" prop="roleKey"> <el-form-item label="角色标识" prop="code">
<el-input <el-input
v-model="queryParams.roleKey" v-model="queryParams.code"
placeholder="请输入权限字符" placeholder="请输入角色标识"
clearable clearable
size="small" size="small"
style="width: 240px" style="width: 240px"
@ -30,10 +30,10 @@
style="width: 240px" style="width: 240px"
> >
<el-option <el-option
v-for="dict in statusOptions" v-for="dict in statusDictDatas"
:key="dict.dictValue" :key="parseInt(dict.value)"
:label="dict.dictLabel" :label="dict.label"
:value="dict.dictValue" :value="parseInt(dict.value)"
/> />
</el-select> </el-select>
</el-form-item> </el-form-item>
@ -65,26 +65,6 @@
v-hasPermi="['system:role:add']" v-hasPermi="['system:role:add']"
>新增</el-button> >新增</el-button>
</el-col> </el-col>
<el-col :span="1.5">
<el-button
type="success"
icon="el-icon-edit"
size="mini"
:disabled="single"
@click="handleUpdate"
v-hasPermi="['system:role:edit']"
>修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="danger"
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['system:role:remove']"
>删除</el-button>
</el-col>
<el-col :span="1.5"> <el-col :span="1.5">
<el-button <el-button
type="warning" type="warning"
@ -97,18 +77,18 @@
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row> </el-row>
<el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange"> <el-table v-loading="loading" :data="roleList">
<el-table-column type="selection" width="55" align="center" /> <el-table-column label="角色编号" prop="id" width="120" />
<el-table-column label="角色编号" prop="roleId" width="120" /> <el-table-column label="角色名称" prop="name" :show-overflow-tooltip="true" width="150" />
<el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" /> <el-table-column label="角色标识" prop="code" :show-overflow-tooltip="true" width="150" />
<el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" /> <el-table-column label="角色类型" prop="type" :formatter="typeFormat" width="80"></el-table-column>
<el-table-column label="显示顺序" prop="roleSort" width="100" /> <el-table-column label="显示顺序" prop="sort" width="100" />
<el-table-column label="状态" align="center" width="100"> <el-table-column label="状态" align="center" width="100">
<template slot-scope="scope"> <template slot-scope="scope">
<el-switch <el-switch
v-model="scope.row.status" v-model="scope.row.status"
active-value="0" :active-value="0"
inactive-value="1" :inactive-value="1"
@change="handleStatusChange(scope.row)" @change="handleStatusChange(scope.row)"
></el-switch> ></el-switch>
</template> </template>
@ -127,6 +107,13 @@
@click="handleUpdate(scope.row)" @click="handleUpdate(scope.row)"
v-hasPermi="['system:role:edit']" v-hasPermi="['system:role:edit']"
>修改</el-button> >修改</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-circle-check"
@click="handleMenu(scope.row)"
v-hasPermi="['system:role:edit']"
>菜单权限</el-button>
<el-button <el-button
size="mini" size="mini"
type="text" type="text"
@ -156,38 +143,14 @@
<!-- 添加或修改角色配置对话框 --> <!-- 添加或修改角色配置对话框 -->
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body> <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="80px"> <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="角色名称" prop="roleName"> <el-form-item label="角色名称" prop="name">
<el-input v-model="form.roleName" placeholder="请输入角色名称" /> <el-input v-model="form.name" placeholder="请输入角色名称" />
</el-form-item> </el-form-item>
<el-form-item label="权限字符" prop="roleKey"> <el-form-item label="角色标识" prop="code">
<el-input v-model="form.roleKey" placeholder="请输入权限字符" /> <el-input v-model="form.code" placeholder="请输入角色标识" />
</el-form-item> </el-form-item>
<el-form-item label="角色顺序" prop="roleSort"> <el-form-item label="角色顺序" prop="sort">
<el-input-number v-model="form.roleSort" controls-position="right" :min="0" /> <el-input-number v-model="form.sort" controls-position="right" :min="0" />
</el-form-item>
<el-form-item label="状态">
<el-radio-group v-model="form.status">
<el-radio
v-for="dict in statusOptions"
:key="dict.dictValue"
:label="dict.dictValue"
>{{dict.dictLabel}}</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="菜单权限">
<el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
<el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
<el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
<el-tree
class="tree-border"
:data="menuOptions"
show-checkbox
ref="menu"
node-key="id"
:check-strictly="!form.menuCheckStrictly"
empty-text="加载中,请稍后"
:props="defaultProps"
></el-tree>
</el-form-item> </el-form-item>
<el-form-item label="备注"> <el-form-item label="备注">
<el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input> <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
@ -199,14 +162,14 @@
</div> </div>
</el-dialog> </el-dialog>
<!-- 分配角色数据权限对话框 --> <!-- 分配角色数据权限对话框 -->
<el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body> <el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body>
<el-form :model="form" label-width="80px"> <el-form :model="form" label-width="80px">
<el-form-item label="角色名称"> <el-form-item label="角色名称">
<el-input v-model="form.roleName" :disabled="true" /> <el-input v-model="form.name" :disabled="true" />
</el-form-item> </el-form-item>
<el-form-item label="权限字符"> <el-form-item label="角色标识">
<el-input v-model="form.roleKey" :disabled="true" /> <el-input v-model="form.code" :disabled="true" />
</el-form-item> </el-form-item>
<el-form-item label="权限范围"> <el-form-item label="权限范围">
<el-select v-model="form.dataScope"> <el-select v-model="form.dataScope">
@ -240,6 +203,37 @@
<el-button @click="cancelDataScope"> </el-button> <el-button @click="cancelDataScope"> </el-button>
</div> </div>
</el-dialog> </el-dialog>
<!-- 分配角色的菜单权限对话框 -->
<el-dialog :title="title" :visible.sync="openMenu" width="500px" append-to-body>
<el-form :model="form" label-width="80px">
<el-form-item label="角色名称">
<el-input v-model="form.name" :disabled="true" />
</el-form-item>
<el-form-item label="角色标识">
<el-input v-model="form.code" :disabled="true" />
</el-form-item>
<el-form-item label="菜单权限">
<el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
<el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
<el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
<el-tree
class="tree-border"
:data="menuOptions"
show-checkbox
ref="menu"
node-key="id"
:check-strictly="!form.menuCheckStrictly"
empty-text="加载中,请稍后"
:props="defaultProps"
></el-tree>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitMenu"> </el-button>
<el-button @click="cancelMenu"> </el-button>
</div>
</el-dialog>
</div> </div>
</template> </template>
@ -247,6 +241,8 @@
import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role"; 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 menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept"; 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 { export default {
name: "Role", name: "Role",
@ -254,12 +250,6 @@ export default {
return { return {
// //
loading: true, loading: true,
//
ids: [],
//
single: true,
//
multiple: true,
// //
showSearch: true, showSearch: true,
// //
@ -272,6 +262,9 @@ export default {
open: false, open: false,
// //
openDataScope: false, openDataScope: false,
//
openMenu: false,
// TODO
menuExpand: false, menuExpand: false,
menuNodeAll: false, menuNodeAll: false,
deptExpand: true, deptExpand: true,
@ -309,10 +302,10 @@ export default {
deptOptions: [], deptOptions: [],
// //
queryParams: { queryParams: {
pageNum: 1, pageNo: 1,
pageSize: 10, pageSize: 10,
roleName: undefined, name: undefined,
roleKey: undefined, code: undefined,
status: undefined status: undefined
}, },
// //
@ -323,23 +316,26 @@ export default {
}, },
// //
rules: { rules: {
roleName: [ name: [
{ required: true, message: "角色名称不能为空", trigger: "blur" } { required: true, message: "角色名称不能为空", trigger: "blur" }
], ],
roleKey: [ code: [
{ required: true, message: "权限字符不能为空", trigger: "blur" } { required: true, message: "角色标识不能为空", trigger: "blur" }
], ],
roleSort: [ sort: [
{ required: true, message: "角色顺序不能为空", trigger: "blur" } { required: true, message: "角色顺序不能为空", trigger: "blur" }
] ]
} },
//
CommonStatusEnum: SysCommonStatusEnum,
//
roleTypeDictDatas: getDictDatas(DICT_TYPE.SYS_ROLE_TYPE),
statusDictDatas: getDictDatas(DICT_TYPE.SYS_COMMON_STATUS)
}; };
}, },
created() { created() {
this.getList(); this.getList();
this.getDicts("sys_normal_disable").then(response => {
this.statusOptions = response.data;
});
}, },
methods: { methods: {
/** 查询角色列表 */ /** 查询角色列表 */
@ -347,8 +343,8 @@ export default {
this.loading = true; this.loading = true;
listRole(this.addDateRange(this.queryParams, this.dateRange)).then( listRole(this.addDateRange(this.queryParams, this.dateRange)).then(
response => { response => {
this.roleList = response.rows; this.roleList = response.data.list;
this.total = response.total; this.total = response.data.total;
this.loading = false; this.loading = false;
} }
); );
@ -384,34 +380,41 @@ export default {
return checkedKeys; return checkedKeys;
}, },
/** 根据角色ID查询菜单树结构 */ /** 根据角色ID查询菜单树结构 */
getRoleMenuTreeselect(roleId) { getRoleMenuTreeselect(id) {
return roleMenuTreeselect(roleId).then(response => { return roleMenuTreeselect(id).then(response => {
this.menuOptions = response.menus; this.menuOptions = response.menus;
return response; return response;
}); });
}, },
/** 根据角色ID查询部门树结构 */ /** 根据角色ID查询部门树结构 */
getRoleDeptTreeselect(roleId) { getRoleDeptTreeselect(id) {
return roleDeptTreeselect(roleId).then(response => { return roleDeptTreeselect(id).then(response => {
this.deptOptions = response.depts; this.deptOptions = response.depts;
return response; return response;
}); });
}, },
// //
handleStatusChange(row) { handleStatusChange(row) {
let text = row.status === "0" ? "启用" : "停用"; // row
this.$confirm('确认要"' + text + '""' + row.roleName + '"角色吗?', "警告", { let text = row.status === SysCommonStatusEnum.ENABLE ? "启用" : "停用";
this.$confirm('确认要"' + text + '""' + row.name + '"角色吗?', "警告", {
confirmButtonText: "确定", confirmButtonText: "确定",
cancelButtonText: "取消", cancelButtonText: "取消",
type: "warning" type: "warning"
}).then(function() { }).then(function() {
return changeRoleStatus(row.roleId, row.status); return changeRoleStatus(row.id, row.status);
}).then(() => { }).then(() => {
this.msgSuccess(text + "成功"); this.msgSuccess(text + "成功");
}).catch(function() { }).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() { cancel() {
this.open = false; this.open = false;
@ -422,6 +425,11 @@ export default {
this.openDataScope = false; this.openDataScope = false;
this.reset(); this.reset();
}, },
//
cancelMenu() {
this.openMenu = false;
this.reset();
},
// //
reset() { reset() {
if (this.$refs.menu != undefined) { if (this.$refs.menu != undefined) {
@ -432,10 +440,10 @@ export default {
this.deptExpand = true, this.deptExpand = true,
this.deptNodeAll = false, this.deptNodeAll = false,
this.form = { this.form = {
roleId: undefined, id: undefined,
roleName: undefined, name: undefined,
roleKey: undefined, code: undefined,
roleSort: 0, sort: 0,
status: "0", status: "0",
ids: [], ids: [],
deptIds: [], deptIds: [],
@ -447,7 +455,7 @@ export default {
}, },
/** 搜索按钮操作 */ /** 搜索按钮操作 */
handleQuery() { handleQuery() {
this.queryParams.pageNum = 1; this.queryParams.pageNo = 1;
this.getList(); this.getList();
}, },
/** 重置按钮操作 */ /** 重置按钮操作 */
@ -456,12 +464,6 @@ export default {
this.resetForm("queryForm"); this.resetForm("queryForm");
this.handleQuery(); this.handleQuery();
}, },
//
handleSelectionChange(selection) {
this.ids = selection.map(item => item.roleId)
this.single = selection.length!=1
this.multiple = !selection.length
},
// / // /
handleCheckedTreeExpand(value, type) { handleCheckedTreeExpand(value, type) {
if (type == 'menu') { if (type == 'menu') {
@ -495,31 +497,50 @@ export default {
/** 新增按钮操作 */ /** 新增按钮操作 */
handleAdd() { handleAdd() {
this.reset(); this.reset();
this.getMenuTreeselect();
this.open = true; this.open = true;
this.title = "添加角色"; this.title = "添加角色";
}, },
/** 修改按钮操作 */ /** 修改按钮操作 */
handleUpdate(row) { handleUpdate(row) {
this.reset(); this.reset();
const roleId = row.roleId || this.ids const id = row.id
const roleMenu = this.getRoleMenuTreeselect(roleId); getRole(id).then(response => {
getRole(roleId).then(response => {
this.form = response.data; this.form = response.data;
this.open = true; this.open = true;
this.title = "修改角色";
});
},
/** 分配菜单权限操作 */
handleMenu(row) {
this.reset();
const id = row.id
// TODO
this.getMenuTreeselect();
const roleMenu = this.getRoleMenuTreeselect(id);
this.$nextTick(() => { this.$nextTick(() => {
roleMenu.then(res => { roleMenu.then(res => {
this.$refs.menu.setCheckedKeys(res.checkedKeys); this.$refs.menu.setCheckedKeys(res.checkedKeys);
}); });
}); });
this.title = "修改角色"; 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) { handleDataScope(row) {
this.reset(); this.reset();
const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId); const roleDeptTreeselect = this.getRoleDeptTreeselect(row.id);
getRole(row.roleId).then(response => { getRole(row.id).then(response => {
this.form = response.data; this.form = response.data;
this.openDataScope = true; this.openDataScope = true;
this.$nextTick(() => { this.$nextTick(() => {
@ -534,15 +555,17 @@ export default {
submitForm: function() { submitForm: function() {
this.$refs["form"].validate(valid => { this.$refs["form"].validate(valid => {
if (valid) { if (valid) {
if (this.form.roleId != undefined) { if (this.form.id !== undefined) {
this.form.ids = this.getMenuAllCheckedKeys(); // TODO
// this.form.ids = this.getMenuAllCheckedKeys();
updateRole(this.form).then(response => { updateRole(this.form).then(response => {
this.msgSuccess("修改成功"); this.msgSuccess("修改成功");
this.open = false; this.open = false;
this.getList(); this.getList();
}); });
} else { } else {
this.form.ids = this.getMenuAllCheckedKeys(); // TODO
// this.form.ids = this.getMenuAllCheckedKeys();
addRole(this.form).then(response => { addRole(this.form).then(response => {
this.msgSuccess("新增成功"); this.msgSuccess("新增成功");
this.open = false; this.open = false;
@ -554,7 +577,19 @@ export default {
}, },
/** 提交按钮(数据权限) */ /** 提交按钮(数据权限) */
submitDataScope: function() { 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(); this.form.deptIds = this.getDeptAllCheckedKeys();
dataScope(this.form).then(response => { dataScope(this.form).then(response => {
this.msgSuccess("修改成功"); this.msgSuccess("修改成功");
@ -565,13 +600,13 @@ export default {
}, },
/** 删除按钮操作 */ /** 删除按钮操作 */
handleDelete(row) { handleDelete(row) {
const roleIds = row.roleId || this.ids; const ids = row.id || this.ids;
this.$confirm('是否确认删除角色编号为"' + roleIds + '"的数据项?', "警告", { this.$confirm('是否确认删除角色编号为"' + ids + '"的数据项?', "警告", {
confirmButtonText: "确定", confirmButtonText: "确定",
cancelButtonText: "取消", cancelButtonText: "取消",
type: "warning" type: "warning"
}).then(function() { }).then(function() {
return delRole(roleIds); return delRole(ids);
}).then(() => { }).then(() => {
this.getList(); this.getList();
this.msgSuccess("删除成功"); this.msgSuccess("删除成功");

View File

@ -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.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult; 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.*;
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.convert.permission.SysRoleConvert; 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.dal.mysql.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -82,14 +78,10 @@ public class SysRoleController {
@ApiOperation("修改角色状态") @ApiOperation("修改角色状态")
@PostMapping("/update-status") @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')") // @PreAuthorize("@ss.hasPermi('system:role:edit')")
// @Log(title = "角色管理", businessType = BusinessType.UPDATE) // @Log(title = "角色管理", businessType = BusinessType.UPDATE)
public CommonResult<Boolean> updateRoleStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) { public CommonResult<Boolean> updateRoleStatus(@Validated @RequestBody SysRoleUpdateStatusReqVO reqVO) {
roleService.updateRoleStatus(id, status); roleService.updateRoleStatus(reqVO.getId(), reqVO.getStatus());
return success(true); return success(true);
} }

View File

@ -30,4 +30,7 @@ public class SysRoleBaseVO {
@ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "见 RoleTypeEnum 枚举") @ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "见 RoleTypeEnum 枚举")
private Integer type; private Integer type;
@ApiModelProperty(value = "备注", example = "我是一个角色")
private String remark;
} }

View File

@ -28,9 +28,6 @@ public class SysRoleRespVO extends SysRoleBaseVO {
@ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "参见 RoleTypeEnum 枚举类") @ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "参见 RoleTypeEnum 枚举类")
private Integer type; private Integer type;
@ApiModelProperty(value = "备注", example = "我是一个角色")
private String remark;
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式") @ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式")
private Date createTime; private Date createTime;

View File

@ -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;
}

View File

@ -105,15 +105,8 @@ public class SysRoleServiceImpl implements SysRoleService {
@Override @Override
public void updateRole(SysRoleUpdateReqVO reqVO) { public void updateRole(SysRoleUpdateReqVO reqVO) {
// 校验更新的角色是否存在 // 校验是否可以更新
SysRoleDO role = roleMapper.selectById(reqVO.getId()); this.checkUpdateRole(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);
}
// 校验角色的唯一字段是否重复 // 校验角色的唯一字段是否重复
checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId());
// 更新到数据库 // 更新到数据库
@ -123,15 +116,8 @@ public class SysRoleServiceImpl implements SysRoleService {
@Override @Override
public void deleteRole(Long id) { public void deleteRole(Long id) {
// 校验删除的角色是否存在 // 校验是否可以更新
SysRoleDO roleDO = roleMapper.selectById(id); this.checkUpdateRole(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);
}
// 标记删除 // 标记删除
roleMapper.deleteById(id); roleMapper.deleteById(id);
// 删除相关数据 // 删除相关数据
@ -152,11 +138,9 @@ public class SysRoleServiceImpl implements SysRoleService {
@Override @Override
public void updateRoleStatus(Long id, Integer status) { public void updateRoleStatus(Long id, Integer status) {
// 校验修改的角色是否存在 // 校验是否可以更新
SysRoleDO roleDO = roleMapper.selectById(id); this.checkUpdateRole(id);
if (roleMapper.selectById(id) == null) { // 更新状态
throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS);
}
SysRoleDO updateObject = new SysRoleDO(); SysRoleDO updateObject = new SysRoleDO();
updateObject.setId(id); updateObject.setId(id);
updateObject.setStatus(status); 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);
}
}
} }