BaseController.java
2.86 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
package com.server.web.controller;
import com.server.shiro.constants.Constants;
import com.server.shiro.persistent.bean.SysUser;
import com.server.utils.ErrorCode;
import com.server.utils.StrUtils;
import com.server.web.common.service.OpenSqlRingsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2018/3/10.
*/
public class BaseController {
public final static String OSS_NAMESPACE = "/rest/kzy/oss";
public int pageNo = 0;
public int pageSize = 10;
@Autowired
public OpenSqlRingsService openSqlRingsService;
/**
* 返回成功
* @param obj
* @return
*/
public Map<String,Object> success(Object obj){
Map<String,Object> returnResult = new HashMap<String,Object>();
returnResult.put("status", ErrorCode.SUCCESS.getCode());
returnResult.put("message",ErrorCode.SUCCESS.getMessage());
returnResult.put("data",obj);
return returnResult;
}
/**
* 返回失败
* @param code
* @param message
* @param obj
* @return
*/
public Map<String,Object> error(String code, String message ,Object obj){
Map<String,Object> returnResult = new HashMap<String,Object>();
returnResult.put("status", code);
returnResult.put("message",message);
returnResult.put("data",obj);
return returnResult;
}
public Pageable getPageable(HttpServletRequest request) {
String pPage = request.getParameter("page");
String pRows = request.getParameter("rows");
int page = 0;
int size = 10;
if (StrUtils.isNumber(pPage)) {
page = Integer.parseInt(pPage);
}
if (StrUtils.isNumber(pRows)) {
size = Integer.parseInt(pRows);
}
Sort sort = getSort(request);
if (sort != null) {
return new PageRequest(page, size, sort);
}
return new PageRequest(page, size);
}
public Sort getSort(HttpServletRequest request) {
String orderBy = request.getParameter("sort");
String order = request.getParameter("order");
if (StrUtils.isBlank(orderBy)) {
return null;
}
if (Sort.Direction.DESC.name().equalsIgnoreCase(order)) {
order = Sort.Direction.DESC.name();
} else {
order = Sort.Direction.ASC.name();
}
return new Sort(Sort.Direction.fromString(order), orderBy);
}
public SysUser getSysUser(){
return (SysUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.SESSION_USER_INFO);
}
}