| @@ -0,0 +1,26 @@ | |||||
| package com.xkcoding.common.enums; | |||||
| import com.xkcoding.common.enums.base.IStatus; | |||||
| import lombok.AllArgsConstructor; | |||||
| import lombok.Getter; | |||||
| /** | |||||
| * 常用状态 | |||||
| * | |||||
| * @author 一珩(沈扬凯 yk.shen@tuya.com) | |||||
| * @date 2022-08-20 01:49 | |||||
| */ | |||||
| @Getter | |||||
| @AllArgsConstructor | |||||
| public enum CommonStatus implements IStatus { | |||||
| /** | |||||
| * 操作状态码 | |||||
| */ | |||||
| OK(200, "请求成功"), | |||||
| BASE_ERROR(202, "基础服务异常"), | |||||
| PARAM_ERROR(203, "请求参数缺失"), | |||||
| SERVER_ERROR(500, "系统错误,请联系管理员"); | |||||
| private final Integer code; | |||||
| private final String desc; | |||||
| } | |||||
| @@ -0,0 +1,23 @@ | |||||
| package com.xkcoding.common.enums.base; | |||||
| /** | |||||
| * 状态抽象 | |||||
| * | |||||
| * @author 一珩(沈扬凯 yk.shen@tuya.com) | |||||
| * @date 2022-08-20 01:49 | |||||
| */ | |||||
| public interface IStatus { | |||||
| /** | |||||
| * 状态码 | |||||
| * | |||||
| * @return 状态码 | |||||
| */ | |||||
| Integer getCode(); | |||||
| /** | |||||
| * 描述 | |||||
| * | |||||
| * @return 描述 | |||||
| */ | |||||
| String getDesc(); | |||||
| } | |||||
| @@ -0,0 +1,43 @@ | |||||
| package com.xkcoding.common.exception; | |||||
| import com.xkcoding.common.enums.CommonStatus; | |||||
| import com.xkcoding.common.enums.base.IStatus; | |||||
| import lombok.Data; | |||||
| import lombok.EqualsAndHashCode; | |||||
| /** | |||||
| * 通用业务异常 | |||||
| * | |||||
| * @author 一珩(沈扬凯 yk.shen@tuya.com) | |||||
| * @date 2022-08-20 01:55 | |||||
| */ | |||||
| @Data | |||||
| @EqualsAndHashCode(callSuper = true) | |||||
| public class CommonBizException extends RuntimeException { | |||||
| public Integer code; | |||||
| private String message; | |||||
| public CommonBizException(Integer code, String message) { | |||||
| super(message); | |||||
| this.code = code; | |||||
| this.message = message; | |||||
| } | |||||
| public CommonBizException(Throwable cause) { | |||||
| super(cause); | |||||
| this.code = CommonStatus.SERVER_ERROR.getCode(); | |||||
| this.message = cause.getMessage(); | |||||
| } | |||||
| public CommonBizException(String message) { | |||||
| super(message); | |||||
| this.code = CommonStatus.SERVER_ERROR.getCode(); | |||||
| this.message = message; | |||||
| } | |||||
| public CommonBizException(IStatus status) { | |||||
| super(status.getDesc()); | |||||
| this.code = status.getCode(); | |||||
| this.message = status.getDesc(); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,65 @@ | |||||
| package com.xkcoding.common.model.viewmodel; | |||||
| import com.xkcoding.common.enums.CommonStatus; | |||||
| import com.xkcoding.common.enums.base.IStatus; | |||||
| import com.xkcoding.common.exception.CommonBizException; | |||||
| import lombok.Data; | |||||
| import java.io.Serializable; | |||||
| /** | |||||
| * 通用接口返回 | |||||
| * | |||||
| * @author 一珩(沈扬凯 yk.shen@tuya.com) | |||||
| * @date 2022-08-20 01:43 | |||||
| */ | |||||
| @Data | |||||
| public class Response<T> implements Serializable { | |||||
| /** | |||||
| * 状态码 | |||||
| */ | |||||
| private Integer code; | |||||
| /** | |||||
| * 返回内容 | |||||
| */ | |||||
| private String message; | |||||
| /** | |||||
| * 返回数据 | |||||
| */ | |||||
| private T data; | |||||
| private Response() { | |||||
| } | |||||
| private Response(Integer code, String message, T data) { | |||||
| this.code = code; | |||||
| this.message = message; | |||||
| this.data = data; | |||||
| } | |||||
| public static <T> Response<T> of(Integer code, String message, T data) { | |||||
| return new Response<>(code, message, data); | |||||
| } | |||||
| public static <T> Response<T> ofSuccess(T data) { | |||||
| return ofStatus(CommonStatus.OK, data); | |||||
| } | |||||
| public static <T> Response<T> ofStatus(IStatus status) { | |||||
| return ofStatus(status, null); | |||||
| } | |||||
| public static <T> Response<T> ofStatus(IStatus status, T data) { | |||||
| return of(status.getCode(), status.getDesc(), data); | |||||
| } | |||||
| public static <T> Response<T> ofError(CommonBizException exception, T data) { | |||||
| return of(exception.getCode(), exception.getMessage(), data); | |||||
| } | |||||
| public static <T> Response<T> ofError(CommonBizException exception) { | |||||
| return ofError(exception, null); | |||||
| } | |||||
| } | |||||