AppBaseController.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.ruoyi.app.controller.base;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.ruoyi.app.base.AppLoginUser;
  5. import com.ruoyi.app.base.AppTokenService;
  6. import com.ruoyi.common.constant.HttpStatus;
  7. import com.ruoyi.common.core.domain.AjaxResult;
  8. import com.ruoyi.common.core.page.PageDomain;
  9. import com.ruoyi.common.core.page.TableDataInfo;
  10. import com.ruoyi.common.core.page.TableSupport;
  11. import com.ruoyi.common.utils.DateUtils;
  12. import com.ruoyi.common.utils.StringUtils;
  13. import com.ruoyi.common.utils.sql.SqlUtil;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.WebDataBinder;
  18. import org.springframework.web.bind.annotation.InitBinder;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.beans.PropertyEditorSupport;
  21. import java.util.Date;
  22. import java.util.List;
  23. public class AppBaseController {
  24. protected final Logger logger = LoggerFactory.getLogger(AppBaseController.class);
  25. @Autowired
  26. AppTokenService appTokenService;
  27. /**
  28. * 将前台传递过来的日期格式的字符串,自动转化为Date类型
  29. */
  30. @InitBinder
  31. public void initBinder(WebDataBinder binder) {
  32. // Date 类型转换
  33. binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
  34. {
  35. @Override
  36. public void setAsText(String text)
  37. {
  38. setValue(DateUtils.parseDate(text));
  39. }
  40. });
  41. }
  42. /**
  43. * 设置请求分页数据
  44. */
  45. protected void startPage() {
  46. PageDomain pageDomain = TableSupport.buildPageRequest();
  47. Integer pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
  48. Integer pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
  49. if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
  50. {
  51. String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
  52. PageHelper.startPage(pageNum, pageSize, orderBy);
  53. }
  54. }
  55. /**
  56. * 响应请求分页数据
  57. */
  58. @SuppressWarnings({ "rawtypes", "unchecked" })
  59. protected TableDataInfo getDataTable(List<?> list) {
  60. TableDataInfo rspData = new TableDataInfo();
  61. rspData.setCode(HttpStatus.SUCCESS);
  62. rspData.setMsg("查询成功");
  63. rspData.setRows(list);
  64. rspData.setTotal(new PageInfo(list).getTotal());
  65. return rspData;
  66. }
  67. /**
  68. * 响应返回结果
  69. *
  70. * @param rows 影响行数
  71. * @return 操作结果
  72. */
  73. protected AjaxResult toAjax(int rows)
  74. {
  75. return rows > 0 ? AjaxResult.success() : AjaxResult.error();
  76. }
  77. /**
  78. * 根据token获取登录用户
  79. * @return
  80. */
  81. protected AppLoginUser getLoginUser(){
  82. return appTokenService.getLoginUser();
  83. }
  84. }