公用
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.ruoyi.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
/**
|
||||
* 查询时字典转换注解 支持的返回类型:PageRes,List,VO类
|
||||
* @Author wangqiong
|
||||
* @Date 2023/01/5
|
||||
* @Version V1.0
|
||||
*/
|
||||
// 标注这个类它可以标注的位置
|
||||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
|
||||
// 标注这个注解的注解保留时期
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
// 是否生成注解文档
|
||||
@Documented
|
||||
public @interface DictConvert {
|
||||
//转换字典的目标字段,不填时默认为注解的字段本身
|
||||
String targetField() default "";
|
||||
//是否多选
|
||||
boolean isMultiple() default false;
|
||||
//分隔符 默认为逗号
|
||||
String separator() default ",";
|
||||
// 字典类型
|
||||
String dictType() default "";
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.ruoyi.common.annotation;
|
||||
|
||||
|
||||
import com.ruoyi.common.interfaces.EnumsInterface;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
/**
|
||||
* 枚举转义
|
||||
* @Author wangqiong
|
||||
* @Date 2023/01/5
|
||||
* @Version V1.0
|
||||
*/
|
||||
// 标注这个类它可以标注的位置
|
||||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
|
||||
// 标注这个注解的注解保留时期
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
// 是否生成注解文档
|
||||
@Documented
|
||||
public @interface EnumsConvert {
|
||||
Class<? extends EnumsInterface> getEnumsInterface();
|
||||
|
||||
//转换字典的目标字段,不填时默认为注解的字段本身
|
||||
String targetField() default "";
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.ruoyi.common.core.domain.entity;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
import com.ruoyi.common.annotation.DictConvert;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.annotation.Excel;
|
||||
@@ -64,7 +66,9 @@ public class SysUser extends BaseEntity
|
||||
|
||||
/** 用户性别 */
|
||||
@Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
|
||||
@DictConvert(targetField = "sexName",dictType = "sys_user_sex")
|
||||
private String sex;
|
||||
private String sexName;
|
||||
|
||||
/** 用户头像 */
|
||||
private String avatar;
|
||||
@@ -141,6 +145,14 @@ public class SysUser extends BaseEntity
|
||||
return deptId;
|
||||
}
|
||||
|
||||
public String getSexName() {
|
||||
return sexName;
|
||||
}
|
||||
|
||||
public void setSexName(String sexName) {
|
||||
this.sexName = sexName;
|
||||
}
|
||||
|
||||
public void setDeptId(Long deptId)
|
||||
{
|
||||
this.deptId = deptId;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.ruoyi.common.enums;
|
||||
|
||||
import com.ruoyi.common.interfaces.EnumsInterface;
|
||||
|
||||
/**
|
||||
* @author wangqiong
|
||||
* @description 是否枚举
|
||||
* @date 2023-11-17 14:05
|
||||
*/
|
||||
public enum YesOrNoEnum
|
||||
public enum YesOrNoEnum implements EnumsInterface
|
||||
{
|
||||
NO(0, "否"),
|
||||
YES(1, "是"),
|
||||
@@ -30,4 +32,32 @@ public enum YesOrNoEnum
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据code获取text
|
||||
* @param codeNo
|
||||
* @return
|
||||
*/
|
||||
public static String getTextByCode(Integer codeNo){
|
||||
for (YesOrNoEnum value : YesOrNoEnum.values()) {
|
||||
if (value.getCode().equals(codeNo)){
|
||||
return value.getText();
|
||||
}
|
||||
}
|
||||
return codeNo.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据text获取code
|
||||
* @param textStr
|
||||
* @return
|
||||
*/
|
||||
public static String getCodeByText(String textStr){
|
||||
for (YesOrNoEnum value : YesOrNoEnum.values()) {
|
||||
if (value.getText().equals(textStr)){
|
||||
return value.getText();
|
||||
}
|
||||
}
|
||||
return textStr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.ruoyi.common.interfaces;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 枚举接口,反射出枚举code对应的文本 或 文本对应的code
|
||||
*
|
||||
* @Author wangqiong
|
||||
* @Date 2023/01/5
|
||||
* @Version V1.0
|
||||
*/
|
||||
public interface EnumsInterface {
|
||||
static <T extends EnumsInterface>String getText(Class<T> clazz, String code){
|
||||
try {
|
||||
Method method = clazz.getMethod("getTextByCode", String.class);
|
||||
if(method!=null){
|
||||
return (String)method.invoke(null,code);
|
||||
}
|
||||
}catch (Exception e){
|
||||
System.err.println("getTextByCode方法不存在");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
static <T extends EnumsInterface>String getCode(Class<T> clazz,String text){
|
||||
try {
|
||||
Method method = clazz.getMethod("getCodeByText", String.class);
|
||||
if(method!=null){
|
||||
return (String)method.invoke(null,text);
|
||||
}
|
||||
}catch (Exception e){
|
||||
System.err.println("getCodeByText方法不存在");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user