xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
package com.monkeylessey.response;
 
import org.springframework.lang.NonNull;
 
import java.util.HashMap;
 
 
/**
 * 新的响应类
 * @author xp
 * @date 2022/12/2
 */
public class Result extends HashMap<String, Object> {
 
    private static final long serialVersionUID = 1L;
 
    private static final String TOTAL = "total";
 
    public static final String DATA = "data";
 
    private static final String MSG = "msg";
 
    private static final String CODE = "code";
 
    public Result() {
        put(MSG, "success");
        put(CODE, 200);
    }
 
    /**
     * 成功,啥也不做
     * @return
     */
    public static Result ok() {
        return new Result();
    }
 
    /**
     * 成功,自定义提示信息
     * @param msg
     * @return
     */
    public static Result ok(String msg) {
        Result result = new Result();
        result.put(MSG, msg);
        return result;
    }
 
    /**
     * 失败,啥也不做
     * @return
     */
    public static Result error() {
        Result result = new Result();
        result.put(CODE, 500);
        result.put(MSG, "操作失败");
        return result;
    }
 
    /**
     * 失败,自定义错误提示
     * @param msg
     * @return
     */
    public static Result error(String msg) {
        Result result = new Result();
        result.put(CODE, 500);
        result.put(MSG, msg);
        return result;
    }
 
    /**
     * 失败,自定义错误码
     * @param code
     * @return
     */
    public static Result error(@NonNull Integer code) {
        Result result = new Result();
        result.put(CODE, code);
        result.put(MSG, "操作失败");
        return result;
    }
 
    /**
     * 失败,自定义错误码和错误提示
     * @param code
     * @param msg
     * @return
     */
    public static Result error(@NonNull Integer code, String msg) {
        Result result = new Result();
        result.put(CODE, code);
        result.put(MSG, msg);
        return result;
    }
 
    /**
     * 添加数据
     * @param data
     * @return
     */
    public Result data(Object data) {
        this.put(DATA, data);
        return this;
    }
 
    /**
     * 添加总条数
     * @param total
     * @return
     */
    public Result total(long total) {
        this.put(TOTAL, total);
        return this;
    }
 
    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}