Jeecg-boot字典翻译改造

一.找到字典切面类(DictAspect)
二.改造方法(parseDictText)
三.修改后的parseDictText方法,支持IPage、List、Object

private void parseDictText(Object result) {
        if (result instanceof Result) {
            List<Object> list = new LinkedList<>();
            if (((Result) result).getResult() instanceof IPage) {
                //分页
                list = ((IPage) ((Result) result).getResult()).getRecords();
            } else if (((Result) result).getResult() instanceof List) {
                //List集合
                list = (List<Object>) ((Result) result).getResult();
            }else{
                //单对象
                Object record = ((Result) result).getResult();
                //判断能否转换成JSON,因为有些结果集返回的是String类型,导致翻译异常,因此判断是否可以转换json
                if(checkIsJsonStr(record)){
                    //字典翻译
                    record =  this.dictEscape(record);
                }
                ((Result) result).setResult(record);
            }
            if(list != null && list.size() > 0){
                List<Object> items = new ArrayList<>();
                for(Object record : list){
                    if(checkIsJsonStr(record)){
                        //字典翻译
                        record =  this.dictEscape(record);
                    }
                    items.add(record);
                }
                if (((Result) result).getResult() instanceof IPage) {
                    ((IPage) ((Result) result).getResult()).setRecords(items);
                } else if (((Result) result).getResult() instanceof List) {
                    ((Result) result).setResult(items);
                }
            }
        }
    }

四.提取公共代码作为单独的方法进行翻译

/**
     * 字典翻译
     * @param record
     * @return
     */
    private JSONObject dictEscape(Object record){
        ObjectMapper mapper = new ObjectMapper();
        String json = "{}";
        JSONObject item = null;
        try {
            //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
            json = mapper.writeValueAsString(record);//对象序列化为JSON字符串
        } catch (JsonProcessingException e) {
            log.error("json解析失败" + e.getMessage(), e);
        }
        try {
            item = JSONObject.parseObject(json);
            //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
            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 = key;
                    //非中文时翻译
                    if(!checkCountName(key)){
                        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()))));
                }
            }
        }catch (Exception e){
            log.info("字典翻译异常:"+e.getMessage(),e);
        }
        return item;
    }

五.增加中文检测方法

/**
     * 检测是否是中文
     * @param countName
     * @return
     */
    public static boolean checkCountName(String countName){
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(countName);
        if (m.find()) {
            return true;
        }
        return false;
    }

六.增加检测是否可转换为JSON字符串方法

/**
     * 检测是否可转换为JSON字符串
     * @param record
     * @return
     */
	public static boolean checkIsJsonStr(Object record){
        boolean jsonFlag = false;
        try {
            String json = new ObjectMapper().writeValueAsString(record);
            if(json.startsWith("{")) {
                jsonFlag = true;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonFlag;
    }

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值