SpringBoot+Shiro+LayUI权限管理系统项目-6.实现角色管理

1.说明

只讲解关键部分,详细看源码,文章下方捐赠或QQ联系捐赠获取。

2.功能展示

在这里插入图片描述
在这里插入图片描述

3.业务模型

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_role")
public class SysRole implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 角色id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 角色编码
     */
    private String roleCode;


    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建人
     */
    private String createUser;

    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 修改人
     */
    private String updateUser;

    @TableField(exist=false)
    private String deptName;

    @TableField(exist=false)
    private List<SysUser> userList;

    @TableField(exist=false)
    private boolean checked;

}

4.控制器

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysRoleUserService roleUserService;

    @GetMapping("listUI")
    public String listUI() {
        return "role/list";
    }

    @GetMapping("selectMenu/{id}")
    public String selectMenu(@PathVariable Integer id, Map<String,Object> map) {
        SysRole role = roleService.getById(id);
        map.put("role", role);
        return "role/selectMenu";
    }

    @PostMapping("list")
    @ResponseBody
    public Result<IPage<SysRole>> list(@RequestParam(defaultValue = "1") Integer pageNo,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        Page<SysRole> page = new Page<>(pageNo,pageSize);
        IPage<SysRole> result = roleService.selectRoleList(page, null);
        // 设置总记录数
        result.setTotal(roleService.count(queryWrapper));

        /*Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
        Map<String, Object> parameters = null;
        if(Validator.isNullOrEmpty(pager.getParameters())){
            parameters = new HashMap<>();
        }else{
            parameters = pager.getParameters();
        }
        Integer deptId = getUserEntity().getDeptId();
        if(Validator.isNotNullOrEmpty(parameters.get("deptId"))){
            deptId = Integer.parseInt(parameters.get("deptId").toString());
        }
        Page<SysRole> list = roleService.selectRoleList(new Page<SysRole>(pager.getNowPage(), pager.getPageSize()),deptId);
        makeGridResult(parameters, pager, list);*/
        return ResultUtil.ok(result);
    }

    @GetMapping("form")
    public String form(Map<String,Object> map) {
        return "role/form";
    }

    @OperLog(operModule = "用户管理",operType = "修改",operDesc = "修改角色")
    @PostMapping("save")
    @ResponseBody
    public Result<String> add(@RequestBody SysRole role){
        if(role.getId()==null){
            role.setCreateTime(new Date(System.currentTimeMillis()));
            role.setUpdateTime(new Date(System.currentTimeMillis()));
        }else
        {
            role.setUpdateTime(new Date(System.currentTimeMillis()));
        }

        if(!roleService.saveOrUpdate(role)){
            return ResultUtil.fail("保存失败!");
        }
        return ResultUtil.ok();
    }

    @OperLog(operModule = "角色管理",operType = "删除",operDesc = "删除角色")
    @PostMapping("/remove")
    @ResponseBody
    public Result<String> remove(@RequestParam Integer id) {
        // 判断该角色下是否有用户
        QueryWrapper<SysRoleUser> roleUserQuery = new QueryWrapper<>();
        roleUserQuery.eq("role_id",id);
        int roleUserCount = roleUserService.count(roleUserQuery);
        if(roleUserCount > 0){
            return ResultUtil.fail("该角色下存在用户!");
        }

        // 先删除角色和权限关系记录
        roleService.deleteRoleMenu(id);
        // 删除角色记录
        roleService.removeById(id);

        return ResultUtil.ok();
    }

    /**
     * 根据角色ID查询角色
     * @param map
     * @param roleId
     * @return
     */
    @GetMapping("{roleId}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer roleId) {
        SysRole role = roleService.getById(roleId);
        map.put("role", role);
        return "role/edit";
    }

    @GetMapping("{roleId}/menu")
    public String permission(Map<String,Object> map,@PathVariable(required=true) Integer roleId) {
        SysRole role = roleService.getById(roleId);
        List<SysMenu> resources = menuService.queryMenuList(ConvertUtil.toMap("roleId",(Object)roleId));
        List<JSTreeEntity> jstreeList = new TreeUtil().generateJSTree(resources);
        map.put("role", role);
        map.put("menus", jstreeList);
        return "role/menu";
    }

    /**
     *  根据角色ID查询角色拥有的菜单权限
     * @param roleId
     * @return
     */
    @GetMapping("/getMenu/{roleId}")
    @ResponseBody
    public List<SysMenu> getMenu(@PathVariable(required=true) Integer roleId) {
        List<SysMenu> menus = menuService.queryMenuList(ConvertUtil.toMap("roleId",(Object)roleId));
        return menus;
    }

    @OperLog(operModule = "角色管理",operType = "修改",operDesc = "分配权限")
    @PostMapping("saveMenu")
    @ResponseBody
    public Result<String> saveMenu(Integer roleId, @RequestParam(value = "menuIds[]") Integer[] menuIds){
        roleService.saveMenu(roleId, Arrays.asList(menuIds));
        return ResultUtil.ok();
    }

    @GetMapping("listAll")
    @ResponseBody
    public List<SysRole> listAll(){
        List<SysRole> sysRoles = roleService.selectRoleList(null);
        /*List<SelectEntity> selectList = new ArrayList<>();
        SelectEntity entity = null;
        for (SysRole role: sysRoles
             ) {
            entity = new SelectEntity();
            entity.setId(role.getId());
            entity.setName(role.getRoleName());
            selectList.add(entity);
        }*/

        return sysRoles;
    }
}

5.前端页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>角色列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/static/plugin/layui/css/layui.css}" media="all">
</head>
<body>
<table class="layui-hide" id="SysRole" lay-filter="SysRole"></table>

<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">

<!--编辑表单-->
<div class="layui-row" id="editForm" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px">
            <input type="text" id="id" name="id" hidden="hidden">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="roleName" name="roleName" required lay-verify="required" autocomplete="off"
                               placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">角色编码</label>
                    <div class="layui-input-inline">
                        <input type="text" id="roleCode" name="roleCode" required lay-verify="required" autocomplete="off"
                               placeholder="" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="confirm">确认</button>
                    <button type="button" class="layui-btn layui-btn-primary" id="closeBtn">关闭</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/html" id="roleToolBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="role:add">新增</button>
        <button class="layui-btn layui-btn-sm" lay-event="addMenu" shiro:hasPermission="role:addMenu">分配权限</button>
    </div>
</script>

6 获取源码

捐赠任意金额,评论区留下邮箱发送 :)

在这里插入图片描述

posted @ 2021-04-17 10:37  一锤子技术员  阅读(25)  评论(0编辑  收藏  举报  来源