如何区分授权角色在左侧,未授权在右侧

来源:互联网 发布:java post accept 编辑:程序博客网 时间:2024/04/28 15:14

如何实现以下效果


代码如下:

/** * 辅助查询 * @param list * @return */public Map<String,String> changMapByUserRole(List<SysUserRoleR> list){    Map<String,String> resultMap = new HashMap<>();    for(SysUserRoleR roleR:list){
//设置一个标志        resultMap.put(roleR.getRoleId(),"1");    }    return resultMap;}
思路:把所有的角色查询出来,然后利用map的属性来进行区分。

解释:

形参:将关联表中的所有角色查询出来。

以下代码是主查询的实现

/** * 主要代码 * @param req 接受的是json串,传json传的http请求只能是POST * @return */@RequestMapping(value = "/findAllRole",method = RequestMethod.POST)@ResponseBodypublic Object findAllRole(@RequestBody String req){    SysUserRoleR sur = null;    try{
//这里将json传转换为对象,只有key与对象的属性一致才能转换成功,否则失败        sur = jsonTranster.readValue(req,SysUserRoleR.class);    }catch (Exception e){        e.printStackTrace();        return addResultMapMsg(false,e.getMessage());    }
//判断对象是否为空或者用户的主键未传    if(BeanUtils.isBlank(sur) || StringUtils.isEmpty(sur.getUserId())){        return addResultMapMsg(false,"未传入userId");    }    //查询出所有角色    List<Role> rolelist = roleService.findAll();    if(rolelist.size() == 0 || rolelist.isEmpty()){        return addResultMapMsg(false,"用户角色列表查询失败");    }    //根据用户id查询角色    List<SysUserRoleR> list = sysUserRoleRService.findByUserId(sur.getUserId());    if(list == null){        return addResultMapMsg(true,rolelist);    }
//调用上面的辅助查询方法    Map<String,String> roleMap =  changMapByUserRole(list);
//遍历所有的角色    for(Role role:rolelist){
//利用辅助查询里面的设置来判断角色是否授权,如果有id就和“1”进行来进行判断        if("1".equals(roleMap.get(role.getId()))){ //当前用户有的角色
//判断有这个id设置Role中的标示字段为true,否则为false,前端会根据这个字段的属性进行区分            role.setRoleFlag(true);        }else{            role.setRoleFlag(false);        }    }   return addResultMapMsg(true,rolelist);}


阅读全文
0 0
原创粉丝点击