mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
update 优化多用户体系处理 更名 LoginUtils 为 LoginHelper 支持 LoginUser 多级缓存
This commit is contained in:
@ -4,8 +4,9 @@ import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@ -31,19 +32,19 @@ public class ExcelBigNumberConvert implements Converter<Long> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
return Convert.toLong(cellData.getData());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
public WriteCellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
if (ObjectUtil.isNotNull(object)) {
|
||||
String str = Convert.toStr(object);
|
||||
if (str.length() > 15) {
|
||||
return new CellData<>(str);
|
||||
return new WriteCellData<>(str);
|
||||
}
|
||||
}
|
||||
CellData<Object> cellData = new CellData<>(new BigDecimal(object));
|
||||
WriteCellData<Object> cellData = new WriteCellData<>(new BigDecimal(object));
|
||||
cellData.setType(CellDataTypeEnum.NUMBER);
|
||||
return cellData;
|
||||
}
|
||||
|
@ -4,8 +4,9 @@ import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import com.ruoyi.common.annotation.ExcelDictFormat;
|
||||
import com.ruoyi.common.core.service.DictService;
|
||||
@ -35,7 +36,7 @@ public class ExcelDictConvert implements Converter<Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
ExcelDictFormat anno = getAnnotation(contentProperty.getField());
|
||||
String type = anno.dictType();
|
||||
String label = cellData.getStringValue();
|
||||
@ -49,9 +50,9 @@ public class ExcelDictConvert implements Converter<Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||
if (StringUtils.isNull(object)) {
|
||||
return new CellData<>("");
|
||||
return new WriteCellData<>("");
|
||||
}
|
||||
ExcelDictFormat anno = getAnnotation(contentProperty.getField());
|
||||
String type = anno.dictType();
|
||||
@ -62,7 +63,7 @@ public class ExcelDictConvert implements Converter<Object> {
|
||||
} else {
|
||||
label = SpringUtils.getBean(DictService.class).getDictLabel(type, value, anno.separator());
|
||||
}
|
||||
return new CellData<>(label);
|
||||
return new WriteCellData<>(label);
|
||||
}
|
||||
|
||||
private ExcelDictFormat getAnnotation(Field field) {
|
||||
|
@ -2,7 +2,7 @@ package com.ruoyi.common.core.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.utils.LoginUtils;
|
||||
import com.ruoyi.common.helper.LoginHelper;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
|
||||
/**
|
||||
@ -71,27 +71,27 @@ public class BaseController {
|
||||
* 获取用户缓存信息
|
||||
*/
|
||||
public LoginUser getLoginUser() {
|
||||
return LoginUtils.getLoginUser();
|
||||
return LoginHelper.getLoginUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户id
|
||||
*/
|
||||
public Long getUserId() {
|
||||
return LoginUtils.getUserId();
|
||||
return LoginHelper.getUserId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录部门id
|
||||
*/
|
||||
public Long getDeptId() {
|
||||
return LoginUtils.getDeptId();
|
||||
return LoginHelper.getDeptId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户名
|
||||
*/
|
||||
public String getUsername() {
|
||||
return LoginUtils.getUsername();
|
||||
return LoginHelper.getUsername();
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,11 @@ public class LoginUser implements Serializable {
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 用户类型
|
||||
*/
|
||||
private String userType;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
@ -80,4 +85,8 @@ public class LoginUser implements Serializable {
|
||||
*/
|
||||
private String username;
|
||||
|
||||
public String getLoginId() {
|
||||
return userType + userId;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.ruoyi.common.enums;
|
||||
|
||||
import com.ruoyi.common.exception.UtilException;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@ -24,4 +26,13 @@ public enum UserType {
|
||||
APP_USER("app_user:");
|
||||
|
||||
private final String userType;
|
||||
|
||||
public static UserType getUserType(String str) {
|
||||
for (UserType value : values()) {
|
||||
if (StringUtils.contains(str, value.getUserType())) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("'UserType' not found By " + str);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package com.ruoyi.common.helper;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.ruoyi.common.utils.ServletUtils;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
@ -13,6 +15,7 @@ import java.util.Map;
|
||||
* @author Lion Li
|
||||
* @version 3.5.0
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@SuppressWarnings("unchecked cast")
|
||||
public class DataPermissionHelper {
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.ruoyi.common.utils;
|
||||
package com.ruoyi.common.helper;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@ -6,19 +6,21 @@ import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.enums.DeviceType;
|
||||
import com.ruoyi.common.enums.UserType;
|
||||
import com.ruoyi.common.exception.UtilException;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 登录鉴权工具
|
||||
* 登录鉴权助手
|
||||
* 为适配多端登录而封装
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class LoginUtils {
|
||||
public class LoginHelper {
|
||||
|
||||
private static final String LOGIN_USER_KEY = "loginUser";
|
||||
private static final ThreadLocal<LoginUser> LOGIN_CACHE = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 登录系统
|
||||
@ -26,8 +28,8 @@ public class LoginUtils {
|
||||
*
|
||||
* @param loginUser 登录用户信息
|
||||
*/
|
||||
public static void login(LoginUser loginUser, UserType userType) {
|
||||
StpUtil.login(userType.getUserType() + loginUser.getUserId());
|
||||
public static void login(LoginUser loginUser) {
|
||||
StpUtil.login(loginUser.getLoginId());
|
||||
setLoginUser(loginUser);
|
||||
}
|
||||
|
||||
@ -37,25 +39,37 @@ public class LoginUtils {
|
||||
*
|
||||
* @param loginUser 登录用户信息
|
||||
*/
|
||||
public static void loginByDevice(LoginUser loginUser, UserType userType, DeviceType deviceType) {
|
||||
StpUtil.login(userType.getUserType() + loginUser.getUserId(), deviceType.getDevice());
|
||||
public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
|
||||
StpUtil.login(loginUser.getLoginId(), deviceType.getDevice());
|
||||
setLoginUser(loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户数据
|
||||
* 设置用户数据(多级缓存)
|
||||
*/
|
||||
public static void setLoginUser(LoginUser loginUser) {
|
||||
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
|
||||
LOGIN_CACHE.set(loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户
|
||||
**/
|
||||
* 获取用户(多级缓存)
|
||||
*/
|
||||
public static LoginUser getLoginUser() {
|
||||
LoginUser loginUser = LOGIN_CACHE.get();
|
||||
if (loginUser != null) {
|
||||
return loginUser;
|
||||
}
|
||||
return (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除一级缓存 防止内存问题
|
||||
*/
|
||||
public static void clearCache() {
|
||||
LOGIN_CACHE.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户id
|
||||
*/
|
||||
@ -63,13 +77,13 @@ public class LoginUtils {
|
||||
LoginUser loginUser = getLoginUser();
|
||||
if (ObjectUtil.isNull(loginUser)) {
|
||||
String loginId = StpUtil.getLoginIdAsString();
|
||||
String userId;
|
||||
String replace = "";
|
||||
if (StringUtils.contains(loginId, UserType.SYS_USER.getUserType())) {
|
||||
userId = StringUtils.replace(loginId, UserType.SYS_USER.getUserType(), replace);
|
||||
} else if (StringUtils.contains(loginId, UserType.APP_USER.getUserType())) {
|
||||
userId = StringUtils.replace(loginId, UserType.APP_USER.getUserType(), replace);
|
||||
} else {
|
||||
String userId = null;
|
||||
for (UserType value : UserType.values()) {
|
||||
if (StringUtils.contains(loginId, value.getUserType())) {
|
||||
userId = StringUtils.replace(loginId, value.getUserType(), StringUtils.EMPTY);
|
||||
}
|
||||
}
|
||||
if (StringUtils.isBlank(userId)) {
|
||||
throw new UtilException("登录用户: LoginId异常 => " + loginId);
|
||||
}
|
||||
return Long.parseLong(userId);
|
||||
@ -79,14 +93,14 @@ public class LoginUtils {
|
||||
|
||||
/**
|
||||
* 获取部门ID
|
||||
**/
|
||||
*/
|
||||
public static Long getDeptId() {
|
||||
return getLoginUser().getDeptId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户账户
|
||||
**/
|
||||
*/
|
||||
public static String getUsername() {
|
||||
return getLoginUser().getUsername();
|
||||
}
|
||||
@ -96,17 +110,7 @@ public class LoginUtils {
|
||||
*/
|
||||
public static UserType getUserType() {
|
||||
String loginId = StpUtil.getLoginIdAsString();
|
||||
return getUserType(loginId);
|
||||
}
|
||||
|
||||
public static UserType getUserType(Object loginId) {
|
||||
if (StringUtils.contains(loginId.toString(), UserType.SYS_USER.getUserType())) {
|
||||
return UserType.SYS_USER;
|
||||
} else if (StringUtils.contains(loginId.toString(), UserType.APP_USER.getUserType())) {
|
||||
return UserType.APP_USER;
|
||||
} else {
|
||||
throw new UtilException("登录用户: LoginId异常 => " + loginId);
|
||||
}
|
||||
return UserType.getUserType(loginId);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user