update 优化 重构 LoginHelper 将本地存储代码操作封装
This commit is contained in:
parent
40ec6584c5
commit
96ab48396c
@ -15,6 +15,7 @@ import org.dromara.common.core.domain.model.LoginUser;
|
||||
import org.dromara.common.core.enums.UserType;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 登录鉴权助手
|
||||
@ -36,6 +37,7 @@ public class LoginHelper {
|
||||
public static final String USER_KEY = "userId";
|
||||
public static final String DEPT_KEY = "deptId";
|
||||
public static final String CLIENT_KEY = "clientid";
|
||||
public static final String TENANT_ADMIN_KEY = "isTenantAdmin";
|
||||
|
||||
/**
|
||||
* 登录系统 基于 设备类型
|
||||
@ -62,17 +64,13 @@ public class LoginHelper {
|
||||
* 获取用户(多级缓存)
|
||||
*/
|
||||
public static LoginUser getLoginUser() {
|
||||
LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
|
||||
if (loginUser != null) {
|
||||
return loginUser;
|
||||
}
|
||||
return (LoginUser) getStorageIfAbsentSet(LOGIN_USER_KEY, () -> {
|
||||
SaSession session = StpUtil.getSession();
|
||||
if (ObjectUtil.isNull(session)) {
|
||||
return null;
|
||||
}
|
||||
loginUser = (LoginUser) session.get(LOGIN_USER_KEY);
|
||||
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
|
||||
return loginUser;
|
||||
return session.get(LOGIN_USER_KEY);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,17 +107,7 @@ public class LoginHelper {
|
||||
}
|
||||
|
||||
private static Object getExtra(String key) {
|
||||
Object obj;
|
||||
try {
|
||||
obj = SaHolder.getStorage().get(key);
|
||||
if (ObjectUtil.isNull(obj)) {
|
||||
obj = StpUtil.getExtra(key);
|
||||
SaHolder.getStorage().set(key, obj);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return obj;
|
||||
return getStorageIfAbsentSet(key, () -> StpUtil.getExtra(key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,7 +150,26 @@ public class LoginHelper {
|
||||
}
|
||||
|
||||
public static boolean isTenantAdmin() {
|
||||
Object value = getStorageIfAbsentSet(TENANT_ADMIN_KEY, () -> {
|
||||
return isTenantAdmin(getLoginUser().getRolePermission());
|
||||
});
|
||||
return Convert.toBool(value);
|
||||
}
|
||||
|
||||
public static boolean isLogin() {
|
||||
return getLoginUser() != null;
|
||||
}
|
||||
|
||||
public static Object getStorageIfAbsentSet(String key, Supplier<Object> handle) {
|
||||
try {
|
||||
Object obj = SaHolder.getStorage().get(key);
|
||||
if (ObjectUtil.isNull(obj)) {
|
||||
obj = handle.get();
|
||||
SaHolder.getStorage().set(key, obj);
|
||||
}
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user