|
@@ -1,85 +1,95 @@
|
|
package com.zanxiang.common.domain;
|
|
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.Data;
|
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
|
|
|
import java.io.Serializable;
|
|
import java.io.Serializable;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 响应信息主体
|
|
|
|
- *
|
|
|
|
- * @author ruoyi
|
|
|
|
|
|
+ * @author : lingfeng
|
|
|
|
+ * @time : 2022-06-07
|
|
|
|
+ * @description : http响应消息
|
|
*/
|
|
*/
|
|
@Data
|
|
@Data
|
|
|
|
+@NoArgsConstructor
|
|
|
|
+@AllArgsConstructor
|
|
public class ResultVo<T> implements Serializable {
|
|
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 int code;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 错误信息
|
|
|
|
+ */
|
|
private String msg;
|
|
private String msg;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 返回内容
|
|
|
|
+ */
|
|
private T data;
|
|
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.code = code;
|
|
this.msg = msg;
|
|
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) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|