LoginUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.ruoyi.common.utils;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.ruoyi.common.core.domain.model.LoginUser;
  5. import com.ruoyi.common.enums.DeviceType;
  6. import com.ruoyi.common.enums.UserType;
  7. import com.ruoyi.common.exception.UtilException;
  8. import lombok.AccessLevel;
  9. import lombok.NoArgsConstructor;
  10. /**
  11. * 登录鉴权工具
  12. * 为适配多端登录而封装
  13. *
  14. * @author Lion Li
  15. */
  16. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  17. public class LoginUtils {
  18. private static final String LOGIN_USER_KEY = "loginUser";
  19. /**
  20. * 登录系统
  21. * 针对两套用户体系
  22. *
  23. * @param loginUser 登录用户信息
  24. */
  25. public static void login(LoginUser loginUser, UserType userType) {
  26. StpUtil.login(userType.getUserType() + loginUser.getUserId());
  27. setLoginUser(loginUser);
  28. }
  29. /**
  30. * 登录系统 基于 设备类型
  31. * 针对一套用户体系
  32. *
  33. * @param loginUser 登录用户信息
  34. */
  35. public static void loginByDevice(LoginUser loginUser, UserType userType, DeviceType deviceType) {
  36. StpUtil.login(userType.getUserType() + loginUser.getUserId(), deviceType.getDevice());
  37. setLoginUser(loginUser);
  38. }
  39. /**
  40. * 设置用户数据
  41. */
  42. public static void setLoginUser(LoginUser loginUser) {
  43. StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
  44. }
  45. /**
  46. * 获取用户
  47. **/
  48. public static LoginUser getLoginUser() {
  49. return (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
  50. }
  51. /**
  52. * 获取用户id
  53. */
  54. public static Long getUserId() {
  55. LoginUser loginUser = getLoginUser();
  56. if (ObjectUtil.isNull(loginUser)) {
  57. String loginId = StpUtil.getLoginIdAsString();
  58. String userId;
  59. String replace = "";
  60. if (StringUtils.contains(loginId, UserType.SYS_USER.getUserType())) {
  61. userId = StringUtils.replace(loginId, UserType.SYS_USER.getUserType(), replace);
  62. } else if (StringUtils.contains(loginId, UserType.APP_USER.getUserType())) {
  63. userId = StringUtils.replace(loginId, UserType.APP_USER.getUserType(), replace);
  64. } else {
  65. throw new UtilException("登录用户: LoginId异常 => " + loginId);
  66. }
  67. return Long.parseLong(userId);
  68. }
  69. return loginUser.getUserId();
  70. }
  71. /**
  72. * 获取部门ID
  73. **/
  74. public static Long getDeptId() {
  75. return getLoginUser().getDeptId();
  76. }
  77. /**
  78. * 获取用户账户
  79. **/
  80. public static String getUsername() {
  81. return getLoginUser().getUsername();
  82. }
  83. /**
  84. * 获取用户类型
  85. */
  86. public static UserType getUserType() {
  87. String loginId = StpUtil.getLoginIdAsString();
  88. return getUserType(loginId);
  89. }
  90. public static UserType getUserType(Object loginId) {
  91. if (StringUtils.contains(loginId.toString(), UserType.SYS_USER.getUserType())) {
  92. return UserType.SYS_USER;
  93. } else if (StringUtils.contains(loginId.toString(), UserType.APP_USER.getUserType())) {
  94. return UserType.APP_USER;
  95. } else {
  96. throw new UtilException("登录用户: LoginId异常 => " + loginId);
  97. }
  98. }
  99. }