公用
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
package com.ruoyi.common.handler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.annotation.DictConvert;
|
||||
import com.ruoyi.common.annotation.EnumsConvert;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.interfaces.EnumsInterface;
|
||||
import com.ruoyi.common.utils.DictUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 响应数据统一处理
|
||||
*
|
||||
* @Author wangqiong
|
||||
* @Date 2023/1/5
|
||||
* @Version V1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class ResponseBodyAdviceHandler implements ResponseBodyAdvice {
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Class aClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest request, ServerHttpResponse serverHttpResponse) {
|
||||
if (body instanceof String) {
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
try {
|
||||
return om.writeValueAsString(AjaxResult.success(body));
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("JsonProcessingException e:{}", e);
|
||||
}
|
||||
}
|
||||
//判断类型,然后转换字典
|
||||
turnType(body);
|
||||
AjaxResult result = AjaxResult.success(body);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断类型,然后转换字典
|
||||
*
|
||||
* @param body
|
||||
*/
|
||||
private void turnType(Object body) {
|
||||
if (body instanceof AjaxResult) {
|
||||
AjaxResult result = (AjaxResult) body;
|
||||
if(result.isSuccess()){
|
||||
Object data = result.get("data");
|
||||
if(data!=null){
|
||||
if(data instanceof List){
|
||||
List list = (List) data;
|
||||
convertList(list);
|
||||
}else if(data instanceof TableDataInfo){
|
||||
TableDataInfo pageRes = (TableDataInfo) data;
|
||||
List list = pageRes.getRows();
|
||||
convertList(list);
|
||||
}else{
|
||||
loopTurn(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (body instanceof TableDataInfo) {
|
||||
TableDataInfo pageRes = (TableDataInfo) body;
|
||||
List list = pageRes.getRows();
|
||||
convertList(list);
|
||||
} else if (body instanceof List) {
|
||||
List list = (List) body;
|
||||
convertList(list);
|
||||
} else {
|
||||
loopTurn(body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归转换列表数据,将列表套列表数据都进行转换
|
||||
*
|
||||
* @param list 列表数据
|
||||
*/
|
||||
private void convertList(List list) {
|
||||
if (list != null && list.size() > 0) {
|
||||
for (Object o : list) {
|
||||
if (o instanceof List) {
|
||||
convertList((List) o);
|
||||
} else {
|
||||
loopTurn(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归转换枚举、字典
|
||||
*
|
||||
* @param obj 属性
|
||||
*/
|
||||
private void loopTurn(Object obj) {
|
||||
if (obj != null) {
|
||||
Class clazz = obj.getClass();
|
||||
//获取类的所有字段
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
Object value = null;
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
//获取属性值
|
||||
value = field.get(obj);
|
||||
} catch (IllegalAccessException e) {
|
||||
log.error("turnDict getField Value error!", e);
|
||||
}
|
||||
//List属性递归每个子集
|
||||
if (value != null && value instanceof List) {
|
||||
for (Object child : (List) value) {
|
||||
loopTurn(child);
|
||||
}
|
||||
//非List进行显示转换
|
||||
} else {
|
||||
turnEnum(obj, clazz, field, value);
|
||||
turnDict(obj, clazz, field, value);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换枚举类型
|
||||
*
|
||||
* @param obj 属性判断
|
||||
*/
|
||||
private void turnEnum(Object obj, Class clazz, Field field, Object value) {
|
||||
EnumsConvert enumsConvert = field.getAnnotation(EnumsConvert.class);
|
||||
//判断是否有枚举转换的注解
|
||||
if (enumsConvert != null) {
|
||||
if (value != null && StrUtil.isNotEmpty(value.toString())) {
|
||||
String key = value.toString();
|
||||
String target = enumsConvert.targetField();
|
||||
|
||||
Field targetField = getTargetField(clazz, field, target);
|
||||
|
||||
try {
|
||||
if (targetField != null) {
|
||||
targetField.setAccessible(true);
|
||||
//给目标字段设置转换后的值
|
||||
targetField.set(obj, EnumsInterface.getText(enumsConvert.getEnumsInterface(), key));
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
log.error("turnEnum set value error!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典转换
|
||||
*
|
||||
* @param obj 转换对象
|
||||
*/
|
||||
private void turnDict(Object obj, Class clazz, Field field, Object value) {
|
||||
DictConvert dictConvert = field.getAnnotation(DictConvert.class);
|
||||
//判断是否有字典转换的注解
|
||||
if (dictConvert != null) {
|
||||
if (value != null && StrUtil.isNotEmpty(value.toString())) {
|
||||
String key = value.toString();
|
||||
String target = dictConvert.targetField();
|
||||
|
||||
Field targetField = getTargetField(clazz, field, target);
|
||||
|
||||
try {
|
||||
if (targetField != null) {
|
||||
targetField.setAccessible(true);
|
||||
String dictType = dictConvert.dictType();
|
||||
if(StrUtil.isEmpty(dictType)){
|
||||
return;
|
||||
}
|
||||
//单个字典时直接设置值
|
||||
if (!dictConvert.isMultiple()) {
|
||||
|
||||
targetField.set(obj, ObjectUtil.isNotEmpty( DictUtils.getDictLabel(dictType,key)) ? DictUtils.getDictLabel(dictType,key):null);
|
||||
} else {
|
||||
//多个字典时,用逗号拼接值
|
||||
List<String> textList = new ArrayList<>();
|
||||
String[] ids = key.split(dictConvert.separator());
|
||||
for (String id : ids) {
|
||||
if (ObjectUtil.isNotEmpty(DictUtils.getDictLabel(dictType,id))&&StrUtil.isNotEmpty(DictUtils.getDictLabel(dictType,id))) {
|
||||
textList.add(DictUtils.getDictLabel(dictType,id));
|
||||
}
|
||||
}
|
||||
targetField.set(obj, StrUtil.join(dictConvert.separator(), textList));
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
log.error("turnDict set value error!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取目标属性
|
||||
*
|
||||
* @param clazz 取值对象
|
||||
* @param field 属性
|
||||
* @param target 属性名
|
||||
* @return Field 目标属性
|
||||
*/
|
||||
private Field getTargetField(Class clazz, Field field, String target) {
|
||||
Field targetField = null;
|
||||
//转换的目标字段为当前字段
|
||||
if (StrUtil.isEmpty(target)) {
|
||||
targetField = field;
|
||||
} else {
|
||||
try {
|
||||
//目标字段为设置的字段
|
||||
targetField = clazz.getDeclaredField(target);
|
||||
} catch (NoSuchFieldException e) {
|
||||
log.error("getTargetField getDeclaredField error!", e);
|
||||
}
|
||||
}
|
||||
return targetField;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user