cloudroam
2025-03-10 c306cba52bcc3e2c423f77d4a52c35ad04c52038
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
72
73
74
75
76
77
78
79
80
81
82
83
package com.jsh.erp.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
 
public class ResponseJsonUtil {
    public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
 
    static {
        FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }
 
    /**
     * 响应过滤器
     */
    public static final class ResponseFilter extends ExtJsonUtils.ExtFilter implements ValueFilter {
        @Override
        public Object process(Object object, String name, Object value) {
            if (name.equals("createTime") || name.equals("modifyTime")||name.equals("updateTime")) {
                return value;
            } else if (value instanceof Date) {
                return FORMAT.format(value);
            } else {
                return value;
            }
        }
    }
 
    /**
     *
     * @param responseCode
     * @return
     */
    public static String backJson4HttpApi(ResponseCode responseCode) {
        if (responseCode != null) {
            String result = JSON.toJSONString(responseCode, new ResponseFilter(),
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNonStringKeyAsString);
            result = result.replaceFirst("\"data\":\\{", "");
            return result.substring(0, result.length() - 1);
        }
        return null;
    }
 
    /**
     * 验证失败的json串
     * @param code
     * @return
     */
    public static String backJson4VerifyFailure(int code) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", "未通过验证");
        return JSON.toJSONString(new ResponseCode(code, map), new ResponseFilter(),
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteNonStringKeyAsString);
    }
 
    /**
     * 成功的json串
     * @param responseCode
     * @return
     */
    public static String backJson(ResponseCode responseCode) {
        if (responseCode != null) {
            return JSON.toJSONString(responseCode, new ResponseFilter(),
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNonStringKeyAsString);
        }
        return null;
    }
 
    public static String returnJson(Map<String, Object> map, String message, int code) {
        map.put("message", message);
        return backJson(new ResponseCode(code, map));
    }
}