update 优化 重构 LoginHelper 将本地存储代码操作封装

2.X
疯狂的狮子Li 1 year ago
parent b28d2cbd1c
commit b0fe5a00e0

@ -15,6 +15,7 @@ import org.dromara.common.core.enums.UserType;
import org.dromara.system.api.model.LoginUser; import org.dromara.system.api.model.LoginUser;
import java.util.Set; 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 USER_KEY = "userId";
public static final String DEPT_KEY = "deptId"; public static final String DEPT_KEY = "deptId";
public static final String CLIENT_KEY = "clientid"; 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() { public static LoginUser getLoginUser() {
LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY); return (LoginUser) getStorageIfAbsentSet(LOGIN_USER_KEY, () -> {
if (loginUser != null) { SaSession session = StpUtil.getSession();
return loginUser; if (ObjectUtil.isNull(session)) {
} return null;
SaSession session = StpUtil.getSession(); }
if (ObjectUtil.isNull(session)) { return session.get(LOGIN_USER_KEY);
return null; });
}
loginUser = (LoginUser) session.get(LOGIN_USER_KEY);
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
return loginUser;
} }
/** /**
@ -109,17 +107,7 @@ public class LoginHelper {
} }
private static Object getExtra(String key) { private static Object getExtra(String key) {
Object obj; return getStorageIfAbsentSet(key, () -> StpUtil.getExtra(key));
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;
} }
/** /**
@ -162,7 +150,26 @@ public class LoginHelper {
} }
public static boolean isTenantAdmin() { public static boolean isTenantAdmin() {
return isTenantAdmin(getLoginUser().getRolePermission()); 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…
Cancel
Save