2. 6 后端:角色权限进行修改授权【先删除之前权限,在添加所有已选权限】
- @RestController
- @RequestMapping("/role")
- public class SysRoleController {
- @Resource
- private SysRoleService sysRoleService;
-
- @GetMapping
- public BaseResult findAll() {
- List
list = sysRoleService.list(); - return BaseResult.ok("查询成功", list);
- }
-
- }
效果展示:要求角色和权限菜单左右分屏

- <template>
- <div>
- <el-row>
- <el-col :span="16">
- <el-card class="role-list-card">
- <div slot="header" class="clearfix">
- <span>角色列表span>
- div>
-
- <el-table
- :data="roleList"
- stripe
- style="width: 100%">
- <el-table-column
- prop="id"
- label="角色ID"
- fixed
- width="80">
- el-table-column>
- <el-table-column
- prop="roleName"
- label="角色名称"
- fixed
- width="180">
- el-table-column>
- <el-table-column
- prop="roleDesc"
- label="角色描述"
- width="200">
- el-table-column>
- <el-table-column
- label="操作"
- fixed="right">
- <template slot-scope="scope">
- <el-button size="mini">编辑el-button>
- <el-button size="mini" type="danger">删除el-button>
- template>
- el-table-column>
- el-table>
-
- el-card>
- el-col>
- <el-col :span="8" style="padding-left: 10px;">
- <el-card class="perm-list-card">
- <div slot="header" class="clearfix">
- <span>权限展示span>
- <el-button type="primary" style="float: right; padding: 3px 0">授权el-button>
- div>
-
-
- el-card>
-
- el-col>
- el-row>
- div>
- template>
-
- <script>
- export default {
- data() {
- return {
- roleList: []
- }
- },
- methods: {
- async findAllRole() {
- // ajax
- let { data: baseResult } = await this.$axios.get('/user-service/role')
- // 处理
- if(baseResult.code == 20000) {
- this.roleList = baseResult.data
- } else {
- this.$message.error(baseResult.message)
- }
- }
- },
- mounted() {
- // 查询所有的角色
- this.findAllRole()
- },
- }
- script>
-
- <style>
- .role-list-card {
- height: 100%;
- }
- .perm-list-card {
- height: 100%;
- }
- style>

方式1:在controller中排序查询所有,然后使用Map进行缓存处理,将所有权限拼凑成父子关系。
方式2:使用mapper注解版
编写PermMapper:查询指定父id的所有权限,需配置关联项(当前权限的所有的孩子)
编写service:查询所有
编写controller:查询所有
1. 编写PermMapper:查询指定父id的所有权限,需配置关联项(当前权限的所有的孩子)
- @Mapper
- public interface SysPermissionMapper extends BaseMapper
{ - /**
- * 通过父id查询所有的权限
- * @author 桐叔
- * @email liangtong@itcast.cn
- * @return
- */
- @Select("SELECT * FROM sys_permission WHERE parent_id = #{parentId}")
- @Results({
- @Result(property = "id", column = "id"),
- @Result(property = "permName", column = "permName"),
- @Result(property = "parentId", column = "parent_id"),
- @Result(property = "path", column = "path"),
- @Result(property = "children", many = @Many(select = "com.czxy.classes.mapper.SysPermissionMapper.findAllByParentId"), column = "id")
- })
- public List
findAllByParentId(@Param("parentId") Integer parentId) ; -
-
- }
-
2.编写service:查询所有
接口
- @Service
- @Transactional
- public interface SysPermissionService extends IService
{ - public List
findAllByParentId(Integer parentId) ; - }
-
实现类
- @Service
- @Transactional
- public class SysPermissionServiceImpl extends ServiceImpl
implements SysPermissionService { -
- @Override
- public List
findAllByParentId(Integer parentId) { - return baseMapper.findAllByParentId(parentId);
- }
- }
3.编写controller:查询所有
- @RestController
- @RequestMapping("/perm")
- public class SysPermissionController {
- @Resource
- private SysPermissionService sysPermissionService;
-
-
- /**
- * 查询所有,含孩子
- * @author 桐叔
- * @email liangtong@itcast.cn
- * @return
- */
- @GetMapping("/parent/{parentId}")
- public BaseResult findAllByParentId(@PathVariable("parentId") Integer parentId) {
- // 查询
- List
list = sysPermissionService.findAllByParentId(parentId); - return BaseResult.ok("查询成功", list);
- }
-
- }
提交数据:roleId = 1
获得数据:[ {roleId: 1, permId: 1}, {roleId: 1, permId: 2}, ...] --> [1,2,3,4]

-
- @RestController
- @RequestMapping("/rolePerm")
- public class SysRolePermissionController {
- @Resource
- private SysRolePermissionService sysRolePermissionService;
-
- @GetMapping("/role/{roleId}")
- public BaseResult findAllByRoleId(@PathVariable("roleId") Integer roleId) {
- //1 条件 roleId = 1
- QueryWrapper
queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("role_id", roleId);
-
- //2 查询所有- 角色权限对象
- List
list = sysRolePermissionService.list(queryWrapper); -
- //3 处理数据,只需要权限id
- List
roleIdList = list.stream().map(sysRolePermission -> sysRolePermission.getPermId()).collect(Collectors.toList()); -
- //4 返回
- return BaseResult.ok("查询成功", roleIdList);
- }
-
-
- }
-
编写变量、发送ajax查询、页面加载成功时调用
使用tree进行展示

- <template>
- <div>
- <el-row>
- <el-col :span="16">
- <el-card class="role-list-card">
- <div slot="header" class="clearfix">
- <span>角色列表span>
- div>
-
-
- el-card>
- el-col>
- <el-col :span="8" style="padding-left: 10px;">
- <el-card class="perm-list-card">
- <div slot="header" class="clearfix">
- <span>权限展示span>
- <el-button type="primary" style="float: right; padding: 3px 0">授权el-button>
- div>
-
- <el-tree
- :data="permList"
- show-checkbox
- default-expand-all
- node-key="id"
- ref="permTree"
- highlight-current
- :props="defaultProps">
- el-tree>
-
- el-card>
-
- el-col>
- el-row>
- div>
- template>
-
- <script>
- export default {
- data() {
- return {
- roleList: [], //角色列表
- permList: [], //权限列表
- defaultProps: { //tree提供的数据 与 所需数据 对应关系
- children: 'children',
- label: 'permName'
- }
- }
- },
- methods: {
- //获取所有角色
- async findAllRole() {
- // ajax
- let { data: baseResult } = await this.$axios.get('/user-service/role')
- // 处理
- if(baseResult.code == 20000) {
- this.roleList = baseResult.data
- } else {
- this.$message.error(baseResult.message)
- }
- },
- //获取所有的一级权限及孩子
- async findAllPerm(parentId) {
- // ajax
- let { data: baseResult } = await this.$axios.get(`/user-service/perm/parent/${parentId}`)
- // 处理
- if(baseResult.code == 20000) {
- this.permList = baseResult.data
- } else {
- this.$message.error(baseResult.message)
- }
- }
- },
- mounted() {
- // 查询所有的角色
- this.findAllRole()
- // 查询所有的一级权限
- this.findAllPerm(0)
- },
- }
- script>
-
- <style>
- .role-list-card {
- height: 100%;
- }
- .perm-list-card {
- height: 100%;
- }
- style>
页面效果演示:

表格行的点击,并获得当前行的数据
查询当前角色对应的所有选项,并回显到tree中。点击角色行,获取对应的权限并勾选。
表格及树形控件中的方法绑定及属性设置,请按照图片在对应位置进行设置

- //查询对应角色的权限
- async findAllPermByRoleId(row, column, event) {
- // ajax 查询 /user-service/rolePerm/role/1
- let { data: baseResult } = await this.$axios.get(`/user-service/rolePerm/role/${row.id}`)
- // 处理
- if(baseResult.code == 20000) {
- // 查询成功,将查询的结果填充到右侧tree中
- this.$refs.permTree.setCheckedKeys(baseResult.data);
- } else {
- this.$message.error(baseResult.message)
- }
- }


- //查询对应角色的权限
- //对点击过角色所在行的操作,对角色id进行记录
- async findAllPermByRoleId(row, column, event) {
- // ajax 查询 /user-service/rolePerm/role/1
- let { data: baseResult } = await this.$axios.get(`/user-service/rolePerm/role/${row.id}`)
- // 处理
- if(baseResult.code == 20000) {
- // 查询成功,将查询的结果填充到右侧tree中
- this.$refs.permTree.setCheckedKeys(baseResult.data);
- // 记录已有数据
- this.role.id = row.id
- this.role.permIds = baseResult.data
- console.info(this.role)
- } else {
- this.$message.error(baseResult.message)
- }
- },
- //判断是否点击过角色行,并进行提示
- //对角色的权限进行修改
- async addPermWithRoleId() {
- // 判断是否选择角色
- if(! this.role.id) {
- this.$message.warning('请先选择角色')
- return;
- }
- // 更新所选权限
- this.role.permIds = this.$refs.permTree.getCheckedKeys()
- // ajax 提交
- let { data: baseResult } = await this.$axios.post('/user-service/rolePerm/addPerm', this.role)
- // 提示
- if(baseResult.code == 20000) {
- this.$message.success(baseResult.message)
- } else {
- this.$message.error(baseResult.message)
- }
- }
编写controller

- @PostMapping("/addPerm")
- public BaseResult addPermWithRoleId(@RequestBody SysRole sysRole) {
- try {
- // 添加权限
- sysRolePermissionService.addPermWithRoleId(sysRole);
-
- // 提示
- return BaseResult.ok("授权成功");
- } catch (Exception e) {
- return BaseResult.error("授权失败");
- }
-
- }
编写service

接口
- @Service
- @Transactional
- public interface SysRolePermissionService extends IService
{ - void addPermWithRoleId(SysRole sysRole);
- }
实现类
- @Service
- @Transactional
- public class SysRolePermissionServiceImpl extends ServiceImpl
implements SysRolePermissionService { -
- @Override
- public void addPermWithRoleId(SysRole sysRole) {
- // 1 删除
- QueryWrapper
queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("role_id", sysRole.getId());
- baseMapper.delete(queryWrapper);
-
- // 2 添加
- for (Integer permId : sysRole.getPermIds()) {
- SysRolePermission sysRolePermission = new SysRolePermission(sysRole.getId(), permId);
- baseMapper.insert(sysRolePermission);
- }
- }
- }
四季轮换,已经数不清凋零了多少, 愿我们往后能向心而行,一路招摇胜!
🐋 你的支持认可是我创作的动力
💟 创作不易,不妨点赞💚评论❤️收藏💙一下
😘 感谢大佬们的支持,欢迎各位前来不吝赐教