BaseController.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.qs.mp.web.controller;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.qs.mp.common.core.domain.AjaxResult;
  5. import com.qs.mp.common.core.page.PageDomain;
  6. import com.qs.mp.common.core.page.TableDataInfo;
  7. import com.qs.mp.common.core.page.TableSupport;
  8. import com.qs.mp.common.enums.ErrorCodeEnum;
  9. import com.qs.mp.common.utils.DateUtils;
  10. import com.qs.mp.common.utils.StringUtils;
  11. import com.qs.mp.common.utils.sql.SqlUtil;
  12. import java.beans.PropertyEditorSupport;
  13. import java.util.Date;
  14. import java.util.List;
  15. import java.util.Map;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.web.bind.WebDataBinder;
  19. import org.springframework.web.bind.annotation.InitBinder;
  20. /**
  21. * web层通用数据处理
  22. *
  23. * @author ygp
  24. */
  25. public class BaseController {
  26. protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  27. /**
  28. * 将前台传递过来的日期格式的字符串,自动转化为Date类型
  29. */
  30. @InitBinder
  31. public void initBinder(WebDataBinder binder) {
  32. // Date 类型转换
  33. binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
  34. @Override
  35. public void setAsText(String text) {
  36. setValue(DateUtils.parseDate(text));
  37. }
  38. });
  39. }
  40. /**
  41. * 设置请求分页数据
  42. */
  43. protected void startPage() {
  44. PageDomain pageDomain = TableSupport.buildPageRequest();
  45. Integer pageNum = pageDomain.getPageNum();
  46. Integer pageSize = pageDomain.getPageSize();
  47. if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
  48. String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
  49. PageHelper.startPage(pageNum, pageSize, orderBy);
  50. }
  51. }
  52. /**
  53. * 设置请求排序数据
  54. */
  55. protected void startOrderBy() {
  56. PageDomain pageDomain = TableSupport.buildPageRequest();
  57. if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) {
  58. String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
  59. PageHelper.orderBy(orderBy);
  60. }
  61. }
  62. /**
  63. * 响应请求分页数据
  64. */
  65. @SuppressWarnings({"rawtypes", "unchecked"})
  66. protected TableDataInfo getDataTable(List<?> list) {
  67. TableDataInfo rspData = new TableDataInfo();
  68. rspData.setCode(AjaxResult.Type.SUCCESS.value());
  69. rspData.setMsg("查询成功");
  70. rspData.setRows(list);
  71. rspData.setTotal(new PageInfo(list).getTotal());
  72. return rspData;
  73. }
  74. protected TableDataInfo getErrorDataTable(String msg) {
  75. TableDataInfo rspData = new TableDataInfo();
  76. rspData.setCode(AjaxResult.Type.ERROR.value());
  77. rspData.setMsg(msg);
  78. rspData.setRows(null);
  79. rspData.setTotal(0);
  80. return rspData;
  81. }
  82. /**
  83. * 返回成功
  84. */
  85. public AjaxResult success() {
  86. return AjaxResult.success();
  87. }
  88. /**
  89. * 返回成功消息
  90. */
  91. public AjaxResult success(String message) {
  92. return AjaxResult.success(message);
  93. }
  94. /**
  95. * 返回成功
  96. */
  97. public AjaxResult success(String msg, Object data) {
  98. return AjaxResult.success(msg, data);
  99. }
  100. public AjaxResult success(Object data) {
  101. return AjaxResult.success(data);
  102. }
  103. /**
  104. * 登陆超时
  105. */
  106. public AjaxResult timeout() {
  107. return AjaxResult.timeout();
  108. }
  109. /**
  110. * 登陆超时
  111. */
  112. public AjaxResult timeout(String msg) {
  113. return AjaxResult.timeout(msg);
  114. }
  115. /**
  116. * 返回失败消息
  117. */
  118. public AjaxResult error() {
  119. return AjaxResult.error();
  120. }
  121. /**
  122. * 返回失败消息
  123. */
  124. public AjaxResult error(String message) {
  125. return AjaxResult.error(message);
  126. }
  127. /**
  128. * 返回成功
  129. */
  130. public AjaxResult error(String msg, Object data) {
  131. return AjaxResult.error(msg, data);
  132. }
  133. /**
  134. * 返回错误码消息
  135. */
  136. public AjaxResult error(AjaxResult.Type type, String message) {
  137. return new AjaxResult(type, message);
  138. }
  139. public AjaxResult error(int code, String message) {
  140. return new AjaxResult(code, message, null);
  141. }
  142. public AjaxResult error(ErrorCodeEnum errorCodeEnum) {
  143. return new AjaxResult(errorCodeEnum.getCode(), errorCodeEnum.getMsg(), null);
  144. }
  145. /**
  146. * 响应返回结果
  147. *
  148. * @param rows 影响行数
  149. * @return 操作结果
  150. */
  151. protected AjaxResult toAjax(int rows) {
  152. return rows > 0 ? AjaxResult.success() : AjaxResult.error();
  153. }
  154. /**
  155. * 响应返回结果
  156. *
  157. * @param rows 影响行数
  158. * @return 操作结果
  159. */
  160. protected AjaxResult toAjax(int rows, String msg) {
  161. return rows > 0 ? success(msg) : error(msg);
  162. }
  163. /**
  164. * 响应返回结果
  165. *
  166. * @param rows 影响行数
  167. * @return 操作结果
  168. */
  169. protected AjaxResult toAjax(int rows, String msg, Object data) {
  170. return rows > 0 ? success(msg, data) : error(msg, data);
  171. }
  172. /**
  173. * 响应返回结果
  174. *
  175. * @param result 结果
  176. * @return 操作结果
  177. */
  178. protected AjaxResult toAjax(boolean result) {
  179. return result ? success() : error();
  180. }
  181. /**
  182. * 响应返回结果
  183. *
  184. * @param result 结果
  185. * @return 操作结果
  186. */
  187. protected AjaxResult toAjax(boolean result, String msg) {
  188. return result ? success(msg) : error(msg);
  189. }
  190. /**
  191. * 响应返回结果
  192. *
  193. * @param result 结果
  194. * @return 操作结果
  195. */
  196. protected AjaxResult toAjax(boolean result, String msg, Object data) {
  197. return result ? success(msg, data) : error(msg, data);
  198. }
  199. /**
  200. * 响应返回结果
  201. *
  202. * @return 操作结果
  203. */
  204. protected AjaxResult toAjax(AjaxResult.Type type, String msg, Object data) {
  205. return new AjaxResult(type, msg, data);
  206. }
  207. public Map<String, Object> getReturnJson(Map<String, Object> rsMap, int code, String msg,
  208. Object obj) {
  209. rsMap.put("code", code);
  210. rsMap.put("msg", msg);
  211. if (null != obj) {
  212. rsMap.put("data", obj);
  213. }
  214. logger.info("result data:" + rsMap);
  215. return rsMap;
  216. // return JSONObject.fromObject(rsMap).toString();
  217. }
  218. /**
  219. * 页面跳转
  220. */
  221. public String redirect(String url) {
  222. return StringUtils.format("redirect:{}", url);
  223. }
  224. }