WebResult.java
2.52 KB
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
package com.dituhui.pea.pojo;
import com.dituhui.pea.enums.StatusCodeEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 结果封装类
* @author zl
*
*/
@Data
@Accessors(chain = true)
public class WebResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功标志
*/
private Boolean success;
/**
* 消息
*/
private String message;
/**
* 返回代码
*/
private String code;
/**
* 请求轨迹id
*/
private String traceId;
/**
* 时间戳
*/
private Long timestamp = System.currentTimeMillis();
/**
* 结果对象
*/
private T result;
public static <T> WebResult<T> ok() {
return restResult(StatusCodeEnum.SUCCESS, null);
}
public static <T> WebResult<T> ok(T result) {
return restResult(StatusCodeEnum.SUCCESS, result);
}
public static <T> WebResult<T> failed() {
return restResult(StatusCodeEnum.FAILED, null);
}
public static <T> WebResult<T> failed(T result, String code, String msg, Boolean success) {
return restResult(result, code, msg, success);
}
public static <T> WebResult<T> failed(StatusCodeEnum codeEnum) {
return restResult(codeEnum, null);
}
private static <T> WebResult<T> restResult(StatusCodeEnum codeEnum, T result) {
WebResult<T> apiResult = new WebResult<>();
apiResult.setResult(result);
// 用户信息
PlatformUser platformUser = CurrentUser.threadPlatformUser.get();
if (null != platformUser) {
apiResult.setTraceId(platformUser.getIdLog());
}
apiResult.setCode(codeEnum.getCode());
apiResult.setMessage(codeEnum.getDesc());
apiResult.setTimestamp(System.currentTimeMillis());
apiResult.setSuccess(codeEnum.getSuccess());
return apiResult;
}
private static <T> WebResult<T> restResult(T result, String code, String msg, Boolean success) {
WebResult<T> apiResult = new WebResult<>();
apiResult.setResult(result);
// 用户信息
PlatformUser platformUser = CurrentUser.threadPlatformUser.get();
if (null != platformUser) {
apiResult.setTraceId(platformUser.getIdLog());
}
apiResult.setCode(code);
apiResult.setMessage(msg);
apiResult.setTimestamp(System.currentTimeMillis());
apiResult.setSuccess(success);
return apiResult;
}
}