一、根据用户获取角色数据

1、UserController

  1. @ApiOperation(value = "根据用户获取角色数据")
  2. @GetMapping("/toAssign/{userId}")
  3. public R toAssign(@PathVariable String userId) {
  4. Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
  5. return R.ok().data(roleMap);
  6. }

2、RoleServiceImpl

  1. //根据用户获取角色数据
  2. @Override
  3. public Map<String, Object> findRoleByUserId(String userId) {
  4. //查询所有的角色
  5. List<Role> allRolesList =baseMapper.selectList(null);
  6. Map<String, Object> roleMap = new HashMap<>();
  7. roleMap.put("allRolesList", allRolesList);
  8. //根据用户id,查询用户拥有的角色id
  9. LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
  10. wrapper.eq(UserRole::getUserId,userId);
  11. List<UserRole> existUserRoleList = userRoleService.list(wrapper);
  12. // List<UserRole> existUserRoleList = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", userId).select("role_id"));
  13. if(CollectionUtils.isEmpty(existUserRoleList)){
  14. roleMap.put("assignRoles", existUserRoleList);
  15. return roleMap;
  16. }
  17. List<String> existRoleList = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
  18. //根据角色id查询对应的角色对象
  19. List<Role> assignRoles = (List<Role>)roleService.listByIds(existRoleList);
  20. //对角色进行分类
  21. // List<Role> assignRoles = new ArrayList<Role>();
  22. // for (Role role : allRolesList) {
  23. // //已分配
  24. // if(existRoleList.contains(role.getId())) {
  25. // assignRoles.add(role);
  26. // }
  27. // }
  28. roleMap.put("assignRoles", assignRoles);
  29. return roleMap;
  30. }

二、根据用户分配角色

1、UserController

  1. @ApiOperation(value = "根据用户分配角色")
  2. @PostMapping("/doAssign")
  3. public R doAssign(@RequestParam String userId,@RequestParam String[] roleId) {
  4. roleService.saveUserRoleRealtionShip(userId,roleId);
  5. return R.ok();
  6. }

2、RoleServiceImpl

  1. //根据用户分配角色
  2. @Override
  3. public void saveUserRoleRealtionShip(String userId, String[] roleIds) {
  4. // 删除当前用户下所有的角色
  5. LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
  6. wrapper.eq(UserRole::getUserId,userId);
  7. userRoleService.remove(wrapper);
  8. // 分配角色
  9. List<UserRole> userRoles = new ArrayList<>();
  10. if(StringUtils.isEmpty(roleIds)){
  11. return;
  12. }
  13. for(String roleId : roleIds){
  14. UserRole userRole = new UserRole();
  15. userRole.setUserId(userId);
  16. userRole.setRoleId(roleId);
  17. userRoles.add(userRole);
  18. }
  19. userRoleService.saveBatch(userRoles);
  20. // userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id", userId));
  21. // List<UserRole> userRoleList = new ArrayList<>();
  22. // for(String roleId : roleIds) {
  23. // if(StringUtils.isEmpty(roleId)) continue;
  24. // UserRole userRole = new UserRole();
  25. // userRole.setUserId(userId);
  26. // userRole.setRoleId(roleId);
  27. //
  28. // userRoleList.add(userRole);
  29. // }
  30. // userRoleService.saveBatch(userRoleList);
  31. }

三、根据用户id拥有的角色id

1、RoleService

  1. public interface RoleService extends IService<Role> {
  2. // 根据用户id拥有的角色id
  3. List<Role> selectRoleByUserId(String id);
  4. }

2、RoleServiceImpl

  1. @Override
  2. public List<Role> selectRoleByUserId(String id) {
  3. //根据用户id拥有的角色id
  4. List<UserRole> userRoleList = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", id).select("role_id"));
  5. List<String> roleIdList = userRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
  6. List<Role> roleList = new ArrayList<>();
  7. if(roleIdList.size() > 0) {
  8. roleList = baseMapper.selectBatchIds(roleIdList);
  9. }
  10. return roleList;
  11. }

3、IndexServiceImpl

  1. /**
  2. * 根据用户名获取用户登录信息
  3. *
  4. * @param username
  5. * @return
  6. */
  7. public Map<String, Object> getUserInfo(String username) {
  8. Map<String, Object> result = new HashMap<>();
  9. User user = userService.selectByUsername(username);
  10. if (null == user) {
  11. //throw new GuliException(ResultCodeEnum.FETCH_USERINFO_ERROR);
  12. }
  13. //根据用户id获取角色
  14. List<Role> roleList = roleService.selectRoleByUserId(user.getId());
  15. List<String> roleNameList = roleList.stream().map(item -> item.getRoleName()).collect(Collectors.toList());
  16. if(roleNameList.size() == 0) {
  17. //前端框架必须返回一个角色,否则报错,如果没有角色,返回一个空角色
  18. roleNameList.add("");
  19. }
  20. //根据用户id获取操作权限值
  21. List<String> permissionValueList = permissionService.selectPermissionValueByUserId(user.getId());
  22. redisTemplate.opsForValue().set(username, permissionValueList);
  23. result.put("name", user.getUsername());
  24. result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
  25. result.put("roles", roleNameList);
  26. result.put("permissionValueList", permissionValueList);
  27. return result;
  28. }