| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.xintong.visualinspection.controller;
- import java.io.FileNotFoundException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.ibatis.exceptions.TooManyResultsException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.converter.HttpMessageNotReadableException;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.validation.BindingResult;
- import org.springframework.validation.ObjectError;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import com.alibaba.fastjson.JSON;
- import com.github.pagehelper.PageInfo;
- import com.xintong.system.err.BusinessException;
- import com.xintong.system.err.ErrorCode;
- import com.xintong.system.securityTools.JwtTokenUtil;
- import com.xintong.system.securityTools.RedisCacheUtil;
- import com.xintong.visualinspection.bean.User;
- import com.xintong.visualinspection.util.CacheUtil;
- /**
- * 文件名:TestController
- * 版本信息:日期:2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
- */
- @ControllerAdvice
- public class BaseController {
-
- @Autowired
- private JwtTokenUtil jwtTokenUtil;
- @Autowired
- private RedisCacheUtil redisCacheUtil;
- @Value("${jwt.header}")
- private String tokenHeader;
- @Value("${jwt.tokenHead}")
- private String tokenHead;
- /**
- * 返回前台结果结构体
- * @return
- * String
- * @exception
- * @since 1.0.0
- */
- public String returnResult(int result_code,String result_desc, Object o){
- Map<String,Object> result = new HashMap<>();
- result.put("result_code", result_code);
- result.put("result_desc", result_desc);
- result.put("result_data", o);
- return JSON.toJSON(result).toString();
- }
-
- /**
- * 返回前台结果结构体
- * @return
- * String
- * @exception
- * @since 1.0.0
- */
- public String returnSuccessResult(String result_desc){
- Map<String,Object> result = new HashMap<>();
- result.put("result_code", 0);
- result.put("result_desc", result_desc);
- return JSON.toJSON(result).toString();
- }
-
- /**
- * 返回前台结果结构体
- * @return
- * String
- * @exception
- * @since 1.0.0
- */
- public String returnSuccessResult(){
- Map<String,Object> result = new HashMap<>();
- result.put("result_code", 0);
- result.put("result_desc", "success");
- return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
- }
-
- /**
- * 返回前台结果结构体
- * @return
- * String
- * @exception
- * @since 1.0.0
- */
- public String returnSuccessResult(Object o){
- Map<String,Object> result = new HashMap<>();
- result.put("result_code", 0);
- result.put("result_desc", "success");
- result.put("result_data", o);
- return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
- }
-
- /**
- * 返回前台结果分页结构体
- * @return
- * String
- * @exception
- * @since 1.0.0
- */
- public String returnSuccessPageResult(Object o){
- Map<String,Object> result = new HashMap<>();
- result.put("result_code", 0);
- result.put("result_desc", "success");
- result.put("result_data", new PageInfo((List)o));
- return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
- }
-
- /**
- * 返回前台结果结构体
- * @return
- * String
- * @exception
- * @since 1.0.0
- */
- public String returnSuccessResult(String result_desc, Object o){
- Map<String,Object> result = new HashMap<>();
- result.put("result_code", 0);
- result.put("result_desc", result_desc);
- result.put("result_data", o);
- return JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm");
- }
-
- /** 基于@ExceptionHandler异常处理 */
- @ExceptionHandler
- public String exp(HttpServletRequest request, Exception ex) {
- // 根据不同错误提示不同的错误
- ErrorCode code;
- if(ex instanceof NullPointerException) {
- code = new ErrorCode(10001);
- } else if (ex instanceof NumberFormatException) {
- code = new ErrorCode(10002);
- } else if (ex instanceof IndexOutOfBoundsException) {
- code = new ErrorCode(10003);
- } else if (ex instanceof ArithmeticException) {
- code = new ErrorCode(10004);
- } else if (ex instanceof FileNotFoundException) {
- code = new ErrorCode(10005);
- } else if (ex instanceof IllegalArgumentException) {
- code = new ErrorCode(10006);
- } else if (ex instanceof HttpMessageNotReadableException) {
- code = new ErrorCode(10007);
- } else if (ex instanceof BusinessException) {
- code = ((BusinessException) ex).getErrCode();
- } else if(ex.getCause() instanceof TooManyResultsException){
- code = new ErrorCode(10008);
- } else if (ex instanceof MethodArgumentNotValidException) {
- BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
- code = new ErrorCode(20001,getValidatorErrors(bindingResult));
- } else{
- code = new ErrorCode(11000);
- }
- return returnResult(code.getCode(),code.getDesc(),null);
- }
-
- public static String getValidatorErrors(BindingResult bindingResult){
- StringBuffer sb = new StringBuffer();
- for(ObjectError err:bindingResult.getAllErrors()){
- sb.append("["+err.getDefaultMessage()+"]");
- }
- return sb.toString();
- }
-
- public User getCurrentUser(HttpServletRequest request){
- String authHeader = request.getHeader(this.tokenHeader);
- String authToken = authHeader.substring(tokenHead.length());
- String username = jwtTokenUtil.getUsernameFromToken(authToken);
- if (username != null) {
- UserDetails u = redisCacheUtil.getUserByUserName(username);
- User u_t = (User) JSON.parseObject(JSON.toJSONString(u),User.class) ;
- User user = CacheUtil.userMap.get(new Long(u_t.getId()));
- user.setRoles(u_t.getRoles());
- return user;
- }
- return null;
- }
- }
|