feat: 完善后端架构和service层实现
- 创建完整的entity实体类体系,包括所有业务实体 - 实现BaseEntity基类,统一管理公共字段 - 创建雪花算法ID生成器和自动填充处理器 - 简化所有mapper接口,只继承BaseMapper - 重构service层,使用LambdaQueryWrapper进行数据库操作 - 创建BasePageRequest分页查询基类 - 完善用户上下文管理和JWT认证 - 新增WebSocket聊天功能和相关控制器 - 更新前端配置和组件,完善用户认证流程 - 同步数据库建表脚本
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
package com.emotion.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 用户上下文工具类
|
||||
* 提供便捷的用户上下文操作方法
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Slf4j
|
||||
public class UserContextUtils {
|
||||
|
||||
/**
|
||||
* 获取当前用户ID,如果为空则返回默认值
|
||||
*
|
||||
* @param defaultValue 默认值
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getCurrentUserIdOrDefault(String defaultValue) {
|
||||
String userId = UserContextHolder.getCurrentUserId();
|
||||
return userId != null ? userId : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户ID,如果为空则返回"system"
|
||||
*
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getCurrentUserIdOrSystem() {
|
||||
return getCurrentUserIdOrDefault("system");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户名,如果为空则返回默认值
|
||||
*
|
||||
* @param defaultValue 默认值
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getCurrentUsernameOrDefault(String defaultValue) {
|
||||
String username = UserContextHolder.getCurrentUsername();
|
||||
return username != null ? username : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户名,如果为空则返回"guest"
|
||||
*
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getCurrentUsernameOrGuest() {
|
||||
return getCurrentUsernameOrDefault("guest");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户类型,如果为空则返回默认值
|
||||
*
|
||||
* @param defaultValue 默认值
|
||||
* @return 用户类型
|
||||
*/
|
||||
public static String getCurrentUserTypeOrDefault(String defaultValue) {
|
||||
String userType = UserContextHolder.getCurrentUserType();
|
||||
return userType != null ? userType : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户类型,如果为空则返回"GUEST"
|
||||
*
|
||||
* @return 用户类型
|
||||
*/
|
||||
public static String getCurrentUserTypeOrGuest() {
|
||||
return getCurrentUserTypeOrDefault("GUEST");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端IP,如果为空则返回默认值
|
||||
*
|
||||
* @param defaultValue 默认值
|
||||
* @return 客户端IP
|
||||
*/
|
||||
public static String getClientIpOrDefault(String defaultValue) {
|
||||
String clientIp = UserContextHolder.getClientIp();
|
||||
return clientIp != null ? clientIp : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端IP,如果为空则返回"unknown"
|
||||
*
|
||||
* @return 客户端IP
|
||||
*/
|
||||
public static String getClientIpOrUnknown() {
|
||||
return getClientIpOrDefault("unknown");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求ID,如果为空则返回默认值
|
||||
*
|
||||
* @param defaultValue 默认值
|
||||
* @return 请求ID
|
||||
*/
|
||||
public static String getRequestIdOrDefault(String defaultValue) {
|
||||
String requestId = UserContextHolder.getRequestId();
|
||||
return requestId != null ? requestId : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求ID,如果为空则返回"unknown"
|
||||
*
|
||||
* @return 请求ID
|
||||
*/
|
||||
public static String getRequestIdOrUnknown() {
|
||||
return getRequestIdOrDefault("unknown");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前用户是否为访客
|
||||
*
|
||||
* @return 是否为访客
|
||||
*/
|
||||
public static boolean isGuest() {
|
||||
String userId = UserContextHolder.getCurrentUserId();
|
||||
String userType = UserContextHolder.getCurrentUserType();
|
||||
|
||||
return userId == null ||
|
||||
userId.startsWith("guest_") ||
|
||||
"GUEST".equalsIgnoreCase(userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前用户是否为系统用户
|
||||
*
|
||||
* @return 是否为系统用户
|
||||
*/
|
||||
public static boolean isSystem() {
|
||||
String userId = UserContextHolder.getCurrentUserId();
|
||||
String userType = UserContextHolder.getCurrentUserType();
|
||||
|
||||
return "system".equals(userId) ||
|
||||
"SYSTEM".equalsIgnoreCase(userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前用户是否为注册用户
|
||||
*
|
||||
* @return 是否为注册用户
|
||||
*/
|
||||
public static boolean isRegisteredUser() {
|
||||
return !isGuest() && !isSystem();
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地执行需要用户上下文的操作
|
||||
* 如果没有用户上下文,会设置默认的系统上下文
|
||||
*
|
||||
* @param operation 操作
|
||||
*/
|
||||
public static void executeWithContext(Runnable operation) {
|
||||
boolean hasContext = UserContextHolder.hasUserContext();
|
||||
|
||||
try {
|
||||
// 如果没有用户上下文,设置默认的系统上下文
|
||||
if (!hasContext) {
|
||||
UserContextHolder.setUserContext("system", "system", "SYSTEM", "127.0.0.1", "system");
|
||||
log.debug("设置默认系统用户上下文");
|
||||
}
|
||||
|
||||
// 执行操作
|
||||
operation.run();
|
||||
|
||||
} finally {
|
||||
// 如果是我们设置的默认上下文,执行后清理
|
||||
if (!hasContext) {
|
||||
UserContextHolder.clear();
|
||||
log.debug("清理默认系统用户上下文");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时设置用户上下文执行操作
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param username 用户名
|
||||
* @param userType 用户类型
|
||||
* @param operation 操作
|
||||
*/
|
||||
public static void executeWithTempContext(String userId, String username, String userType, Runnable operation) {
|
||||
// 保存当前上下文
|
||||
String originalUserId = UserContextHolder.getCurrentUserId();
|
||||
String originalUsername = UserContextHolder.getCurrentUsername();
|
||||
String originalUserType = UserContextHolder.getCurrentUserType();
|
||||
String originalClientIp = UserContextHolder.getClientIp();
|
||||
String originalRequestId = UserContextHolder.getRequestId();
|
||||
|
||||
try {
|
||||
// 设置临时上下文
|
||||
UserContextHolder.setUserContext(userId, username, userType,
|
||||
originalClientIp != null ? originalClientIp : "127.0.0.1",
|
||||
originalRequestId != null ? originalRequestId : "temp");
|
||||
|
||||
// 执行操作
|
||||
operation.run();
|
||||
|
||||
} finally {
|
||||
// 恢复原始上下文
|
||||
if (originalUserId != null || originalUsername != null) {
|
||||
UserContextHolder.setUserContext(originalUserId, originalUsername, originalUserType,
|
||||
originalClientIp, originalRequestId);
|
||||
} else {
|
||||
UserContextHolder.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user