123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- package com.zcxk.aspect;
- import com.alibaba.fastjson.JSONObject;
- import com.fasterxml.jackson.annotation.JsonFormat;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.zcxk.annotation.Dict;
- import com.zcxk.dict.DictUtil;
- import com.zcxk.model.AjaxMessage;
- import com.zcxk.model.Pagination;
- 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.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.lang.reflect.Field;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- @Aspect
- @Component
- @Slf4j
- public class DictAspect {
- private static final String DICT_TEXT_SUFFIX = "Dict";
- @Autowired
- private DictUtil dictUtil;
-
- @Pointcut("execution(public * com.zcxk..*.*Controller.*(..))")
- public void excudeService() {
- }
- @Around("excudeService()")
- public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
- long time1 = System.currentTimeMillis();
- Object result = pjp.proceed();
- long time2 = System.currentTimeMillis();
- log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");
- long start = System.currentTimeMillis();
- this.parseDictText(result);
- long end = System.currentTimeMillis();
- log.debug("解析注入JSON数据 耗时" + (end - start) + "ms");
- return result;
- }
-
- private void parseDictText(Object result) {
- if (!(result instanceof AjaxMessage)) {
- return;
- }
- if (!(((AjaxMessage) result).getData() instanceof Pagination)) {
- return;
- }
- List<JSONObject> items = new ArrayList<>();
- List dataList = ((Pagination) ((AjaxMessage) result).getData()).getList();
- for (Object record : dataList) {
- ObjectMapper mapper = new ObjectMapper();
- String json = "{}";
- try {
-
- json = mapper.writeValueAsString(record);
- } catch (JsonProcessingException e) {
- log.error("json解析失败" + e.getMessage(), e);
- }
- JSONObject item = JSONObject.parseObject(json);
- for (Field field : getAllFields(record)) {
- if (field.getAnnotation(Dict.class) != null) {
- String code = field.getAnnotation(Dict.class).dicCode();
- String text = field.getAnnotation(Dict.class).dicText();
- String table = field.getAnnotation(Dict.class).dictTable();
- String key = String.valueOf(item.get(field.getName()));
-
- String textValue = translateDictValue(code, text, table, key);
- log.debug(" 字典Val : " + textValue);
- log.debug(" __翻译字典字段__ " + field.getName() + DICT_TEXT_SUFFIX + ": " + textValue);
- item.put(field.getName() + DICT_TEXT_SUFFIX, textValue);
- }
-
- if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
- SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
- item.put(field.getName(), aDate.format(new Date(Long.valueOf(String.valueOf(item.get(field.getName()))))));
- }
- }
- items.add(item);
- }
- ((Pagination) ((AjaxMessage) result).getData()).setList(items);
- }
-
- private String translateDictValue(String code, String text, String table, String key) {
- if (isEmpty(key)) {
- return null;
- }
- StringBuffer textValue = new StringBuffer();
- String[] keys = key.split(",");
- for (String k : keys) {
- String tmpValue = null;
- log.debug(" 字典 key : " + k);
- if (k.trim().length() == 0) {
- continue;
- }
- tmpValue = dictUtil.getDictName(code, Integer.parseInt(k.trim()));
- if (tmpValue != null) {
- if (!"".equals(textValue.toString())) {
- textValue.append(",");
- }
- textValue.append(tmpValue);
- }
- }
- return textValue.toString();
- }
- private static Field[] getAllFields(Object object) {
- Class<?> clazz = object.getClass();
- List<Field> fieldList = new ArrayList<>();
- while (clazz != null) {
- fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
- clazz = clazz.getSuperclass();
- }
- Field[] fields = new Field[fieldList.size()];
- fieldList.toArray(fields);
- return fields;
- }
- public static boolean isEmpty(Object object) {
- if (object == null) {
- return (true);
- }
- if ("".equals(object)) {
- return (true);
- }
- if ("null".equals(object)) {
- return (true);
- }
- return (false);
- }
- }
|