fix:自动获取userName修改
This commit is contained in:
@@ -6,7 +6,6 @@ import com.sdm.common.entity.resp.system.CIDUserResp;
|
||||
import com.sdm.common.service.UserNameCacheService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
@@ -28,7 +27,16 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class converterType) {
|
||||
// 只处理RestController的方法
|
||||
Class<?> controllerClass = returnType.getContainingClass();
|
||||
String className = controllerClass.getSimpleName();
|
||||
String fullClassName = controllerClass.getName();
|
||||
|
||||
// 排除SysUserController
|
||||
if ("SysUserController".equals(className) || fullClassName.contains("SysUserController")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 只处理RestController的方法
|
||||
return returnType.getContainingClass().isAnnotationPresent(RestController.class);
|
||||
}
|
||||
|
||||
@@ -79,14 +87,137 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
Object data = dataField.get(sdmResponse);
|
||||
|
||||
if (data != null) {
|
||||
// 递归处理data字段
|
||||
processBody(data);
|
||||
// 递归处理data字段 兼容处理双重嵌套的data结构
|
||||
processDoubleNestedData(data);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("解析SdmResponse的data字段失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理双重嵌套的data结构
|
||||
*/
|
||||
private void processDoubleNestedData(Object outerData) {
|
||||
if (outerData == null) return;
|
||||
|
||||
try {
|
||||
// 检查外层data是否还有内层data字段
|
||||
if (hasDataField(outerData)) {
|
||||
Object innerData = getDataValue(outerData);
|
||||
|
||||
if (innerData != null) {
|
||||
// 处理内层data
|
||||
processInnerData(innerData);
|
||||
}
|
||||
} else {
|
||||
// 如果没有内层data,直接处理外层data
|
||||
processBody(outerData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("处理双重嵌套data结构失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取data字段或data键的值
|
||||
*/
|
||||
private Object getDataValue(Object obj) {
|
||||
if (obj == null) return null;
|
||||
|
||||
try {
|
||||
// 情况1:如果是Map,通过get方法获取
|
||||
if (obj instanceof Map) {
|
||||
Map<?, ?> map = (Map<?, ?>) obj;
|
||||
return map.get("data");
|
||||
}
|
||||
|
||||
// 情况2:如果是普通对象,通过反射获取字段值
|
||||
Field dataField = obj.getClass().getDeclaredField("data");
|
||||
dataField.setAccessible(true);
|
||||
return dataField.get(obj);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("获取data值失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有data字段或data键
|
||||
*/
|
||||
private boolean hasDataField(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
// 情况1:如果是Map类型,检查是否有"data"键
|
||||
if (obj instanceof Map) {
|
||||
Map<?, ?> map = (Map<?, ?>) obj;
|
||||
return map.containsKey("data");
|
||||
}
|
||||
// 情况2:如果是普通Java对象,检查是否有data字段
|
||||
try {
|
||||
Field dataField = obj.getClass().getDeclaredField("data");
|
||||
return dataField != null;
|
||||
} catch (NoSuchFieldException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理内层data
|
||||
*/
|
||||
private void processInnerData(Object innerData) {
|
||||
if (innerData == null) return;
|
||||
|
||||
try {
|
||||
// 内层data可能是列表、分页或单个对象
|
||||
if (innerData instanceof Collection) {
|
||||
((Collection<?>) innerData).forEach(this::processBody);
|
||||
} else if (innerData instanceof Page) {
|
||||
((Page<?>) innerData).getRecords().forEach(this::processBody);
|
||||
} else if (hasRecordsField(innerData)) {
|
||||
// 处理分页结构的records字段
|
||||
processRecordsField(innerData);
|
||||
} else {
|
||||
// 单个对象
|
||||
processBody(innerData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("处理内层data失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有records字段(分页结构)
|
||||
*/
|
||||
private boolean hasRecordsField(Object data) {
|
||||
try {
|
||||
Field recordsField = data.getClass().getDeclaredField("records");
|
||||
return recordsField != null;
|
||||
} catch (NoSuchFieldException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理records字段(分页数据)
|
||||
*/
|
||||
private void processRecordsField(Object data) {
|
||||
try {
|
||||
Field recordsField = data.getClass().getDeclaredField("records");
|
||||
recordsField.setAccessible(true);
|
||||
Object records = recordsField.get(data);
|
||||
|
||||
if (records instanceof Collection) {
|
||||
((Collection<?>) records).forEach(this::processBody);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("处理records字段失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断是否是SdmResponse类型
|
||||
*/
|
||||
@@ -137,7 +268,7 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
* 递归处理children字段
|
||||
*/
|
||||
private void processChildrenField(Object obj) throws Exception {
|
||||
Field[] fields = obj.getClass().getDeclaredFields();
|
||||
List<Field> fields = getAllFields(obj);
|
||||
|
||||
for (Field field : fields) {
|
||||
if ("children".equals(field.getName())) {
|
||||
@@ -156,7 +287,7 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
private Set<Long> collectUserIds(Object obj) throws Exception {
|
||||
Set<Long> userIds = new HashSet<>();
|
||||
Field[] fields = obj.getClass().getDeclaredFields();
|
||||
List<Field> fields = getAllFields(obj);
|
||||
|
||||
for (Field field : fields) {
|
||||
if (isUserIdField(field)) {
|
||||
@@ -169,15 +300,29 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
return userIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象的所有字段(包括父类)
|
||||
*/
|
||||
private List<Field> getAllFields(Object obj) {
|
||||
List<Field> fields = new ArrayList<>();
|
||||
Class<?> clazz = obj.getClass();
|
||||
|
||||
// 递归获取所有父类的字段
|
||||
while (clazz != null && clazz != Object.class) {
|
||||
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
private boolean isUserIdField(Field field) {
|
||||
String fieldName = field.getName();
|
||||
return field.getType() == Long.class &&
|
||||
("creator".equals(fieldName) || "updater".equals(fieldName)
|
||||
|| "userId".equals(fieldName));
|
||||
return "creator".equals(fieldName) || "updater".equals(fieldName) || "userId".equals(fieldName);
|
||||
}
|
||||
|
||||
private void setUserNamesToObject(Object obj, Map<Long, String> userNames) throws Exception {
|
||||
Field[] fields = obj.getClass().getDeclaredFields();
|
||||
List<Field> fields = getAllFields(obj);
|
||||
|
||||
for (Field field : fields) {
|
||||
if (isUserIdField(field)) {
|
||||
@@ -188,7 +333,7 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
// 设置对应的name字段
|
||||
String nameFieldName = field.getName() + "Name";
|
||||
try {
|
||||
Field nameField = obj.getClass().getDeclaredField(nameFieldName);
|
||||
Field nameField = getFieldRecursively(obj.getClass(), nameFieldName);
|
||||
nameField.setAccessible(true);
|
||||
nameField.set(obj, userName);
|
||||
} catch (NoSuchFieldException e) {
|
||||
@@ -200,38 +345,22 @@ public class UserNameResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 批量获取用户名,减少接口调用次数
|
||||
// */
|
||||
// @Cacheable(value = "userNames", key = "T(com.sdm.common.common.UserNameResponseAdvice).generateKey(#userIds)")
|
||||
// protected Map<Long, String> batchGetUserNames(Set<Long> userIds) {
|
||||
// log.info("【缓存未命中】批量查询用户名,用户数量: {}", userIds.size());
|
||||
//
|
||||
// // 批量调用用户服务
|
||||
// SdmResponse<List<CIDUserResp>> response = sysUserFeignClient.listUserByIds(
|
||||
// UserQueryReq.builder().userIds(new ArrayList<>(userIds)).build()
|
||||
// );
|
||||
//
|
||||
// Map<Long, String> userMap = response.getData().stream()
|
||||
// .collect(Collectors.toMap(
|
||||
// CIDUserResp::getUserId,
|
||||
// CIDUserResp::getNickname
|
||||
// ));
|
||||
//
|
||||
// return userMap;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 生成缓存key的静态方法
|
||||
// */
|
||||
// public static String generateKey(Set<Long> userIds) {
|
||||
// if (userIds == null || userIds.isEmpty()) {
|
||||
// return "empty";
|
||||
// }
|
||||
// // 排序后拼接成字符串
|
||||
// List<Long> sortedList = new ArrayList<>(userIds);
|
||||
// Collections.sort(sortedList);
|
||||
// return sortedList.toString(); // 例如: "[1, 2, 3]"
|
||||
// }
|
||||
/**
|
||||
* 递归获取字段(包括父类)
|
||||
*/
|
||||
private Field getFieldRecursively(Class<?> clazz, String fieldName) throws NoSuchFieldException {
|
||||
try {
|
||||
// 在当前类中查找
|
||||
return clazz.getDeclaredField(fieldName);
|
||||
} catch (NoSuchFieldException e) {
|
||||
// 如果在当前类中没找到,递归到父类中查找
|
||||
Class<?> superClass = clazz.getSuperclass();
|
||||
if (superClass != null && superClass != Object.class) {
|
||||
return getFieldRecursively(superClass, fieldName);
|
||||
} else {
|
||||
throw new NoSuchFieldException("字段 '" + fieldName + "' 在类 " + clazz.getName() + " 及其父类中未找到");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,10 @@ public class BaseEntity extends BaseBean {
|
||||
|
||||
@Schema(description = "更新者ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
public long updater;
|
||||
public Long updater;
|
||||
|
||||
@Schema(description = "更新者名称")
|
||||
public String updateName;
|
||||
public String updaterName;
|
||||
|
||||
@Schema(description = "更新时间")
|
||||
public String updateTime;
|
||||
@@ -31,7 +31,8 @@ public class BaseEntity extends BaseBean {
|
||||
|
||||
@Schema(description = "创建者ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
public long creator;
|
||||
public Long creator;
|
||||
public String creatorName;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
public String createTime;
|
||||
|
||||
Reference in New Issue
Block a user