ApplicationContextUtil.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package me.zhengjie.base.util;
  2. import com.alibaba.fastjson.JSON;
  3. import lombok.RequiredArgsConstructor;
  4. import me.zhengjie.application.admin.service.dto.JwtUserDto;
  5. import me.zhengjie.security.service.dto.OnlineUserDto;
  6. import me.zhengjie.utils.RedisUtils;
  7. import me.zhengjie.utils.SecurityUtils;
  8. import me.zhengjie.utils.enums.DataScopeEnum;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. @Service
  12. @RequiredArgsConstructor
  13. public class ApplicationContextUtil {
  14. private final RedisUtils redisUtils;
  15. /**
  16. * 获取指定缓存Key的用户信息
  17. * @param key
  18. * @return
  19. */
  20. public JwtUserDto getUserByKey(String key) {
  21. return JSON.parseObject((String) redisUtils.get(key), JwtUserDto.class);
  22. }
  23. /**
  24. * 获取当前登录的用户
  25. * @return UserDetails
  26. */
  27. public JwtUserDto getCurrentUser() {
  28. OnlineUserDto onlineUser = (OnlineUserDto) SecurityUtils.getCurrentUser();
  29. return JSON.parseObject((String) redisUtils.get(onlineUser.getOnlineToken()), JwtUserDto.class);
  30. }
  31. /**
  32. * 获取系统用户名称
  33. *
  34. * @return 系统用户名称
  35. */
  36. public String getCurrentUsername() {
  37. JwtUserDto jwtUserDto = getCurrentUser();
  38. return jwtUserDto.getUsername();
  39. }
  40. /**
  41. * 获取系统用户ID
  42. * @return 系统用户ID
  43. */
  44. public Long getCurrentUserId() {
  45. JwtUserDto jwtUserDto = getCurrentUser();
  46. return jwtUserDto.getUserId();
  47. }
  48. /**
  49. * 获取当前用户的数据权限
  50. * @return /
  51. */
  52. public List<Long> getCurrentUserDataScope(){
  53. JwtUserDto jwtUserDto = getCurrentUser();
  54. return jwtUserDto.getDataScopes();
  55. }
  56. /**
  57. * 获取数据权限级别
  58. * @return 级别
  59. */
  60. public String getDataScopeType() {
  61. List<Long> dataScopes = getCurrentUserDataScope();
  62. if(dataScopes.size() != 0){
  63. return "";
  64. }
  65. return DataScopeEnum.ALL.getValue();
  66. }
  67. }