hr 4 anni fa
parent
commit
80eccc3a03

+ 11 - 11
src/main/java/com/imed/costaccount/common/exception/CostExceptionHandler.java

@@ -1,6 +1,6 @@
 package com.imed.costaccount.common.exception;
 
-import com.imed.costaccount.common.util.Result;
+import com.imed.costaccount.common.util.ErrorResult;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shiro.authz.UnauthorizedException;
 import org.springframework.dao.DataAccessException;
@@ -19,38 +19,38 @@ public class CostExceptionHandler {
 
 
     @ExceptionHandler(value = CostException.class)
-    public Result handlerEmosException(CostException e) {
+    public ErrorResult handlerEmosException(CostException e) {
         e.printStackTrace();
         log.info("GlobalExceptionHandler...");
         log.info("错误代码:" + e.getMessage());
-        return Result.errorMsg(500, e.getMessage());
+        return ErrorResult.errorMsg(500, e.getMessage());
     }
 
 //    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
     @ExceptionHandler(value = MethodArgumentNotValidException.class)
-    public Result handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
+    public ErrorResult handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
         e.printStackTrace();
         log.info("GlobalExceptionHandler...");
         log.info("错误代码:" + e.getMessage());
-        return Result.errorMsg(500, this.formatAllErrorMessages(e.getBindingResult().getAllErrors()));
+        return ErrorResult.errorMsg(500, this.formatAllErrorMessages(e.getBindingResult().getAllErrors()));
     }
 
 //    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
     @ExceptionHandler(value = UnauthorizedException.class)
-    public Result handlerUnauthorizedException(UnauthorizedException e) {
+    public ErrorResult handlerUnauthorizedException(UnauthorizedException e) {
         e.printStackTrace();
         log.info("GlobalExceptionHandler...");
         log.info("错误代码:" + e.getMessage());
-        return Result.errorMsg(403,"您的权限不足,请联系管理员添加");
+        return ErrorResult.errorMsg(403,"您的权限不足,请联系管理员添加");
     }
 
 //    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
     @ExceptionHandler(value = Exception.class)
-    public Result handlerException(Exception e) {
+    public ErrorResult handlerException(Exception e) {
         e.printStackTrace();
         log.info("===============================GlobalExceptionHandler异常信息===============================");
         log.info("错误代码:" + e.getLocalizedMessage());
-        return Result.errorMsg("预期之外错误,请联系管理员~");
+        return ErrorResult.errorMsg(500,"预期之外错误,请联系管理员~");
     }
 
     private String formatAllErrorMessages(List<ObjectError> errors) {
@@ -69,9 +69,9 @@ public class CostExceptionHandler {
 
     @ExceptionHandler(DataAccessException.class)
     @ResponseBody
-    public Result handleDataAccessException(DataAccessException e) {
+    public ErrorResult handleDataAccessException(DataAccessException e) {
         log.error("数据库错误:{}", e.getMessage(), e);
-        return Result.errorMsg("数据库错误");
+        return ErrorResult.errorMsg(500,"数据库错误");
 
     }
 }

+ 5 - 6
src/main/java/com/imed/costaccount/common/shiro/OAuth2Filter.java

@@ -2,8 +2,8 @@ package com.imed.costaccount.common.shiro;
 
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONUtil;
+import com.imed.costaccount.common.util.ErrorResult;
 import com.imed.costaccount.common.token.RedisUtil;
-import com.imed.costaccount.common.util.Result;
 import com.imed.costaccount.common.token.JwtUtil;
 import com.imed.costaccount.common.token.ThreadLocalToken;
 import lombok.extern.slf4j.Slf4j;
@@ -11,7 +11,6 @@ import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.authc.AuthenticationToken;
 import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -92,7 +91,7 @@ public class  OAuth2Filter extends AuthenticatingFilter {
         local.clear();
         String token = this.getRequestToken(req);
         if (StrUtil.isBlank(token)) {
-            String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
+            String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "无效的令牌,请登录"));
             resp.getWriter().print(json);
             return false;
         }
@@ -100,12 +99,12 @@ public class  OAuth2Filter extends AuthenticatingFilter {
         int userId = jwtUtil.getUserId(token);
         String str = (String) redisUtil.get(userId + "");
         if (StrUtil.isBlank(str)) {
-            String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
+            String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "无效的令牌,请登录"));
             resp.getWriter().print(json);
             return false;
         }
         if (!str.equalsIgnoreCase(token)) {
-            String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
+            String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "无效的令牌,请登录"));
             resp.getWriter().print(json);
             return false;
         }
@@ -117,7 +116,7 @@ public class  OAuth2Filter extends AuthenticatingFilter {
 //            // 无效的令牌
             resp.setStatus(400);
             resp.getWriter().print("无效的令牌");
-            String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌"));
+            String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "无效的令牌"));
             resp.getWriter().print(json);
             return false;
         }

+ 72 - 0
src/main/java/com/imed/costaccount/common/util/ErrorResult.java

@@ -0,0 +1,72 @@
+package com.imed.costaccount.common.util;
+
+
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+
+@AllArgsConstructor
+@NoArgsConstructor
+public class ErrorResult {
+
+    private Boolean success;
+
+    private Object data;
+
+    private Integer errorCode;
+
+    private String errorMessage;
+
+    private Integer showType;
+
+    public ErrorResult(Integer errorCode, String errorMessage) {
+        this.success = false;
+        this.data = null;
+        this.errorCode = errorCode;
+        this.errorMessage = errorMessage;
+        this.showType = 4;
+    }
+
+    public static ErrorResult errorMsg(Integer errorCode, String errorMessage) {
+        return new ErrorResult(errorCode, errorMessage);
+    }
+
+    public Boolean getSuccess() {
+        return success;
+    }
+
+    public void setSuccess(Boolean success) {
+        this.success = success;
+    }
+
+    public Object getData() {
+        return data;
+    }
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+
+    public Integer getErrorCode() {
+        return errorCode;
+    }
+
+    public void setErrorCode(Integer errorCode) {
+        this.errorCode = errorCode;
+    }
+
+    public String getErrorMessage() {
+        return errorMessage;
+    }
+
+    public void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+    }
+
+    public Integer getShowType() {
+        return showType;
+    }
+
+    public void setShowType(Integer showType) {
+        this.showType = showType;
+    }
+}

+ 25 - 21
src/main/java/com/imed/costaccount/common/util/Result.java

@@ -50,28 +50,32 @@ public class Result {
         return new Result(null);
     }
 
-    public static Result errorMsg(String msg) {
-        return new Result(500, msg, null);
-    }
-    public static Result errorMsg(int code,String msg) {
-        return new Result(code, msg, null);
-    }
-
-    public static Result errorMap(Object data) {
-        return new Result(501, "error", data);
-    }
-
-    public static Result errorTokenMsg(String msg) {
-        return new Result(502, msg, null);
-    }
-
-    public static Result errorException(String msg) {
-        return new Result(555, msg, null);
-    }
+//    public static Result error(int code, String msg) {
+//
+//    }
 
-    public static Result errorUserQQ(String msg) {
-        return new Result(556, msg, null);
-    }
+//    public static Result errorMsg(String msg) {
+//        return new Result(500, msg, null);
+//    }
+//    public static Result errorMsg(int code,String msg) {
+//        return new Result(code, msg, null);
+//    }
+//
+//    public static Result errorMap(Object data) {
+//        return new Result(501, "error", data);
+//    }
+//
+//    public static Result errorTokenMsg(String msg) {
+//        return new Result(502, msg, null);
+//    }
+//
+//    public static Result errorException(String msg) {
+//        return new Result(555, msg, null);
+//    }
+//
+//    public static Result errorUserQQ(String msg) {
+//        return new Result(556, msg, null);
+//    }
 
     public Result() {
 

+ 2 - 1
src/main/java/com/imed/costaccount/web/ExcelController.java

@@ -5,6 +5,7 @@ import cn.hutool.core.io.IoUtil;
 import cn.hutool.poi.excel.ExcelReader;
 import cn.hutool.poi.excel.ExcelUtil;
 import cn.hutool.poi.excel.ExcelWriter;
+import com.imed.costaccount.common.exception.CostException;
 import com.imed.costaccount.common.util.Result;
 import com.imed.costaccount.model.User;
 import com.imed.costaccount.service.UserService;
@@ -86,7 +87,7 @@ public class ExcelController {
             return userService.importUser(read, user);
         } catch (IOException e) {
             e.printStackTrace();
-            return Result.errorMsg("导入失败");
+            throw new CostException(500, "导入失败");
         }
     }