From 2c4bd9543b6988fa9fa1d72d44e6a72bda5bd4bb Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Thu, 14 Apr 2022 17:50:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=AD=97=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/aspect/DictAspect.java | 79 +++++++++++-------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java index 3ef2049df..83347d360 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java @@ -46,7 +46,7 @@ public class DictAspect { @Around("excudeService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { - long time1=System.currentTimeMillis(); + long time1=System.currentTimeMillis(); Object result = pjp.proceed(); long time2=System.currentTimeMillis(); log.debug("获取JSON数据 耗时:"+(time2-time1)+"ms"); @@ -84,46 +84,57 @@ public class DictAspect { if (((Result) result).getResult() instanceof IPage) { List items = new ArrayList<>(); for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) { - ObjectMapper mapper = new ObjectMapper(); - String json="{}"; - try { - //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat - json = mapper.writeValueAsString(record); - } catch (JsonProcessingException e) { - log.error("json解析失败"+e.getMessage(),e); - } - JSONObject item = JSONObject.parseObject(json); - //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ - //for (Field field : record.getClass().getDeclaredFields()) { - for (Field field : oConvertUtils.getAllFields(record)) { - //update-end--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ - 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())); - - //翻译字典值对应的txt - String textValue = translateDictValue(code, text, table, key); - - log.debug(" 字典Val : "+ textValue); - log.debug(" __翻译字典字段__ "+field.getName() + CommonConstant.DICT_TEXT_SUFFIX+": "+ textValue); - item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); - } - //date类型默认转换string格式化日期 - 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) item.get(field.getName())))); - } - } + JSONObject item = getJsonObject(record); items.add(item); } ((IPage) ((Result) result).getResult()).setRecords(items); + }else if(((Result) result).getResult() instanceof List){ + List items = new ArrayList<>(); + for (Object record : (List)((Result) result).getResult()) { + JSONObject item = getJsonObject(record); + items.add(item); + } + ((Result) result).setResult(items); } - } } + private JSONObject getJsonObject(Object record) { + ObjectMapper mapper = new ObjectMapper(); + String json = "{}"; + try { + //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat + json = mapper.writeValueAsString(record); + } catch (JsonProcessingException e) { + log.error("json解析失败" + e.getMessage(), e); + } + JSONObject item = JSONObject.parseObject(json); + //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ + //for (Field field : record.getClass().getDeclaredFields()) { + for (Field field : oConvertUtils.getAllFields(record)) { + //update-end--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ + 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())); + + //翻译字典值对应的txt + String textValue = translateDictValue(code, text, table, key); + + log.debug(" 字典Val : " + textValue); + log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue); + item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + } + //date类型默认转换string格式化日期 + 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) item.get(field.getName())))); + } + } + return item; + } + /** * 翻译字典文本 * @param code