BaseController.java
7.45 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.lhcredit.framework.web.controller;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.lhcredit.framework.web.page.ColumnNode;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.service.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhcredit.common.utils.DateUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.common.utils.sql.SqlUtil;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.framework.web.domain.AjaxResult.Type;
import com.lhcredit.framework.web.page.PageDomain;
import com.lhcredit.framework.web.page.TableDataInfo;
import com.lhcredit.framework.web.page.TableSupport;
import com.lhcredit.project.system.user.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* web层通用数据处理
*
* @author lhcredit
*/
public class BaseController {
@Autowired
private TokenManager tokenManager;
/**
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateUtils.parseDate(text));
}
});
}
protected Integer getPageSize( ){
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageSize = pageDomain.getPageSize();
if (StringUtils.isNull(pageSize)) {
pageSize = 10;
}
return pageSize;
}
protected Integer getPageNum( ){
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
if (StringUtils.isNull(pageNum)) {
pageNum = 1;
}
return pageNum;
}
/**
* 设置请求分页数据
*/
protected void startPage() {
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (StringUtils.isNull(pageNum)) {
pageNum = 1;
}
if (StringUtils.isNull(pageSize)) {
pageSize = 10;
}
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
PageHelper.startPage(pageNum, pageSize, orderBy);
// if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
// String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
// PageHelper.startPage(pageNum, pageSize, orderBy);
// }else {
// PageHelper.startPage(1, 10, null);
// }
}
/**
* 设置请求分页数据
*/
protected void startPage(Integer pageNum, Integer pageSize) {
PageDomain pageDomain = TableSupport.buildPageRequest();
if (StringUtils.isNull(pageNum)) {
pageNum = 1;
}
if (StringUtils.isNull(pageSize)) {
pageSize = 10;
}
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
PageHelper.startPage(pageNum, pageSize, orderBy);
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({"rawtypes", "unchecked"})
protected TableDataInfo getDataTable(List<?> list) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(2000);
rspData.setRows(list);
rspData.setTotal(new PageInfo(list).getTotal());
return rspData;
}
/**
* 响应请求分页数据
*
* @param rows
* @return 操作结果
* @author LGJ
*/
protected AjaxResult toAjax(List<?> rows) {
TableDataInfo rspData = new TableDataInfo();
rspData.setRows(rows);
rspData.setTotal(new PageInfo(rows).getTotal());
return success(rspData);
}
/**
* 响应请求分页数据
*
* @param rows
* @param columns
* @return 操作结果
* @author LGJ
*/
protected AjaxResult toAjax(List<?> rows, List<ColumnNode> columns) {
TableDataInfo rspData = new TableDataInfo();
rspData.setRows(rows);
rspData.setColumns(columns);
rspData.setTotal(new PageInfo(rows).getTotal());
return success(rspData);
}
/**
* 响应请求分页数据
*
* @param rows
* @param
* @return 操作结果
* @author LGJ
*/
protected AjaxResult toAjaxMvs(List<?> rows, List<Map> mvs) {
TableDataInfo rspData = new TableDataInfo();
rspData.setRows(rows);
rspData.setMvs(mvs);
rspData.setTotal(new PageInfo(rows).getTotal());
return success(rspData);
}
/**
* 响应返回结果
*
* @param rows 影响行数
* @return 操作结果
*/
protected AjaxResult toAjax(int rows) {
return rows > 0 ? success() : error();
}
/**
* 响应返回结果
*
* @param result 结果
* @return 操作结果
*/
protected AjaxResult toAjax(boolean result) {
return result ? success() : error();
}
/**
* 响应返回结果
*
* @param data 结果
* @return 操作结果
* @author LGJ
*/
protected AjaxResult toAjax(Object data) {
return AjaxResult.success(data);
}
/**
* 返回成功
*/
public AjaxResult success() {
return AjaxResult.success();
}
/**
* 返回失败消息
*/
public AjaxResult error() {
return AjaxResult.error();
}
/**
* 返回成功消息
*/
public AjaxResult success(String message) {
return AjaxResult.success(message);
}
/**
* 返回成功消息及数据
*/
public AjaxResult success(Object data) {
return AjaxResult.success(data);
}
/**
* 返回失败消息
*/
public AjaxResult error(String message) {
return AjaxResult.error(message);
}
public AjaxResult loginError(String message) {
return AjaxResult.loginError(message);
}
/**
* 返回错误码消息
*/
public AjaxResult error(Type type, String message) {
return new AjaxResult(type, message);
}
/**
* 页面跳转
*/
public String redirect(String url) {
return StringUtils.format("redirect:{}", url);
}
public User getSysUser() {
return ShiroUtils.getSysUser();
}
public void setSysUser(User user) {
ShiroUtils.setSysUser(user);
}
public Long getUserId() {
return getSysUser().getUserId();
}
public String getLoginName() {
return getSysUser().getLoginName();
}
public FrontUserMon getUserInfo() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("authorization");
FrontUserMon user = tokenManager.getUserInfoByToken(token);
return user;
}
}