2022-01-17 12:03:15 +08:00
|
|
|
package com.ruoyi.common.helper;
|
2021-09-28 17:56:19 +08:00
|
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
2021-12-03 18:46:49 +08:00
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
2021-09-28 17:56:19 +08:00
|
|
|
import com.ruoyi.common.enums.DeviceType;
|
|
|
|
import com.ruoyi.common.enums.UserType;
|
|
|
|
import com.ruoyi.common.exception.UtilException;
|
2022-01-17 12:03:15 +08:00
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
2022-01-16 16:51:23 +08:00
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.NoArgsConstructor;
|
2021-09-28 17:56:19 +08:00
|
|
|
|
|
|
|
/**
|
2022-01-17 12:03:15 +08:00
|
|
|
* 登录鉴权助手
|
2021-09-28 17:56:19 +08:00
|
|
|
* 为适配多端登录而封装
|
|
|
|
*
|
|
|
|
* @author Lion Li
|
|
|
|
*/
|
2022-01-16 16:51:23 +08:00
|
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
2022-01-17 12:03:15 +08:00
|
|
|
public class LoginHelper {
|
2021-09-28 17:56:19 +08:00
|
|
|
|
2022-01-17 17:09:27 +08:00
|
|
|
public static final String JOIN_CODE = ":";
|
|
|
|
public static final String LOGIN_USER_KEY = "loginUser";
|
|
|
|
|
2022-01-17 12:03:15 +08:00
|
|
|
private static final ThreadLocal<LoginUser> LOGIN_CACHE = new ThreadLocal<>();
|
2021-12-03 18:46:49 +08:00
|
|
|
|
2021-09-28 17:56:19 +08:00
|
|
|
/**
|
|
|
|
* 登录系统
|
|
|
|
* 针对两套用户体系
|
2022-01-16 16:51:23 +08:00
|
|
|
*
|
2021-12-03 18:46:49 +08:00
|
|
|
* @param loginUser 登录用户信息
|
2021-09-28 17:56:19 +08:00
|
|
|
*/
|
2022-01-17 12:03:15 +08:00
|
|
|
public static void login(LoginUser loginUser) {
|
2022-01-26 21:07:21 +08:00
|
|
|
LOGIN_CACHE.set(loginUser);
|
2022-01-17 12:03:15 +08:00
|
|
|
StpUtil.login(loginUser.getLoginId());
|
2021-12-03 19:13:15 +08:00
|
|
|
setLoginUser(loginUser);
|
2021-09-28 17:56:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 登录系统 基于 设备类型
|
|
|
|
* 针对一套用户体系
|
2022-01-16 16:51:23 +08:00
|
|
|
*
|
2021-12-03 18:46:49 +08:00
|
|
|
* @param loginUser 登录用户信息
|
2021-09-28 17:56:19 +08:00
|
|
|
*/
|
2022-01-17 12:03:15 +08:00
|
|
|
public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
|
2022-01-26 21:07:21 +08:00
|
|
|
LOGIN_CACHE.set(loginUser);
|
2022-01-17 12:03:15 +08:00
|
|
|
StpUtil.login(loginUser.getLoginId(), deviceType.getDevice());
|
2021-12-03 19:13:15 +08:00
|
|
|
setLoginUser(loginUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-17 12:03:15 +08:00
|
|
|
* 设置用户数据(多级缓存)
|
2021-12-03 19:13:15 +08:00
|
|
|
*/
|
|
|
|
public static void setLoginUser(LoginUser loginUser) {
|
2021-12-03 18:46:49 +08:00
|
|
|
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-17 12:03:15 +08:00
|
|
|
* 获取用户(多级缓存)
|
|
|
|
*/
|
2021-12-03 18:46:49 +08:00
|
|
|
public static LoginUser getLoginUser() {
|
2022-01-17 12:03:15 +08:00
|
|
|
LoginUser loginUser = LOGIN_CACHE.get();
|
|
|
|
if (loginUser != null) {
|
|
|
|
return loginUser;
|
|
|
|
}
|
2021-12-03 18:46:49 +08:00
|
|
|
return (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
|
2021-09-28 17:56:19 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 12:03:15 +08:00
|
|
|
/**
|
|
|
|
* 清除一级缓存 防止内存问题
|
|
|
|
*/
|
|
|
|
public static void clearCache() {
|
|
|
|
LOGIN_CACHE.remove();
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:56:19 +08:00
|
|
|
/**
|
|
|
|
* 获取用户id
|
|
|
|
*/
|
|
|
|
public static Long getUserId() {
|
2021-12-03 18:46:49 +08:00
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
if (ObjectUtil.isNull(loginUser)) {
|
|
|
|
String loginId = StpUtil.getLoginIdAsString();
|
2022-01-17 12:03:15 +08:00
|
|
|
String userId = null;
|
|
|
|
for (UserType value : UserType.values()) {
|
|
|
|
if (StringUtils.contains(loginId, value.getUserType())) {
|
2022-01-17 17:09:27 +08:00
|
|
|
String[] strs = StringUtils.split(loginId, JOIN_CODE);
|
|
|
|
// 用户id在总是在最后
|
|
|
|
userId = strs[strs.length - 1];
|
2022-01-17 12:03:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (StringUtils.isBlank(userId)) {
|
2021-12-03 18:46:49 +08:00
|
|
|
throw new UtilException("登录用户: LoginId异常 => " + loginId);
|
|
|
|
}
|
|
|
|
return Long.parseLong(userId);
|
2021-09-28 17:56:19 +08:00
|
|
|
}
|
2021-12-03 18:46:49 +08:00
|
|
|
return loginUser.getUserId();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取部门ID
|
2022-01-17 12:03:15 +08:00
|
|
|
*/
|
2021-12-03 18:46:49 +08:00
|
|
|
public static Long getDeptId() {
|
|
|
|
return getLoginUser().getDeptId();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户账户
|
2022-01-17 12:03:15 +08:00
|
|
|
*/
|
2021-12-03 18:46:49 +08:00
|
|
|
public static String getUsername() {
|
|
|
|
return getLoginUser().getUsername();
|
2021-09-28 17:56:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户类型
|
|
|
|
*/
|
|
|
|
public static UserType getUserType() {
|
|
|
|
String loginId = StpUtil.getLoginIdAsString();
|
2022-01-17 12:03:15 +08:00
|
|
|
return UserType.getUserType(loginId);
|
2021-09-28 17:56:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|