This commit is contained in:
zhangchenbiao
2023-05-19 19:32:38 +08:00
parent e9b35b41f7
commit 63505941d0
70 changed files with 1238 additions and 192 deletions

View File

@@ -0,0 +1,53 @@
package com.cool.store.constants;
/**
* @author zhangchenbiao
* @FileName: CommonConstatns
* @Description:
* @date 2023-05-18 14:47
*/
public class CommonConstants {
public static final String DEFAULT_DB = "coolcollege_intelligent_config";
public static final String REQUEST_ID = "requestId";
public static final int ACTION_TOKEN_EXPIRE = 14400;
public static final int ZERO = 0;
public static final int ONE = 1;
public static final int TWO = 2;
public static final int THREE = 3;
public static final int FOUR = 4;
public static final int FIVE = 5;
public static final int SIX = 6;
public static final int SEVEN = 7;
public static final int EIGHT = 8;
public static final int NINE = 9;
public static final int TEN = 10;
public static final int TWENTY = 20;
public static final int THIRTY = 30;
public static final int FORTY = 40;
public static final int FIFTY = 50;
public static final int SIXTY = 60;
public static final int SEVENTY = 70;
public static final int EIGHTY = 80;
public static final int NINETY = 90;
public static final int HUNDRED = 100;
public static final String ZERO_STR = "0";
public static final String ONE_STR = "1";
public static final String TWO_STR = "2";
public static final String THREE_STR = "3";
public static final String FOUR_STR = "4";
public static final String FIVE_STR = "5";
public static final String SIX_STR = "6";
public static final String SEVEN_STR = "7";
public static final String EIGHT_STR = "8";
public static final String NINE_STR = "9";
public static final String TEN_STR = "10";
}

View File

@@ -0,0 +1,62 @@
package com.cool.store.enums;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* * 通用错误返回处理,后续全部将使用这个返回,请遵守以下规范。
* * 1.枚举名必须要大写。
* * 2.枚举中包含属性code,msg,en(英文尽量写)
* * 3.code的命名以系统模块划分前三位为模块划分。逐级细分。7位数字先到先取原则细化不浪费
* @author zhangchenbiao
* @FileName: ErrorCodeEnum
* @Description: 返回响应状态和文案枚举
* @date 2023-05-19 04:30
*/
public enum ErrorCodeEnum {
/**
* 000000 未知错误
*/
UNKNOWN(000000, "未知错误", null),
/**
* 2000001 token与当前登录企业不匹配
*/
TOKEN_ERROR(2000001,"请求异常,与当前登录企业不匹配",null),
FAIL(400000, "FAIL", null),
ACCESS_TOKEN_INVALID(400005, "Invalid token", null),
PARAMS_VALIDATE_ERROR(400003, "参数校验失败!", null),
;
protected static final Map<Integer, ErrorCodeEnum> map = Arrays.stream(values()).collect(
Collectors.toMap(ErrorCodeEnum::getCode, Function.identity(), (a, b)->a));
private int code;
private String message;
private String en;
ErrorCodeEnum(int code, String message, String en) {
this.code = code;
this.message = message;
this.en=en;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public static ErrorCodeEnum getByCode(Integer code) {
return map.get(code);
}
}

View File

@@ -0,0 +1,38 @@
package com.cool.store.enums;
/**
* @author Aaron
* @Description 业务统一返回码
* @date 2019/12/20
*/
public enum ResponseCodeEnum {
/**
* 成功返回
*/
SUCCESS(200000, "SUCCESS");
/**
* 返回码
*/
private int code;
/**
* 返回信息
*/
private String message;
ResponseCodeEnum(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}

View File

@@ -0,0 +1,93 @@
package com.cool.store.exception;
import com.cool.store.enums.ErrorCodeEnum;
import lombok.Data;
import java.text.MessageFormat;
/**
* @Description 业务异常类返回
* @author Aaron
* @date 2019/12/20
*/
@Data
public class ServiceException extends RuntimeException{
private static final long serialVersionUID = -5068776742356414959L;
/**
* 返回码
*/
private Integer errorCode;
/**
* 返回信息
*/
private String errorMessage;
private Object data;
/**
* 构造函数
* @param errorCode
* @param errorMessage
*/
@Deprecated
public ServiceException(Integer errorCode, String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
/**
* 构造函数
* @param errorCode
* @param errorMessage
*/
@Deprecated
public ServiceException(Integer errorCode, String errorMessage, Object data) {
super(errorMessage);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.data = data;
}
/**
* 构造函数
* @param errorMessage
*/
@Deprecated
public ServiceException(String errorMessage) {
super(errorMessage);
this.errorMessage = errorMessage;
}
/**
* 构造函数
* @param errorCode
* @param errorMessage
* @param cause
*/
@Deprecated
public ServiceException(Integer errorCode, String errorMessage, Throwable cause) {
super(errorMessage, cause);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public ServiceException(ErrorCodeEnum responseEnum) {
super(responseEnum.getMessage());
this.errorCode = responseEnum.getCode();
this.errorMessage = responseEnum.getMessage();
}
public ServiceException(ErrorCodeEnum responseEnum, Object... objects) {
super(responseEnum.getMessage());
String message = MessageFormat.format(responseEnum.getMessage(), objects);
this.errorCode = responseEnum.getCode();
this.errorMessage = message;
}
}

View File

@@ -1,13 +0,0 @@
package com.cool.store.model.constants;
/**
* @author zhangchenbiao
* @FileName: CommonConstatns
* @Description:
* @date 2023-05-18 14:47
*/
public class CommonConstants {
public static final String DEFAULT_DB = "coolcollege_intelligent_config";
}

View File

@@ -0,0 +1,95 @@
package com.cool.store.response;
import com.cool.store.constants.CommonConstants;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.ResponseCodeEnum;
import lombok.Data;
import lombok.ToString;
import org.slf4j.MDC;
import java.text.MessageFormat;
/**
* 统一返回结果
*
* @author Aaron
* @date 2019/12/20
*/
@Data
@ToString
public class ResponseResult<T> implements Result {
private static final long serialVersionUID = -2217360460304088285L;
public ResponseResult(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
this.requestId = MDC.get(CommonConstants.REQUEST_ID);
}
public ResponseResult(int code, String message, T data, String stackTrace) {
this.code = code;
this.message = message;
this.data = data;
this.stackTrace = stackTrace;
this.requestId = MDC.get(CommonConstants.REQUEST_ID);
}
public ResponseResult(int code, String message) {
this.code = code;
this.message = message;
this.requestId = MDC.get(CommonConstants.REQUEST_ID);
}
/**
* 返回码
*/
private int code;
/**
* 返回信息
*/
private String message;
/**
* 返回数据
*/
private T data;
/**
* 异常堆栈信息
*/
private String stackTrace;
private String requestId;
public long getSystemCurrentTime() {
return System.currentTimeMillis();
}
public static<T> ResponseResult<T> success(T data) {
return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(), "操作成功", data);
}
public static ResponseResult success() {
return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(), "操作成功");
}
public static ResponseResult fail(int code, String msg) {
return new ResponseResult(code, msg, false);
}
public static ResponseResult fail(int code, String msg, String stackTrace) {
return new ResponseResult(code, msg, false, stackTrace);
}
public static ResponseResult fail(ErrorCodeEnum responseEnum){
return new ResponseResult(responseEnum.getCode(), responseEnum.getMessage(), false);
}
public static ResponseResult fail(ErrorCodeEnum responseEnum, Object... objects){
String message = MessageFormat.format(responseEnum.getMessage(), objects);
return new ResponseResult(responseEnum.getCode(), message, false);
}
}

View File

@@ -0,0 +1,11 @@
package com.cool.store.response;
import java.io.Serializable;
/**
* @Description 响应格式父接口
* @author Aaron
* @date 2019/12/20
*/
public interface Result extends Serializable {
}

View File

@@ -1,4 +1,4 @@
package com.cool.store.model.utils;
package com.cool.store.utils;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.*;