UPDATE: 1、优化导出计划检测数据;2、删除导入和合并计划检测数据接口,改成导入并合并计划检测数据异步逻辑。
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package com.njcn.gather.system.config.handler;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.db.mybatisplus.handler.AutoFillValueHandler;
|
||||
import com.njcn.web.utils.RequestUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Primary
|
||||
@Component
|
||||
@Slf4j
|
||||
public class NonWebAutoFillValueHandler extends AutoFillValueHandler {
|
||||
|
||||
/**
|
||||
* 当前用户ID的线程本地变量,用于非Web环境
|
||||
*/
|
||||
private static final ThreadLocal<String> CURRENT_USER_ID = new ThreadLocal<>();
|
||||
|
||||
|
||||
@Override
|
||||
public Supplier<String> getUserIdSupplier() {
|
||||
return () -> {
|
||||
try {
|
||||
// 首先尝试从Web环境获取用户ID
|
||||
String userId = RequestUtil.getUserId();
|
||||
String actualUserId = StrUtil.isBlank(userId) ? "未知用户" : userId;
|
||||
return actualUserId;
|
||||
} catch (BusinessException e) {
|
||||
// 如果是"当前请求web环境为空"异常,则尝试从线程本地变量获取
|
||||
if (e.getMessage().contains("当前请求web环境为空")) {
|
||||
String userId = CURRENT_USER_ID.get();
|
||||
if (userId != null) {
|
||||
return userId;
|
||||
}
|
||||
// 如果线程本地变量中也没有用户ID,则返回默认值
|
||||
log.warn("无法获取当前用户ID");
|
||||
return "未知用户";
|
||||
}
|
||||
// 其他异常直接抛出
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 在非Web环境中设置当前用户ID
|
||||
*
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
public static void setCurrentUserId(String userId) {
|
||||
CURRENT_USER_ID.set(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前线程的用户ID设置
|
||||
*/
|
||||
public static void clearCurrentUserId() {
|
||||
CURRENT_USER_ID.remove();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user