mitao
2024-06-06 3d2b51ea4520533de5e78f88dddf5b5c7dce4247
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
package com.sinata.rest.common;
 
import com.sinata.rest.common.i18n.I18nUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
 
@Data
@ApiModel("API接口工具类")
public class ApiUtils<T> {
 
    /**
     * 返回参数代码
     */
    public static final int CODE_OK = 0;
    public static final String MESSAGE_OK = "message.ok";
    public static final int CODE_NG = 1;
    public static final String MESSAGE_NG = "message.error";
 
    /**
     * 基础参数
     */
    @ApiModelProperty("标识码:0成功,1失败,其他自定义")
    public int code;
    @ApiModelProperty("提示消息")
    public String message;
 
    /**
     * 扩展参数
     */
    @ApiModelProperty("返回数据")
    public T data;
 
    public ApiUtils() {
        this.code = CODE_OK;
    }
 
    public ApiUtils(Integer code) {
        this.code = code;
    }
 
    public ApiUtils(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
 
    public ApiUtils(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
 
    /**
     * 国际化数据映射消息
     *
     * @param message
     */
    public void setI8nMessage(String message) {
        this.message = I18nUtils.getMessage(message);
    }
 
    /**
     * 封装接口返回数据
     */
    public static <T> ApiUtils<T> returnOK() {
        return new ApiUtils(CODE_OK, I18nUtils.getMessage(MESSAGE_OK));
    }
 
    public static <T> ApiUtils<T> returnOK(T data) {
        return new ApiUtils(CODE_OK, I18nUtils.getMessage(MESSAGE_OK), data);
    }
 
    public static <T> ApiUtils<T> returnOK(T data, String message) {
        return new ApiUtils(CODE_OK, message, data);
    }
 
    public static <T> ApiUtils<T> returnOK(T data, String message, Integer code) {
        return new ApiUtils(code, message, data);
    }
 
    /**
     * 封装接口返回数据(失败)
     */
    public static <T> ApiUtils<T> returnNG() {
        return new ApiUtils(CODE_NG, I18nUtils.getMessage(MESSAGE_NG));
    }
 
    public static <T> ApiUtils<T> returnNG(T data) {
        return new ApiUtils(CODE_NG, I18nUtils.getMessage(MESSAGE_NG), data);
    }
 
    public static <T> ApiUtils<T> returnNG(T data, String message) {
        return new ApiUtils(CODE_NG, message, data);
    }
 
    public static <T> ApiUtils<T> returnNG(T data, String message, Integer code) {
        return new ApiUtils(code, message, data);
    }
 
}