DictAspect.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.zcxk.aspect;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fasterxml.jackson.annotation.JsonFormat;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.zcxk.annotation.Dict;
  7. import com.zcxk.dict.DictUtil;
  8. import com.zcxk.model.AjaxMessage;
  9. import com.zcxk.model.Pagination;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.aspectj.lang.ProceedingJoinPoint;
  12. import org.aspectj.lang.annotation.Around;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.Pointcut;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import java.lang.reflect.Field;
  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Date;
  22. import java.util.List;
  23. /**
  24. * @Description: 字典aop类
  25. * @Author: lihui
  26. * @Date: 2021-5-24
  27. * @Version: 1.0
  28. */
  29. @Aspect
  30. @Component
  31. @Slf4j
  32. public class DictAspect {
  33. private static final String DICT_TEXT_SUFFIX = "Dict";
  34. @Autowired
  35. private DictUtil dictUtil;
  36. /***
  37. * 定义切点Pointcut
  38. * @author lihui
  39. * @date 11:26 2021/5/24
  40. * @return void
  41. **/
  42. @Pointcut("execution(public * com.zcxk..*.*Controller.*(..))")
  43. public void excudeService() {
  44. }
  45. @Around("excudeService()")
  46. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  47. long time1 = System.currentTimeMillis();
  48. Object result = pjp.proceed();
  49. long time2 = System.currentTimeMillis();
  50. log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");
  51. long start = System.currentTimeMillis();
  52. this.parseDictText(result);
  53. long end = System.currentTimeMillis();
  54. log.debug("解析注入JSON数据 耗时" + (end - start) + "ms");
  55. return result;
  56. }
  57. /**
  58. * 本方法针对返回对象为Result 的IPage的分页列表数据进行动态字典注入
  59. * 字典注入实现 通过对实体类添加注解@dict 来标识需要的字典内容,字典分为单字典code即可 ,table字典 code table text配合使用与原来jeecg的用法相同
  60. * 示例为SysUser 字段为sex 添加了注解@Dict(dicCode = "sex") 会在字典服务立马查出来对应的text 然后在请求list的时候将这个字典text,已字段名称加_dict形式返回到前端
  61. * 例输入当前返回值的就会多出一个sex_dictText字段
  62. * {
  63. * sex:1,
  64. * sex_dictText:"男"
  65. * }
  66. * 前端直接取值sext_dict在table里面无需再进行前端的字典转换了
  67. * customRender:function (text) {
  68. * if(text==1){
  69. * return "男";
  70. * }else if(text==2){
  71. * return "女";
  72. * }else{
  73. * return text;
  74. * }
  75. * }
  76. * 目前vue是这么进行字典渲染到table上的多了就很麻烦了 这个直接在服务端渲染完成前端可以直接用
  77. * @param result
  78. */
  79. private void parseDictText(Object result) {
  80. if (!(result instanceof AjaxMessage)) {
  81. return;
  82. }
  83. if (!(((AjaxMessage) result).getData() instanceof Pagination)) {
  84. return;
  85. }
  86. List<JSONObject> items = new ArrayList<>();
  87. List dataList = ((Pagination) ((AjaxMessage) result).getData()).getList();
  88. for (Object record : dataList) {
  89. ObjectMapper mapper = new ObjectMapper();
  90. String json = "{}";
  91. try {
  92. //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
  93. json = mapper.writeValueAsString(record);
  94. } catch (JsonProcessingException e) {
  95. log.error("json解析失败" + e.getMessage(), e);
  96. }
  97. JSONObject item = JSONObject.parseObject(json);
  98. for (Field field : getAllFields(record)) {
  99. if (field.getAnnotation(Dict.class) != null) {
  100. String code = field.getAnnotation(Dict.class).dicCode();
  101. String text = field.getAnnotation(Dict.class).dicText();
  102. String table = field.getAnnotation(Dict.class).dictTable();
  103. String key = String.valueOf(item.get(field.getName()));
  104. //翻译字典值对应的txt
  105. String textValue = translateDictValue(code, text, table, key);
  106. log.debug(" 字典Val : " + textValue);
  107. log.debug(" __翻译字典字段__ " + field.getName() + DICT_TEXT_SUFFIX + ": " + textValue);
  108. item.put(field.getName() + DICT_TEXT_SUFFIX, textValue);
  109. }
  110. //date类型默认转换string格式化日期
  111. if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
  112. SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  113. //解决日期较小的情况下被解析为Integer,最后导致导致强转为Long的报错问题
  114. item.put(field.getName(), aDate.format(new Date(Long.valueOf(String.valueOf(item.get(field.getName()))))));
  115. }
  116. }
  117. items.add(item);
  118. }
  119. ((Pagination) ((AjaxMessage) result).getData()).setList(items);
  120. }
  121. /**
  122. * 翻译字典文本
  123. *
  124. * @param code
  125. * @param text
  126. * @param table
  127. * @param key
  128. * @return
  129. */
  130. private String translateDictValue(String code, String text, String table, String key) {
  131. if (isEmpty(key)) {
  132. return null;
  133. }
  134. StringBuffer textValue = new StringBuffer();
  135. String[] keys = key.split(",");
  136. for (String k : keys) {
  137. String tmpValue = null;
  138. log.debug(" 字典 key : " + k);
  139. if (k.trim().length() == 0) {
  140. continue;
  141. }
  142. tmpValue = dictUtil.getDictName(code, Integer.parseInt(k.trim()));
  143. if (tmpValue != null) {
  144. if (!"".equals(textValue.toString())) {
  145. textValue.append(",");
  146. }
  147. textValue.append(tmpValue);
  148. }
  149. }
  150. return textValue.toString();
  151. }
  152. private static Field[] getAllFields(Object object) {
  153. Class<?> clazz = object.getClass();
  154. List<Field> fieldList = new ArrayList<>();
  155. while (clazz != null) {
  156. fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
  157. clazz = clazz.getSuperclass();
  158. }
  159. Field[] fields = new Field[fieldList.size()];
  160. fieldList.toArray(fields);
  161. return fields;
  162. }
  163. public static boolean isEmpty(Object object) {
  164. if (object == null) {
  165. return (true);
  166. }
  167. if ("".equals(object)) {
  168. return (true);
  169. }
  170. if ("null".equals(object)) {
  171. return (true);
  172. }
  173. return (false);
  174. }
  175. }