Browse Source

修改了异常类,JsonResult,将controller返回值弄成固定格式

superb 1 year ago
parent
commit
6ab545fd08

+ 8 - 0
common/src/main/java/com/koobietech/eas/common/exception/EasException.java

@@ -25,4 +25,12 @@ public class EasException extends RuntimeException{
         this.msg = msg;
         this.code = code;
     }
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
 }

+ 50 - 4
common/src/main/java/com/koobietech/eas/common/result/JsonResult.java

@@ -7,6 +7,8 @@ public class JsonResult implements Serializable {
     private String msg = "";
     private Object data = null;
 
+    private Integer code = 200;
+
     /**
      * 成功
      * @return
@@ -18,6 +20,18 @@ public class JsonResult implements Serializable {
         return result;
     }
 
+    /**
+     * 成功
+     * @return
+     */
+    public static JsonResult data(Object data , String message, Integer code ){
+        JsonResult result = JsonResult.ok();
+        result.setData(data);
+        result.setMsg(message);
+        result.setCode(code);
+        return result;
+    }
+
     /**
      * 成功
      * @return
@@ -65,10 +79,11 @@ public class JsonResult implements Serializable {
      * 失败
      * @return
      */
-    public static JsonResult fail( Object data ){
+    public static JsonResult fail( int code){
         JsonResult result = new JsonResult();
         result.setStatus(false);
-        result.setData(data);
+        result.setCode(code);
+        result.setMsg("操作失败");
         return result;
     }
 
@@ -76,10 +91,9 @@ public class JsonResult implements Serializable {
      * 失败
      * @return
      */
-    public static JsonResult fail( Object data, String msg ){
+    public static JsonResult fail( Object data ){
         JsonResult result = new JsonResult();
         result.setStatus(false);
-        result.setMsg(msg);
         result.setData(data);
         return result;
     }
@@ -95,7 +109,39 @@ public class JsonResult implements Serializable {
         return result;
     }
 
+    /**
+     * 失败
+     * @return
+     */
+    public static JsonResult fail( String message, int code ){
+        JsonResult result = new JsonResult();
+        result.setStatus(false);
+        result.setCode(code);
+        result.setMsg(message);
+        return result;
+    }
 
+    /**
+     * 失败
+     * @return
+     */
+    public static JsonResult fail( Object data, String msg ){
+        JsonResult result = new JsonResult();
+        result.setStatus(false);
+        result.setMsg(msg);
+        result.setData(data);
+        return result;
+    }
+
+
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public void setCode(Integer code) {
+        this.code = code;
+    }
     public boolean getStatus() {
         return status;
     }

+ 18 - 1
controller/src/main/java/com/koobietech/eas/config/ExceptionAdvice.java

@@ -1,5 +1,6 @@
 package com.koobietech.eas.config;
 
+import com.koobietech.eas.common.exception.EasException;
 import com.koobietech.eas.common.result.JsonResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,6 +29,22 @@ public class ExceptionAdvice {
             logger.error(element.toString());
         }
         logger.error("-------------");
-        return JsonResult.fail(e.getMessage());
+        return JsonResult.fail(e.getMessage(), 500);
+    }
+
+    @ResponseBody
+    @ResponseStatus(HttpStatus.OK)
+    @ExceptionHandler(EasException.class)
+    public JsonResult exceptionHandler(EasException e){
+        StackTraceElement[] stackTrace = e.getStackTrace();
+        e.printStackTrace();
+        logger.error("运行异常提醒:");
+        logger.error(e.getMessage());
+        logger.error("异常堆栈信息:");
+        for (StackTraceElement element : stackTrace) {
+            logger.error(element.toString());
+        }
+        logger.error("-------------");
+        return JsonResult.fail(e.getMessage(), e.getCode());
     }
 }

+ 8 - 44
controller/src/main/java/com/koobietech/eas/controller/AdminLoginController.java

@@ -1,6 +1,7 @@
 package com.koobietech.eas.controller;
 
 
+import com.anji.captcha.model.common.RepCodeEnum;
 import com.anji.captcha.model.common.ResponseModel;
 import com.anji.captcha.model.vo.CaptchaVO;
 import com.anji.captcha.service.CaptchaService;
@@ -44,61 +45,24 @@ public class AdminLoginController {
 
     @PostMapping("/verify.get")
     @Operation(summary = "获取验证码", description = "前端发起获取验证码请求,后端访问静态资源返回滑动图片")
-    public ResponseModel get(@RequestBody CaptchaVO captchaVO) {
-        return captchaService.get(captchaVO);
+    public JsonResult get(@RequestBody CaptchaVO captchaVO) {
+        return  adminLoginService.verifyToJsonResult(captchaService.get(captchaVO)) ;
     }
 
     @PostMapping("/verify.check")
     @Operation(summary = "核对验证码轨迹", description = "前端把用户的滑动轨迹转化成字符串传入后端,通过算法判断轨迹是否为真人")
-    public ResponseModel check(@RequestBody CaptchaVO captchaVO) {
-        return captchaService.check(captchaVO);
+    public JsonResult check(@RequestBody CaptchaVO captchaVO) {
+        return adminLoginService.verifyToJsonResult(captchaService.check(captchaVO));
     }
 
     @PostMapping("/verify.judge")
     @Operation(summary = "二次校验验证码", description = "校验滑动验证码结果,是否匹配上,传参:captchaVerification")
-    public ResponseModel verify(@RequestBody CaptchaVO captchaVO) {
+    public JsonResult verify(@RequestBody CaptchaVO captchaVO) {
         ResponseModel response = captchaService.verification(captchaVO);
-        //repCode  0000  无异常,代表成功
-        //repCode  9999  服务器内部异常
-        //repCode  0011  参数不能为空
-        //repCode  6110  验证码已失效,请重新获取
-        //repCode  6111  验证失败
-        //repCode  6112  获取验证码失败,请联系管理员
-        //repCode  6113  底图未初始化成功,请检查路径
-        //repCode  6201  get接口请求次数超限,请稍后再试!
-        //repCode  6206  无效请求,请重新获取验证码
-        //repCode  6202  接口验证失败数过多,请稍后再试
-        //repCode  6204  check接口请求次数超限,请稍后再试!
         if (!response.isSuccess()) {
-            String repCode = response.getRepCode();
-            switch (repCode) {
-                case "9999":
-                    throw new EasException("服务器内部异常", 9999);
-                case "0011":
-                    throw new EasException("参数不能为空", 0011);
-                case "6110":
-                    throw new EasException("验证码已失效,请重新获取", 6110);
-                case "6111":
-                    throw new EasException("验证失败", 6111);
-                case "6112":
-                    throw new EasException("获取验证码失败,请联系管理员", 6112);
-                case "6113":
-                    throw new EasException("底图未初始化成功,请检查路径", 6113);
-                case "6201":
-                    throw new EasException("get接口请求次数超限,请稍后再试!", 6201);
-                case "6206":
-                    throw new EasException("无效请求,请重新获取验证码", 6206);
-                case "6202":
-                    throw new EasException("接口验证失败数过多,请稍后再试", 6202);
-                case "6204":
-                    throw new EasException("check接口请求次数超限,请稍后再试!", 6204);
-                default:
-                    // 处理未知的 repCode
-                    throw new EasException("未知错误,你干嘛~哎哟~", -1);
-            }
+            throw new EasException( response.getRepMsg() , Integer.parseInt(response.getRepCode()));
         }
-
-        return response;
+        return adminLoginService.verifyToJsonResult(response);
     }
 
 

+ 6 - 0
service/pom.xml

@@ -42,6 +42,12 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-security</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.anji-plus</groupId>
+            <artifactId>captcha</artifactId>
+            <version>1.3.0</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <parent>

+ 4 - 0
service/src/main/java/com/koobietech/eas/service/AdminLoginService.java

@@ -1,5 +1,6 @@
 package com.koobietech.eas.service;
 
+import com.anji.captcha.model.common.ResponseModel;
 import com.koobietech.eas.common.result.JsonResult;
 import com.koobietech.eas.dao.Pojo.AdminPojo;
 import com.koobietech.eas.dao.adminLoginPojo.UserDetail;
@@ -10,5 +11,8 @@ public interface AdminLoginService {
 
     LoginToken refreshToken(String refreshToken);
 
+    //增加一个接口 把ResponseModel 里面的值赋值到JsonResult里面
+    JsonResult verifyToJsonResult(ResponseModel responseModel);
+
 
 }

+ 23 - 5
service/src/main/java/com/koobietech/eas/service/impl/AdminLoginServiceImpl.java

@@ -1,8 +1,10 @@
 package com.koobietech.eas.service.impl;
 
+import com.anji.captcha.model.common.ResponseModel;
 import com.koobietech.eas.common.constant.UserType;
 import com.koobietech.eas.common.exception.EasException;
 import com.koobietech.eas.common.pojo.JwtUserDto;
+import com.koobietech.eas.common.result.JsonResult;
 import com.koobietech.eas.common.utils.JwtManager;
 import com.koobietech.eas.common.utils.PasswordManager;
 import com.koobietech.eas.dao.Pojo.AdminPojo;
@@ -19,6 +21,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 
 @Service
@@ -37,6 +40,9 @@ public class AdminLoginServiceImpl implements AdminLoginService {
     @Resource
     private JwtManager jwtManager;
 
+    @Resource
+    private JsonResult jsonResult;
+
     // token过期时间 单位:s
     private final Integer token_expires = 30;
     private final Integer refreshToken_expires = 60;
@@ -110,6 +116,23 @@ public class AdminLoginServiceImpl implements AdminLoginService {
     }
 
 
+    @Override
+    public JsonResult verifyToJsonResult(ResponseModel response) {
+        JsonResult result = new JsonResult();
+
+        if (response != null) {
+            result.setMsg(response.getRepMsg());
+            result.setStatus(response.isSuccess());
+            result.setData(response.getRepData());
+            result.setCode( Integer.parseInt(response.getRepCode()) );
+        }
+        else {
+            throw new EasException("ResponseModel参数为空!",9000);
+        }
+
+        return result;
+    }
+
 
 
     private EasSysUserinfo findAdminByUsername(String username, String password)  {
@@ -139,11 +162,6 @@ public class AdminLoginServiceImpl implements AdminLoginService {
     private void validatePassword(String inputPassword, String encryptedPassword){
         String decryptedPassword = passwordManager.decryptPassword(inputPassword);
         System.out.println("前端解密来的decryptedPassword: " + decryptedPassword);
-
-        String encode = passwordEncoder.encode("123456");
-        System.out.println("cunli:"+encode);
-
-
         System.out.println("数据库查到的密码easSysUserinfo.getPasswd(): " + encryptedPassword);
         if (!passwordEncoder.matches(decryptedPassword, encryptedPassword)) {
             throw new EasException("密码不正确", 9901);