1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package cn.yuencode.oisp.admin.aop;
import cn.yuencode.oisp.admin.annotation.DictValue; import cn.yuencode.oisp.common.security.util.DictUtils; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
@Aspect @Component @Slf4j public class DictAspect { @Pointcut("@annotation(cn.yuencode.oisp.admin.annotation.Dict)") public void dictPointCut() { }
@Around("dictPointCut()") public Object aroundMethod(ProceedingJoinPoint pjd) throws Throwable { Object result = null; try { result = pjd.proceed(); List r = (ArrayList) result; for (Object obj : r) { Arrays.stream(obj.getClass().getDeclaredFields()) .forEach(field -> { Object fieldValue = getFieldValue(obj,field); if (field.isAnnotationPresent(DictValue.class) && field.getType().equals(String.class)) { String dictType = field.getAnnotation(DictValue.class).dictType(); String value = field.getAnnotation(DictValue.class).value(); String dictLabel = DictUtils.getDictLabel(dictType, value.equals("") ? (String) fieldValue : value); setFieldValue(obj, field, dictLabel); } }); } } catch (Throwable e) { throw e; } return result; } private Object getFieldValue(Object obj, Field field) { try { field.setAccessible(true); return field.get(obj); } catch (Exception e) { } return null; }
private void setFieldValue(Object obj, Field field, Object value) { field.setAccessible(true); try { field.set(obj, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } }
|