fwq
2022-06-17 32e62c64b2e384d11443cb4ae90177d5664dc8d8
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.hx.resultTool;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hx.exception.TipsException;
 
import java.io.Serializable;
 
/**
 * 统一返回格式
 * @author chenjiahe
 * @Data: 2020-06-20
 */
public class Result implements Serializable {
 
    private static final long serialVersionUID = -3948389268046368059L;
 
    private String code;
 
    private String msg;
 
    private Object data;
 
    public Result() {}
 
    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
 
    public static Result success() {
        Result Result = new Result();
        Result.setCode(ResponseCode.SUCCESS);
        Result.setMsg("SUCCESS");
        return Result;
    }
 
    public static Result success(Object data) {
        Result Result = new Result();
        Result.setCode(ResponseCode.SUCCESS);
        Result.setData(data);
        Result.setMsg("SUCCESS");
        return Result;
    }
 
    public static Result failure(String code, String msg) {
        Result Result = new Result();
        Result.setCode(code);
        Result.setMsg(msg);
        return Result;
    }
 
    public static Result failure(String code, String msg, Object data) {
        Result Result = new Result();
        Result.setCode(code);
        Result.setMsg(msg);
        Result.setData(data);
        return Result;
    }
 
    /**校验返回码*/
    public Boolean checkCode(){
        if(ResponseCode.SUCCESS.equals(code)){
            return true;
        }
        return false;
    }
 
    /**校验返回码,进行错误提示*/
    public void checkTips(){
        if(!ResponseCode.SUCCESS.equals(code)){
            throw new TipsException("请求失败:"+this.code+","+this.msg);
        }
    }
 
    /**返回数据转JSONObject*/
    public JSONObject getJsonObject(Object data){
        return JSONObject.parseObject(JSON.toJSONString(data));
    }
 
    /**返回数据转JSONArray*/
    public JSONArray getJsonArray(Object data){
        return JSONArray.parseArray(JSON.toJSONString(data));
    }
 
    /*******************************************************************************/
 
    public String getCode() {
        return this.code;
    }
 
    public void setCode(final String code) {
        this.code = code;
    }
 
    public String getMsg() {
        return this.msg;
    }
 
    public void setMsg(final String msg) {
        this.msg = msg;
    }
 
    public Object getData() {
        return this.data;
    }
 
    public void setData(final Object data) {
        this.data = data;
    }
 
}