LoginUtils.java 3.1 KB

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