CommonUtil.java
5.43 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
package com.server.shiro.util;
import com.alibaba.fastjson.JSONObject;
import com.server.shiro.constants.Constants;
import com.server.shiro.constants.ErrorEnum;
import com.server.shiro.persistent.bean.SysUser;
import com.server.shiro.persistent.bean.UserModel;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
/**
* Created by yinbinhome@163.com on 2018/2/23.
* description:
*/
public class CommonUtil {
/**
* 返回一个returnData为空对象的成功消息的json
*
* @return
*/
public static JSONObject successJson() {
return successJson(new JSONObject());
}
/**
* 返回一个返回码为100的json
*
* @param returnData json里的主要内容
* @return
*/
public static JSONObject successJson(Object returnData) {
JSONObject resultJson = new JSONObject();
resultJson.put("returnCode", Constants.SUCCESS_CODE);
resultJson.put("returnMsg", Constants.SUCCESS_MSG);
resultJson.put("returnData", returnData);
return resultJson;
}
/**
* 返回错误信息JSON
*
* @param errorEnum 错误码的errorEnum
* @return
*/
public static JSONObject errorJson(ErrorEnum errorEnum) {
JSONObject resultJson = new JSONObject();
resultJson.put("returnCode", errorEnum.getErrorCode());
resultJson.put("returnMsg", errorEnum.getErrorMsg());
resultJson.put("returnData", new JSONObject());
return resultJson;
}
/**
* 查询分页结果后的封装工具方法
*
* @param requestJson 请求参数json,此json在之前调用fillPageParam 方法时,已经将pageRow放入
* @param list 查询分页对象list
* @param totalCount 查询出记录的总条数
*/
public static JSONObject successPage(final JSONObject requestJson, List<JSONObject> list, int totalCount) {
int pageRow = requestJson.getIntValue("pageRow");
int totalPage = getPageCounts(pageRow, totalCount);
JSONObject result = successJson();
JSONObject returnData = new JSONObject();
returnData.put("list", list);
returnData.put("totalCount", totalCount);
returnData.put("totalPage", totalPage);
result.put("returnData", returnData);
return result;
}
/**
* 查询分页结果后的封装工具方法
*
* @param list 查询分页对象list
*/
public static JSONObject successPage(List<JSONObject> list) {
JSONObject result = successJson();
JSONObject returnData = new JSONObject();
returnData.put("list", list);
result.put("returnData", returnData);
return result;
}
/**
* 获取总页数
*
* @param pageRow 每页行数
* @param itemCount 结果的总条数
* @return
*/
public static int getPageCounts(int pageRow, int itemCount) {
if (itemCount == 0) {
return 1;
}
return itemCount % pageRow > 0 ?
itemCount / pageRow + 1 :
itemCount / pageRow;
}
/**
* 将request参数值转为json
*
* @param request
* @return
*/
public static JSONObject request2Json(HttpServletRequest request) {
JSONObject requestJson = new JSONObject();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] pv = request.getParameterValues(paramName);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pv.length; i++) {
if (pv[i].length() > 0) {
if (i > 0) {
sb.append(",");
}
sb.append(pv[i]);
}
}
requestJson.put(paramName, sb.toString());
}
return requestJson;
}
/**
* 在分页查询之前,为查询条件里加上分页参数
*
* @param paramObject 查询条件json
* @param defaultPageRow 默认的每页条数,即前端不传pageRow参数时的每页条数
*/
public static void fillPageParam(final JSONObject paramObject, int defaultPageRow) {
int pageNum = paramObject.getIntValue("pageNum");
pageNum = pageNum == 0 ? 1 : pageNum;
int pageRow = paramObject.getIntValue("pageRow");
pageRow = pageRow == 0 ? defaultPageRow : pageRow;
paramObject.put("offSet", (pageNum - 1) * pageRow);
paramObject.put("pageRow", pageRow);
paramObject.put("pageNum", pageNum);
//删除此参数,防止前端传了这个参数,pageHelper分页插件检测到之后,拦截导致SQL错误
paramObject.remove("pageSize");
}
/**
* 分页查询之前的处理参数
* 没有传pageRow参数时,默认每页10条.
*
* @param paramObject
*/
public static void fillPageParam(final JSONObject paramObject) {
fillPageParam(paramObject, 10);
}
public static JSONObject loginJson(UserModel user){
JSONObject resultJson = new JSONObject();
resultJson.put("code", Constants.SUCCESS_CODE);
resultJson.put("message", Constants.SUCCESS_MSG);
resultJson.put("status", "1");
JSONObject data=new JSONObject();
data.put("usermodel",user);
resultJson.put("returnData", data);
return resultJson;
}
}