BaseController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.xintong.visualinspection.controller;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.apache.ibatis.exceptions.TooManyResultsException;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.http.converter.HttpMessageNotReadableException;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.security.core.userdetails.UserDetails;
  13. import org.springframework.validation.BindingResult;
  14. import org.springframework.validation.ObjectError;
  15. import org.springframework.web.bind.MethodArgumentNotValidException;
  16. import org.springframework.web.bind.annotation.ControllerAdvice;
  17. import org.springframework.web.bind.annotation.ExceptionHandler;
  18. import com.alibaba.fastjson.JSON;
  19. import com.github.pagehelper.PageInfo;
  20. import com.xintong.system.err.BusinessException;
  21. import com.xintong.system.err.ErrorCode;
  22. import com.xintong.system.securityTools.JwtTokenUtil;
  23. import com.xintong.system.securityTools.RedisCacheUtil;
  24. import com.xintong.visualinspection.bean.User;
  25. import com.xintong.visualinspection.util.CacheUtil;
  26. /**
  27. * 文件名:TestController
  28. * 版本信息:日期:2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
  29. */
  30. @ControllerAdvice
  31. public class BaseController {
  32. @Autowired
  33. private JwtTokenUtil jwtTokenUtil;
  34. @Autowired
  35. private RedisCacheUtil redisCacheUtil;
  36. @Value("${jwt.header}")
  37. private String tokenHeader;
  38. @Value("${jwt.tokenHead}")
  39. private String tokenHead;
  40. /**
  41. * 返回前台结果结构体
  42. * @return
  43. * String
  44. * @exception
  45. * @since 1.0.0
  46. */
  47. public String returnResult(int result_code,String result_desc, Object o){
  48. Map<String,Object> result = new HashMap<>();
  49. result.put("result_code", result_code);
  50. result.put("result_desc", result_desc);
  51. result.put("result_data", o);
  52. return JSON.toJSON(result).toString();
  53. }
  54. /**
  55. * 返回前台结果结构体
  56. * @return
  57. * String
  58. * @exception
  59. * @since 1.0.0
  60. */
  61. public String returnSuccessResult(String result_desc){
  62. Map<String,Object> result = new HashMap<>();
  63. result.put("result_code", 0);
  64. result.put("result_desc", result_desc);
  65. return JSON.toJSON(result).toString();
  66. }
  67. /**
  68. * 返回前台结果结构体
  69. * @return
  70. * String
  71. * @exception
  72. * @since 1.0.0
  73. */
  74. public String returnSuccessResult(){
  75. Map<String,Object> result = new HashMap<>();
  76. result.put("result_code", 0);
  77. result.put("result_desc", "success");
  78. return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
  79. }
  80. /**
  81. * 返回前台结果结构体
  82. * @return
  83. * String
  84. * @exception
  85. * @since 1.0.0
  86. */
  87. public String returnSuccessResult(Object o){
  88. Map<String,Object> result = new HashMap<>();
  89. result.put("result_code", 0);
  90. result.put("result_desc", "success");
  91. result.put("result_data", o);
  92. return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
  93. }
  94. /**
  95. * 返回前台结果分页结构体
  96. * @return
  97. * String
  98. * @exception
  99. * @since 1.0.0
  100. */
  101. public String returnSuccessPageResult(Object o){
  102. Map<String,Object> result = new HashMap<>();
  103. result.put("result_code", 0);
  104. result.put("result_desc", "success");
  105. result.put("result_data", new PageInfo((List)o));
  106. return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
  107. }
  108. /**
  109. * 返回前台结果结构体
  110. * @return
  111. * String
  112. * @exception
  113. * @since 1.0.0
  114. */
  115. public String returnSuccessResult(String result_desc, Object o){
  116. Map<String,Object> result = new HashMap<>();
  117. result.put("result_code", 0);
  118. result.put("result_desc", result_desc);
  119. result.put("result_data", o);
  120. return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
  121. }
  122. /** 基于@ExceptionHandler异常处理 */
  123. @ExceptionHandler
  124. public String exp(HttpServletRequest request, Exception ex) {
  125. // 根据不同错误提示不同的错误
  126. ErrorCode code;
  127. if(ex instanceof NullPointerException) {
  128. code = new ErrorCode(10001);
  129. } else if (ex instanceof NumberFormatException) {
  130. code = new ErrorCode(10002);
  131. } else if (ex instanceof IndexOutOfBoundsException) {
  132. code = new ErrorCode(10003);
  133. } else if (ex instanceof ArithmeticException) {
  134. code = new ErrorCode(10004);
  135. } else if (ex instanceof FileNotFoundException) {
  136. code = new ErrorCode(10005);
  137. } else if (ex instanceof IllegalArgumentException) {
  138. code = new ErrorCode(10006);
  139. } else if (ex instanceof HttpMessageNotReadableException) {
  140. code = new ErrorCode(10007);
  141. } else if (ex instanceof BusinessException) {
  142. code = ((BusinessException) ex).getErrCode();
  143. } else if(ex.getCause() instanceof TooManyResultsException){
  144. code = new ErrorCode(10008);
  145. } else if (ex instanceof MethodArgumentNotValidException) {
  146. BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
  147. code = new ErrorCode(20001,getValidatorErrors(bindingResult));
  148. } else{
  149. code = new ErrorCode(11000);
  150. }
  151. return returnResult(code.getCode(),code.getDesc(),null);
  152. }
  153. public static String getValidatorErrors(BindingResult bindingResult){
  154. StringBuffer sb = new StringBuffer();
  155. for(ObjectError err:bindingResult.getAllErrors()){
  156. sb.append("["+err.getDefaultMessage()+"]");
  157. }
  158. return sb.toString();
  159. }
  160. public User getCurrentUser(HttpServletRequest request){
  161. String authHeader = request.getHeader(this.tokenHeader);
  162. String authToken = authHeader.substring(tokenHead.length());
  163. String username = jwtTokenUtil.getUsernameFromToken(authToken);
  164. if (username != null) {
  165. UserDetails u = redisCacheUtil.getUserByUserName(username);
  166. User u_t = (User) JSON.parseObject(JSON.toJSONString(u),User.class) ;
  167. User user = CacheUtil.userMap.get(new Long(u_t.getId()));
  168. user.setRoles(u_t.getRoles());
  169. return user;
  170. }
  171. return null;
  172. }
  173. }