10 changed files with 543 additions and 49 deletions
@ -0,0 +1,141 @@ |
|||||
|
package org.nl.modules.common.api; |
||||
|
|
||||
|
/** |
||||
|
* 通用返回对象 |
||||
|
* |
||||
|
* @author gbx |
||||
|
* @date 2023-03-02 |
||||
|
*/ |
||||
|
public class CommonResult<T> { |
||||
|
private long code; |
||||
|
private String desc; |
||||
|
private T result; |
||||
|
|
||||
|
public CommonResult() { |
||||
|
} |
||||
|
|
||||
|
protected CommonResult(T result) { |
||||
|
this.result = result; |
||||
|
this.desc = ResultCode.SUCCESS.getDesc(); |
||||
|
this.code = ResultCode.SUCCESS.getCode(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
protected CommonResult(long code, String desc, T result) { |
||||
|
this.code = code; |
||||
|
this.desc = desc; |
||||
|
this.result = result; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 成功返回结果 |
||||
|
|
||||
|
*/ |
||||
|
public static <T> CommonResult<T> success() { |
||||
|
return new CommonResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), null); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 成功返回结果 |
||||
|
* |
||||
|
* @param result 获取的数据 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> success(T result) { |
||||
|
return new CommonResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc(), result); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 成功返回结果 |
||||
|
* |
||||
|
* @param result 获取的数据 |
||||
|
* @param desc 提示信息 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> success(T result, String desc) { |
||||
|
return new CommonResult<>(ResultCode.SUCCESS.getCode(), desc, result); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 失败返回结果 |
||||
|
* @param errorCode 错误码 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> failed(IErrorCode errorCode) { |
||||
|
return new CommonResult<>(errorCode.getCode(), errorCode.getDesc(), null); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 失败返回结果 |
||||
|
* @param errorCode 错误码 |
||||
|
* @param desc 错误信息 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> failed(IErrorCode errorCode,String desc) { |
||||
|
return new CommonResult<>(errorCode.getCode(), desc, null); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 失败返回结果 |
||||
|
* @param desc 提示信息 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> failed(String desc) { |
||||
|
return new CommonResult<>(ResultCode.FAILED.getCode(), desc, null); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 失败返回结果 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> failed() { |
||||
|
return failed(ResultCode.FAILED); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 参数验证失败返回结果 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> validateFailed() { |
||||
|
return failed(ResultCode.VALIDATE_FAILED); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 参数验证失败返回结果 |
||||
|
* @param desc 提示信息 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> validateFailed(String desc) { |
||||
|
return new CommonResult<>(ResultCode.MISS_PARAMETER.getCode(), desc, null); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 未登录返回结果 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> unauthorized(T result) { |
||||
|
return new CommonResult<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getDesc(), result); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 未授权返回结果 |
||||
|
*/ |
||||
|
public static <T> CommonResult<T> forbidden(T result) { |
||||
|
return new CommonResult<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getDesc(), result); |
||||
|
} |
||||
|
|
||||
|
public long getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
public void setCode(long code) { |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
public String getDesc() { |
||||
|
return desc; |
||||
|
} |
||||
|
|
||||
|
public void setDesc(String desc) { |
||||
|
this.desc = desc; |
||||
|
} |
||||
|
|
||||
|
public T getResult() { |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public void setResult(T result) { |
||||
|
this.result = result; |
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package org.nl.modules.common.api; |
||||
|
|
||||
|
/** |
||||
|
* 封装API的错误码 |
||||
|
* |
||||
|
* @author gbx |
||||
|
* @date 2023-03-02 |
||||
|
*/ |
||||
|
public interface IErrorCode{ |
||||
|
/** |
||||
|
* 返回状态码 |
||||
|
*/ |
||||
|
long getCode(); |
||||
|
|
||||
|
/** |
||||
|
* 返回提示 |
||||
|
*/ |
||||
|
String getDesc(); |
||||
|
} |
@ -0,0 +1,85 @@ |
|||||
|
package org.nl.modules.common.api; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.nl.modules.common.exception.BizCoreException; |
||||
|
|
||||
|
/** |
||||
|
* 回调 |
||||
|
* |
||||
|
* @author gbx |
||||
|
* @date 2023-03-02 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class RestBusinessTemplate{ |
||||
|
public static <T> CommonResult<T> execute(Callback<T> callback) { |
||||
|
CommonResult<T> result = new CommonResult<>(); |
||||
|
try { |
||||
|
result.setCode(ResultCode.SUCCESS.getCode()); |
||||
|
result.setDesc(ResultCode.SUCCESS.getDesc()); |
||||
|
T object = callback.doExecute(); |
||||
|
if(object != null) { |
||||
|
result.setResult(object); |
||||
|
} |
||||
|
} |
||||
|
catch(BizCoreException e) { |
||||
|
String errorMsg; |
||||
|
if(StringUtils.isNotBlank(e.getErrorMsg())) { |
||||
|
errorMsg = e.getErrorMsg(); |
||||
|
} |
||||
|
else if(e.getCode() != null) { |
||||
|
errorMsg = e.getCode().getDesc(); |
||||
|
} |
||||
|
else{ |
||||
|
errorMsg = e.getMessage(); |
||||
|
} |
||||
|
log.error(e.getErrorMsg()); |
||||
|
ResultCode code = e.getCode() == null ? ResultCode.FAILED : e.getCode(); |
||||
|
result.setCode(code.getCode()); |
||||
|
result.setDesc(errorMsg); |
||||
|
} |
||||
|
catch(Exception e) { |
||||
|
log.error("execute error", e); |
||||
|
result.setCode(ResultCode.FAILED.getCode()); |
||||
|
result.setDesc(ResultCode.FAILED.getDesc()); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public static CommonResult<Void> execute(VoidCallback callback) { |
||||
|
CommonResult<Void> result = new CommonResult<>(); |
||||
|
try { |
||||
|
callback.execute(); |
||||
|
result.setCode(ResultCode.SUCCESS.getCode()); |
||||
|
result.setDesc(ResultCode.SUCCESS.getDesc()); |
||||
|
} |
||||
|
catch(BizCoreException e) { |
||||
|
log.error("", e); |
||||
|
ResultCode code = e.getCode() == null ? ResultCode.FAILED : e.getCode(); |
||||
|
result.setCode(code.getCode()); |
||||
|
result.setDesc(StringUtils.isBlank(e.getMessage()) ? code.getDesc() : e.getMessage()); |
||||
|
} |
||||
|
catch(Exception e) { |
||||
|
log.error("execute error", e); |
||||
|
result.setCode(ResultCode.FAILED.getCode()); |
||||
|
result.setDesc(ResultCode.FAILED.getDesc()); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 执行回调 |
||||
|
* |
||||
|
* @param <T> |
||||
|
*/ |
||||
|
public interface Callback<T>{ |
||||
|
T doExecute(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 执行回调 |
||||
|
*/ |
||||
|
public interface VoidCallback{ |
||||
|
void execute(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,78 @@ |
|||||
|
package org.nl.modules.common.api; |
||||
|
|
||||
|
/** |
||||
|
* 枚举了一些常用API操作码 |
||||
|
* |
||||
|
* @author gbx |
||||
|
* @date 2023-03-02 |
||||
|
*/ |
||||
|
public enum ResultCode implements IErrorCode{ |
||||
|
SUCCESS(200, "操作成功"), |
||||
|
FAILED(500, "操作失败"), |
||||
|
MISS_PARAMETER(400, "参数缺失"), |
||||
|
UNAUTHORIZED(401, "暂未登录或token已经过期"), |
||||
|
INVALID_PARAMETER(402, "无效参数"), |
||||
|
FORBIDDEN(403, "没有相关权限"), |
||||
|
VALIDATE_FAILED(404, "参数检验失败"), |
||||
|
INVALID_CAPTCHA(503, "验证码已失效或验证码错误"), |
||||
|
/** |
||||
|
* 员工相关异常 1000 ~ 1199 |
||||
|
*/ |
||||
|
STAFF_NOT_FOUND(1000, "员工不存在"), |
||||
|
STAFF_EXISTED(1001, "员工已存在"), |
||||
|
/** |
||||
|
* 部门相关异常 1200 ~ 1399 |
||||
|
*/ |
||||
|
DEPT_EXIST(1200, "部门已存在"), |
||||
|
PARENT_DEPT_STATUS_EXCEPTION(1201, "父部门状态异常"), |
||||
|
DEPT_NOT_EXIST(1202, "部门不存在"), |
||||
|
/** |
||||
|
* 岗位相关异常 1400 ~ 1599 |
||||
|
*/ |
||||
|
POST_EXIST(1400, "岗位已存在"), |
||||
|
POST_NOT_EXIST(1401, "岗位不存在"), |
||||
|
/** |
||||
|
* 菜单相关异常 1600 ~ 1799 |
||||
|
*/ |
||||
|
MENU_EXIST(1600, "菜单已存在"), |
||||
|
MENU_NOT_EXIST(1601, "菜单不存在"), |
||||
|
MENU_STATUS_EXCEPTION(1602, "菜单状态异常"), |
||||
|
/** |
||||
|
* 角色相关异常 1800 ~ 1999 |
||||
|
*/ |
||||
|
ROLE_EXIST(1800, "角色已存在"), |
||||
|
ROLE_NOT_EXIST(1801, "角色不存在"), |
||||
|
/** |
||||
|
* 账户相关异常 2000 ~ 2099 |
||||
|
*/ |
||||
|
ACCOUNT_EXCEPTION(2000, "账户异常"), |
||||
|
INVALID_RECHARGE_AMOUNT(2001, "无效金额"), |
||||
|
BALANCE_NOT_ENOUGH(2002, "余额不足"), |
||||
|
ACCOUNT_NOT_EXIST(2003, "账户不存在"), |
||||
|
STORAGE_NOT_ENOUGH(2004, "库存不足"), |
||||
|
IS_EXCHANGED(2005, "已有兑换"), |
||||
|
/** |
||||
|
* 短信相关 2100 ~ 2199 |
||||
|
*/ |
||||
|
ERR_MESSAGE(2100, "短信发送失败"), |
||||
|
ERR_SEND_LIMIT(2101, "短信发送上限"), |
||||
|
ERR_PHONE(2102, "短信号码不正确"), |
||||
|
ERR_Content(2103, "短信内容不能为空"); |
||||
|
private final long code; |
||||
|
private final String desc; |
||||
|
|
||||
|
ResultCode(long code, String desc) { |
||||
|
this.code = code; |
||||
|
this.desc = desc; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public long getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getDesc() { |
||||
|
return desc; |
||||
|
} |
||||
|
} |
@ -0,0 +1,88 @@ |
|||||
|
package org.nl.modules.common.exception; |
||||
|
|
||||
|
import org.nl.modules.common.api.ResultCode; |
||||
|
|
||||
|
/** |
||||
|
* 业务异常 |
||||
|
* |
||||
|
* @author gbx |
||||
|
* @date 2022-03-21 |
||||
|
*/ |
||||
|
public class BizCoreException extends RuntimeException{ |
||||
|
private static final long serialVersionUID = -430613633714952314L; |
||||
|
/** |
||||
|
* 错误描述,生成错误响应时,如果该值不为空,则返回message,否则返回code对象的描述值 |
||||
|
*/ |
||||
|
private String errorMsg; |
||||
|
private ResultCode code = ResultCode.FAILED; |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(String message) { |
||||
|
super(message); |
||||
|
this.errorMsg = message; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(String message, ResultCode code) { |
||||
|
super(message); |
||||
|
this.errorMsg = message; |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(ResultCode code) { |
||||
|
super(code.getDesc()); |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(String message, Throwable e) { |
||||
|
super(message, e); |
||||
|
this.errorMsg = message; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(String message, ResultCode code, Throwable e) { |
||||
|
super(message, e); |
||||
|
this.errorMsg = message; |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(Throwable e) { |
||||
|
super(e); |
||||
|
this.errorMsg = e.getMessage(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
*/ |
||||
|
public BizCoreException(ResultCode code, Throwable e) { |
||||
|
super(e); |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
public ResultCode getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
public void setCode(ResultCode code) { |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
public String getErrorMsg() { |
||||
|
return this.errorMsg; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue