Controller
@GetMapping("/info")
public Result<Map<String, Object>> getInfo(@RequestParam("token") String token) {
HashMap<String, Object> info = userService.getInfo(token);
if (info == null) {
return Result.fail(info, "用户名不存在");
}
return Result.success(info, "获取用户信息成功");
}
Service
@Override
public HashMap<String, Object> getInfo(String token) {
Object o = redisTemplate.opsForValue().get(token);
if(o != null) {
String s = JSON.toJSONString(o);
User user = JSON.parseObject(s, User.class);
HashMap<String, Object> info = new HashMap<>();
info.put("name", user.getUsername());
info.put("avatar", user.getAvatar());
// 角色
List<String> roles = userMapper.getRoleNameByUserId(user.getId());
info.put("roles", roles);
return info;
}
return null;
}
SQL
public interface UserMapper extends BaseMapper<User> {
@Select("""
select role_name from x_role
where role_id in (
select role_id from x_user_role
where id = 1
)
""")
public List<String> getRoleNameByUserId(int id);
}