diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..7c78bd1 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..7e16724 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..72a22db --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e517871 --- /dev/null +++ b/pom.xml @@ -0,0 +1,100 @@ + + + 4.0.0 + + work.imyeyu.common4j + common4j + 0.0.1 + jar + + + 21 + 21 + UTF-8 + true + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 3.1.3 + + + org.apache.maven.plugins + maven-source-plugin + 3.3.1 + + + attach-sources + package + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.11.2 + + + attach-javadocs + package + + jar + + + + + + + + + + jtec_nexus + http://106.53.0.213:33232/repository/maven-releases/ + + + + + + jtec_nexus + http://106.53.0.213:33232/repository/maven-public/ + + true + + + true + + + + timi_nexus + https://nexus.imyeyu.com/repository/maven-public/ + + true + + + true + + + + + + + org.junit.jupiter + junit-jupiter + 5.10.3 + test + + + com.imyeyu.java + timi-java + 0.0.1 + + + diff --git a/src/main/java/work/imyeyu/common4j/ApiResponse.java b/src/main/java/work/imyeyu/common4j/ApiResponse.java new file mode 100644 index 0000000..82aff0d --- /dev/null +++ b/src/main/java/work/imyeyu/common4j/ApiResponse.java @@ -0,0 +1,47 @@ +package work.imyeyu.common4j; + +import com.imyeyu.java.bean.timi.TimiResponse; + +import java.util.Objects; + +/** + * 通用接口返回对象 + * + * @author 夜雨 + * @since 2021-07-01 20:18 + */ +public class ApiResponse extends TimiResponse { + + public ApiResponse() { + super(); + } + + public ApiResponse(ResultCode code) { + super(code.toTimiCode()); + } + + public ApiResponse(ResultCode code, T data) { + super(code.toTimiCode(), data); + } + + public ApiResponse(ResultCode code, T data, String msg) { + super(code.toTimiCode(), data, msg); + } + + @Override + public AppException toException() { + return Objects.requireNonNull(ResultCode.fromCode(this.code)).toException(msg); + } + + @Override + public ApiResponse msg(String msg) { + super.msg(msg); + return this; + } + + @Override + public ApiResponse msgKey(String msgKey) { + super.msgKey(msgKey); + return this; + } +} diff --git a/src/main/java/work/imyeyu/common4j/AppError.java b/src/main/java/work/imyeyu/common4j/AppError.java new file mode 100644 index 0000000..565006b --- /dev/null +++ b/src/main/java/work/imyeyu/common4j/AppError.java @@ -0,0 +1,37 @@ +package work.imyeyu.common4j; + +import com.imyeyu.java.bean.timi.TimiError; + +import java.util.Objects; + +/** + * 致命错误 + * + * @author 夜雨 + * @since 2023-04-27 15:55 + */ +public class AppError extends TimiError { + + public AppError(ResultCode code) { + super(code.toTimiCode()); + } + + public AppError(ResultCode code, String msg) { + super(code.toTimiCode(), msg); + } + + @Override + public ApiResponse toResponse() { + return new ApiResponse<>(Objects.requireNonNull(ResultCode.fromCode(code.getValue()))).msg(getMessage()).msgKey(msgKey); + } + + @Override + public TimiError msg(String msg) { + return super.msg(msg); + } + + @Override + public TimiError msgKey(String msgKey) { + return super.msgKey(msgKey); + } +} diff --git a/src/main/java/work/imyeyu/common4j/AppException.java b/src/main/java/work/imyeyu/common4j/AppException.java new file mode 100644 index 0000000..1055b3d --- /dev/null +++ b/src/main/java/work/imyeyu/common4j/AppException.java @@ -0,0 +1,43 @@ +package work.imyeyu.common4j; + +import com.imyeyu.java.bean.timi.TimiException; + +import java.util.Objects; + +/** + * 通用运行时异常,附加通用代码 + * + * @author 夜雨 + * @since 2021-05-20 15:18 + */ +public class AppException extends TimiException { + + public AppException(ResultCode code) { + super(code.toTimiCode()); + } + + public AppException(ResultCode code, String msg) { + super(code.toTimiCode(), msg); + } + + public AppException(ResultCode code, String msg, Throwable e) { + super(code.toTimiCode(), msg, e); + } + + @Override + public ApiResponse toResponse() { + return new ApiResponse<>(Objects.requireNonNull(ResultCode.fromCode(code.getValue()))).msg(getMessage()).msgKey(msgKey); + } + + @Override + public AppException msg(String msg) { + super.msg(msg); + return this; + } + + @Override + public AppException msgKey(String msgKey) { + super.msgKey(msgKey); + return this; + } +} diff --git a/src/main/java/work/imyeyu/common4j/ResultCode.java b/src/main/java/work/imyeyu/common4j/ResultCode.java new file mode 100644 index 0000000..2a38908 --- /dev/null +++ b/src/main/java/work/imyeyu/common4j/ResultCode.java @@ -0,0 +1,141 @@ +package work.imyeyu.common4j; + +import com.imyeyu.java.bean.timi.TimiCode; + +/** + * 通用代码(基于 HTTP 代码扩展) + * + * @author 夜雨 + * @since 2021-05-21 14:32 + */ +public enum ResultCode { + + // ---------- 200 正常 ---------- + + /** 执行成功 */ + SUCCESS(20000), + + /** 执行成功,忽略请求 */ + IGNORE (20001), + + // ---------- 400 参数异常 ---------- + + /** 缺少参数 */ + ARG_MISS(40000), + + /** 不合法的参数 */ + ARG_BAD(40001), + + /** 过期的参数 */ + ARG_EXPIRED(40002), + + // ---------- 401 权限异常 ---------- + + /** 无权限 */ + PERMISSION_MISS(40100), + + /** 无权限 */ + PERMISSION_ERROR (40101), + + /** 非法请求 */ + REQUEST_BAD(40102), + + // ---------- 403 数据异常 ---------- + + /** 数据已存在 */ + DATA_EXIST (40301), + + // ---------- 404 资源异常 ---------- + + /** 无数据 */ + RESULT_NULL(40400), + + /** 返回数据异常 */ + RESULT_BAD (40401), + + /** 禁用的数据 */ + RESULT_BAN (40402), + + /** 上游服务器连接超时 */ + RESULT_TIMEOUT (40403), + + // ---------- 500 致命异常 ---------- + + /** 服务异常 */ + ERROR(50000), + + /** 服务异常 */ + ERROR_NOT_SUPPORT(50001), + + /** 服务异常 */ + ERROR_NPE_VARIABLE(50002), + + /** 服务关闭 */ + ERROR_SERVICE_OFF(50003), + + /** 服务繁忙 */ + ERROR_SERVICE_BUSY(50300); + + final Integer value; + + ResultCode(Integer value) { + this.value = value; + } + + /** + * 转为通用异常 + * + * @return 异常 + */ + public AppException toException() { + return toException(toString()); + } + + /** + * 转为通用异常 + * + * @param msg 异常消息 + * @return 异常 + */ + public AppException toException(String msg) { + return new AppException(this, msg); + } + + /** + * 转为通用返回对象 + * + * @return 返回对象 + */ + public ApiResponse toResponse() { + return new ApiResponse<>(this).msg(toString()); + } + + /** + * 获取代码 + * + * @return 代码 + */ + public Integer getValue() { + return value; + } + + /** + * 根据代码返回对象 + * + * @param code 代码 + * @return 对象 + */ + public static ResultCode fromCode(int code) { + ResultCode[] codes = values(); + for (int i = 0; i < codes.length; i++) { + if (codes[i].getValue() == code) { + return codes[i]; + } + } + return null; + } + + TimiCode toTimiCode() { + return TimiCode.fromCode(value); + } +}