Pārlūkot izejas kodu

feat : 通用异常修改

bilingfeng 2 gadi atpakaļ
vecāks
revīzija
f89e0d6945

+ 62 - 52
game-module/game-common/src/main/java/com/zanxiang/common/domain/ResultVo.java

@@ -1,85 +1,95 @@
 package com.zanxiang.common.domain;
 
-import com.zanxiang.common.enums.ResEnum;
+import com.zanxiang.common.enums.HttpStatusEnum;
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
+import java.util.Objects;
 
 /**
- * 响应信息主体
- *
- * @author ruoyi
+ * @author : lingfeng
+ * @time : 2022-06-07
+ * @description : http响应消息
  */
 @Data
+@NoArgsConstructor
+@AllArgsConstructor
 public class ResultVo<T> implements Serializable {
-    private static final long serialVersionUID = 1L;
 
-    // 成功
-    public static final int CODE_SUCCESS = 200;
-    // 页面重定向
-    public static final int CODE_REDIRECT = 301;
-    // 重新去登录
-    public static final int CODE_TO_LOGIN = 310;
-    // 服务异常
-    public static final int CODE_ERROR = 500;
+    /**
+     * 序列化
+     */
+    private static final long serialVersionUID = 1L;
 
+    /**
+     * 错误码
+     */
     private int code;
 
+    /**
+     * 错误信息
+     */
     private String msg;
 
+    /**
+     * 返回内容
+     */
     private T data;
 
-    private ResultVo() {
-        this(CODE_SUCCESS);
+    /**
+     * 错误消息返回构造
+     *
+     * @param httpStatusEnum : http响应消息枚举
+     */
+    public ResultVo(HttpStatusEnum httpStatusEnum) {
+        this.code = httpStatusEnum.getCode();
+        this.msg = httpStatusEnum.getMsg();
     }
 
-    public ResultVo(int code) {
-        this(code, null);
+    /**
+     * 默认返回成功构造
+     *
+     * @param data : 返回的内容
+     */
+    public ResultVo(T data) {
+        this.code = HttpStatusEnum.SUCCESS.getCode();
+        this.msg = HttpStatusEnum.SUCCESS.getMsg();
+        this.data = data;
     }
 
-    public ResultVo(int code, String msg) {
-        this(code, msg, null);
+    /**
+     * 消息返回构造
+     *
+     * @param httpStatusEnum : http响应消息枚举
+     * @param data           : 返回的内容
+     */
+    public ResultVo(HttpStatusEnum httpStatusEnum, T data) {
+        this.code = httpStatusEnum.getCode();
+        this.msg = httpStatusEnum.getMsg();
+        this.data = data;
     }
 
-    public ResultVo(int code, String msg, T data) {
+    /**
+     * 自定义消息异常
+     *
+     * @param code : 错误码
+     * @param msg  : 错误消息
+     */
+    public ResultVo(int code, String msg) {
         this.code = code;
         this.msg = msg;
-        this.data = data;
-    }
-
-    public static <T> ResultVo<T> ok() {
-        return new ResultVo<>(CODE_SUCCESS);
-    }
-
-    public static <T> ResultVo<T> ok(T data) {
-        return new ResultVo<>(CODE_SUCCESS, null, data);
     }
 
-    public static <T> ResultVo<T> fail(ResEnum res) {
-        return new ResultVo<>(res.getCode(), res.getMsg());
-    }
-
-    public static <T> ResultVo<T> fail() {
-        return fail("服务异常!!");
+    /**
+     * 判断是否成功
+     */
+    public boolean isSuccess() {
+        return Objects.equals(HttpStatusEnum.SUCCESS.getCode(), this.code);
     }
 
     public static <T> ResultVo<T> fail(String msg) {
-        return new ResultVo<>(CODE_ERROR, msg);
-    }
-
-    public static <T> ResultVo<T> toLogin() {
-        return toLogin("登录错误,请重新登录!!!");
-    }
-
-    public static <T> ResultVo<T> toLogin(String msg) {
-        return new ResultVo<>(CODE_TO_LOGIN, msg);
-    }
-
-    public boolean hasSuccess() {
-        return this.code == CODE_SUCCESS;
-    }
-
-    public boolean hasFailed() {
-        return !hasSuccess();
+        return new ResultVo<>(HttpStatusEnum.FAIL.getCode(), msg);
     }
 }

+ 5 - 0
game-module/game-common/src/main/java/com/zanxiang/common/enums/ExpireTimeEnum.java

@@ -22,6 +22,11 @@ public enum ExpireTimeEnum {
      */
     ONE_MIN(60, "1分钟"),
 
+    /**
+     * 5分钟
+     */
+    FIVE_MIN(5 * 60, "1分钟"),
+
     /**
      * 1小时
      */

+ 5 - 0
game-module/game-common/src/main/java/com/zanxiang/common/enums/HttpStatusEnum.java

@@ -17,6 +17,11 @@ public enum HttpStatusEnum {
      */
     SUCCESS(200, "成功"),
 
+    /**
+     * 服务器异常
+     */
+    FAIL(500, "服务器异常"),
+
     /**
      * 未知错误
      */

+ 10 - 0
game-module/game-common/src/main/java/com/zanxiang/common/utils/StringUtils.java

@@ -349,6 +349,16 @@ public class StringUtils extends org.apache.commons.lang.StringUtils {
         return sb.toString();
     }
 
+    /**
+     * 验证字符串是否为手机号
+     *
+     * @param str : 字符串
+     * @return : 是否为手机号
+     */
+    public static boolean checkPhone(String str) {
+        return str.matches("^[1][3,4,5,7,8,9][0-9]{9}$");
+    }
+
     @SuppressWarnings("unchecked")
     public static <T> T cast(Object obj) {
         return (T) obj;