BaseController.java 7.0 KB

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