18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.yzh.commons.model;
 
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.yzh.commons.util.Exceptions;
import org.yzh.commons.util.JsonUtils;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
@ToString
@Data
@Accessors(chain = true)
public class R<T> {
 
    public static final R SUCCESS = new ImmutableR<>(R.success());
    public static final R Unauthorized = new ImmutableR<>(R.error(APICodes.Unauthorized));
    public static final R NonPermission = new ImmutableR<>(R.error(APICodes.NonPermission));
 
    public interface View {
    }
 
    @JsonView(View.class)
    @Schema(description = "响应码(成功:200;客户端错误:400-499;服务端错误:500-599)")
    protected int code;
    @JsonView(View.class)
    @Schema(description = "响应消息")
    protected String msg;
    @JsonView(View.class)
    @Schema(description = "响应消息详情")
    protected String details;
    @JsonView(View.class)
    @Schema(description = "响应数据")
    protected T data;
 
    public R() {
    }
 
    public R(int code, T data, String msg, String details) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.details = details;
    }
 
    public static <T> R<T> success() {
        return new R<>(APICodes.Success.code, null, null, null);
    }
 
    public static <T> R<T> success(T data) {
        return new R<>(APICodes.Success.code, data, null, null);
    }
 
    public static <T> R<T> success(T data, String msg) {
        return new R<>(APICodes.Success.code, data, msg, null);
    }
 
    public static <T> R<T> successMsg(String msg) {
        return new R<>(APICodes.Success.code, null, msg, null);
    }
 
    public static <T> R<T> error() {
        return new R<>(APICodes.InternalError.code, null, "内部错误", null);
    }
 
    public static <T> R<T> error(Throwable e) {
        return new R<>(APICodes.InternalError.code, null, e.getMessage(), Exceptions.getStackTrace(e));
    }
 
    public static <T> R<T> error(String msg) {
        return new R<>(APICodes.InternalError.code, null, msg, null);
    }
 
    public static <T> R<T> error(String msg, Throwable e) {
        return new R<>(APICodes.InternalError.code, null, msg, Exceptions.getStackTrace(e));
    }
 
    public static <T> R<T> error(int code) {
        return new R<>(code, null, null, null);
    }
 
    public static <T> R<T> error(int code, String msg) {
        return new R<>(code, null, msg, null);
    }
 
    public static <T> R<T> error(APICode apiCode) {
        return new R<>(apiCode.getCode(), null, apiCode.getMessage(), null);
    }
 
    public static <T> R<T> error(APICode apiCode, String msg) {
        return new R<>(apiCode.getCode(), null, msg, null);
    }
 
    public static <T> R<T> error(APICode apiCode, Throwable e) {
        return new R<>(apiCode.getCode(), null, apiCode.getMessage(), Exceptions.getStackTrace(e));
    }
 
    public static <T> R<T> error(APICode apiCode, String msg, Throwable e) {
        return new R<>(apiCode.getCode(), null, msg, Exceptions.getStackTrace(e));
    }
 
    public boolean isSuccess() {
        return APICodes.Success.getCode() == code;
    }
 
    public String toJson() {
        return JsonUtils.toJson(this);
    }
 
    public static final class ImmutableR<T> extends R<T> {
 
        public ImmutableR(R<T> that) {
            this.code = that.code;
            this.msg = that.msg;
            this.details = that.details;
            this.data = that.data;
        }
 
        @Override
        public ImmutableR<T> setCode(int code) {
            throw new UnsupportedOperationException();
        }
 
        @Override
        public ImmutableR<T> setMsg(String msg) {
            throw new UnsupportedOperationException();
        }
 
        @Override
        public ImmutableR<T> setDetails(String details) {
            throw new UnsupportedOperationException();
        }
 
        @Override
        public ImmutableR<T> setData(T data) {
            throw new UnsupportedOperationException();
        }
    }
}