BaseController.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.fdkankan.ucenter.common;
  2. import cn.hutool.core.map.TolerantMap;
  3. import com.fdkankan.common.constant.ErrorCode;
  4. import com.fdkankan.common.util.DateEditor;
  5. import com.fdkankan.common.util.JwtUtil;
  6. import com.fdkankan.ucenter.common.constants.NacosProperty;
  7. import com.fdkankan.ucenter.common.constants.ResultCode;
  8. import com.fdkankan.ucenter.constant.LoginConstant;
  9. import com.fdkankan.ucenter.entity.User;
  10. import com.fdkankan.ucenter.exception.BusinessException;
  11. import com.fdkankan.ucenter.service.IUserService;
  12. import com.fdkankan.ucenter.util.DateUserUtil;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.propertyeditors.StringTrimmerEditor;
  16. import org.springframework.data.domain.PageRequest;
  17. import org.springframework.data.domain.Sort;
  18. import org.springframework.data.domain.Sort.Direction;
  19. import org.springframework.util.StringUtils;
  20. import org.springframework.web.bind.WebDataBinder;
  21. import org.springframework.web.bind.annotation.InitBinder;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.*;
  25. import java.util.Date;
  26. @Slf4j
  27. public class BaseController {
  28. @Autowired
  29. protected HttpServletRequest request;
  30. @Autowired
  31. protected HttpServletResponse response;
  32. @Autowired
  33. IUserService userService;
  34. @InitBinder
  35. protected void initBinder(WebDataBinder webDataBinder) {
  36. webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
  37. webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
  38. }
  39. /**
  40. * 带参重定向
  41. *
  42. * @param path
  43. * @return
  44. */
  45. protected String redirect(String path) {
  46. return "redirect:" + path;
  47. }
  48. /**
  49. * 不带参重定向
  50. *
  51. * @param response
  52. * @param path
  53. * @return
  54. */
  55. protected String redirect(HttpServletResponse response, String path) {
  56. try {
  57. response.sendRedirect(path);
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. return null;
  62. }
  63. public static void output(HttpServletResponse resp, File file) {
  64. OutputStream os = null;
  65. BufferedInputStream bis = null;
  66. byte[] buff = new byte[1024];
  67. try {
  68. os = resp.getOutputStream();
  69. bis = new BufferedInputStream(new FileInputStream(file));
  70. int i = 0;
  71. while ((i = bis.read(buff)) != -1) {
  72. os.write(buff, 0, i);
  73. os.flush();
  74. }
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. } finally {
  78. try {
  79. bis.close();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }
  85. protected String getToken(){
  86. String token = request.getHeader("token");
  87. if(StringUtils.isEmpty(token)){
  88. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  89. }
  90. return token;
  91. }
  92. protected String getUserName(){
  93. String token = request.getHeader("token");
  94. if(StringUtils.isEmpty(token)){
  95. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  96. }
  97. String username = JwtUtil.getUsername(token);
  98. if(StringUtils.isEmpty(username)){
  99. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  100. }
  101. return username;
  102. }
  103. protected Long getUserId(){
  104. String token = request.getHeader("token");
  105. if(StringUtils.isEmpty(token)){
  106. return null;
  107. }
  108. String username = JwtUtil.getUsername(token);
  109. User user = userService.getByUserName(username);
  110. if(user == null){
  111. return null;
  112. }
  113. return user.getId();
  114. }
  115. protected User getUser(){
  116. String username = JwtUtil.getUsername(getToken());
  117. if(org.apache.commons.lang3.StringUtils.isBlank(username)){
  118. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  119. }
  120. User user = userService.getByUserName(username);
  121. if(user == null){
  122. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  123. }
  124. return user;
  125. }
  126. protected String getLang(){
  127. return StringUtils.isEmpty(request.getHeader("lang")) ? "en":request.getHeader("lang");
  128. }
  129. protected String getSign(){
  130. return request.getHeader("sign");
  131. }
  132. protected String getAgentKey(){
  133. return request.getHeader("agent-key");
  134. }
  135. protected Integer getTimeZone(){
  136. Integer minute = 0;
  137. String timeZone = request.getHeader("timeZone");
  138. try {
  139. if(!StringUtils.isEmpty(timeZone)){
  140. minute = Integer.parseInt(timeZone);
  141. }
  142. }catch (Exception e){
  143. log.error("getClientTime-error:requestHeaderTimeZone:{}",timeZone,e);
  144. }
  145. if(!NacosProperty.activeFile.contains("eur")){
  146. minute += 480;
  147. }
  148. return minute;
  149. }
  150. protected String getClientTime(){
  151. return DateUserUtil.AddMinute(new Date(),getTimeZone());
  152. }
  153. }