R.java
4.67 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.glsx.vo;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* 返回数据
*/
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public final static String CODE_KEY = "code";
public final static String MSG_KEY = "message";
private final static String DATA_KEY = "data";
private final static String LIST_KEY = "list";
private final static String ITEMS_KEY = "items";
private final static String PAGE_KEY = "page";
private final static String TOTAL_PAGES_KEY = "totalPages";
private final static String TOTAL_ELEMENTS_KEY = "totalElements";
private final static int SUCCESS_CODE = 200;
public R() {
put(CODE_KEY, SUCCESS_CODE);
put(MSG_KEY, "success");
}
public static R error() {
return error(500, "未知异常,请联系管理员");
}
public static R error(String msg) {
return error(500, msg);
}
public static R error(int code, String msg) {
R r = new R();
r.put(CODE_KEY, code);
r.put(MSG_KEY, msg);
return r;
}
public static R error(String code, String msg) {
R r = new R();
r.put(CODE_KEY, code);
r.put(MSG_KEY, msg);
return r;
}
public static R ok(String msg) {
R r = new R();
r.put(MSG_KEY, msg);
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}
public static R ok() {
return new R();
}
public R data(Object o) {
super.put(DATA_KEY, o);
return this;
}
@Override
public R put(String key, Object value) {
super.put(key, value);
return this;
}
/**
* 分页信息,需要前端自己算分页数
*
* @param value
* @param totalElements
* @return
*/
public R putPageData(Object value, long totalElements) {
Map data = new HashMap(2);
data.put(LIST_KEY, value);
data.put(TOTAL_ELEMENTS_KEY, totalElements);
super.put(DATA_KEY, data);
return this;
}
/**
* spring jdbc分页信息
*
* @param page
* @return
*/
public R putPageData(org.springframework.data.domain.Page page) {
Map data = new HashMap(3);
data.put(LIST_KEY, page.getContent());//分页数据
data.put(TOTAL_PAGES_KEY, page.getTotalPages());//总页数
data.put(TOTAL_ELEMENTS_KEY, page.getTotalElements());//总数
super.put(DATA_KEY, data);
return this;
}
/**
* mybatis pagehelper分页信息
*
* @param page
* @return
*/
public R putPageData(com.github.pagehelper.PageInfo page) {
Map data = new HashMap(3);
data.put(LIST_KEY, page.getList());//分页数据
data.put(TOTAL_PAGES_KEY, page.getPages());//总页数
data.put(TOTAL_ELEMENTS_KEY, page.getTotal());//总数
super.put(DATA_KEY, data);
return this;
}
/**
* mybatis pagehelper分页信息
*
* @param page
* @return
*/
public R putPageData(com.github.pagehelper.Page<?> page, Collection<?> coll) {
Map data = new HashMap(2);
data.put(PAGE_KEY, page);//分页信息
data.put(LIST_KEY, coll);//分页数据
super.put(DATA_KEY, data);
return this;
}
/**
* 分页信息,需要前端自己算分页数
*
* @param page
* @return
*/
public R putPageData(Pagination page) {
Map data = new HashMap(2);
data.put(LIST_KEY, page.getList());
data.put(TOTAL_ELEMENTS_KEY, page.getTotal());//总数
super.put(DATA_KEY, data);
return this;
}
/**
* 分页信息,需要前端自己算分页数
*
* @param page
* @return
*/
public R putPageData(Pagination pagination, cn.hutool.db.Page page) {
Map data = new HashMap(3);
data.put(PAGE_KEY, page);//分页信息
data.put(LIST_KEY, pagination.getList());//数据列表
data.put(TOTAL_ELEMENTS_KEY, pagination.getTotal());//总数
super.put(DATA_KEY, data);
return this;
}
public void setMessage(String formatMessage) {
this.put(MSG_KEY, formatMessage);
}
public String getMessage() {
return (String) this.get(MSG_KEY);
}
public void setCode(int code) {
this.put(CODE_KEY, code);
}
public int getCode() {
return (int) this.get(CODE_KEY);
}
public Object getData() {
return this.get(DATA_KEY);
}
public boolean isSuccess() {
return SUCCESS_CODE == this.getCode();
}
}