diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 8cb328656..d774250a1 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -135,6 +135,11 @@ yudao-spring-boot-starter-biz-social ${revision} + + cn.iocoder.cloud + yudao-spring-boot-starter-biz-error-code + ${revision} + diff --git a/yudao-framework/pom.xml b/yudao-framework/pom.xml index e5ad00428..b7afee01b 100644 --- a/yudao-framework/pom.xml +++ b/yudao-framework/pom.xml @@ -19,6 +19,7 @@ yudao-spring-boot-starter-file yudao-spring-boot-starter-monitor yudao-spring-boot-starter-protection + yudao-spring-boot-starter-job yudao-spring-boot-starter-mq yudao-spring-boot-starter-rpc @@ -29,12 +30,15 @@ yudao-spring-boot-starter-biz-operatelog yudao-spring-boot-starter-biz-dict yudao-spring-boot-starter-biz-sms - yudao-spring-boot-starter-activiti + yudao-spring-boot-starter-biz-pay yudao-spring-boot-starter-biz-weixin yudao-spring-boot-starter-biz-social yudao-spring-boot-starter-biz-tenant yudao-spring-boot-starter-biz-data-permission + yudao-spring-boot-starter-biz-error-code + + yudao-spring-boot-starter-activiti yudao-spring-boot-starter-flowable diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/ServerException.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/ServerException.java new file mode 100644 index 000000000..fac56d5e2 --- /dev/null +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/ServerException.java @@ -0,0 +1,60 @@ +package cn.iocoder.yudao.framework.common.exception; + +import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 服务器异常 Exception + */ +@Data +@EqualsAndHashCode(callSuper = true) +public final class ServerException extends RuntimeException { + + /** + * 全局错误码 + * + * @see GlobalErrorCodeConstants + */ + private Integer code; + /** + * 错误提示 + */ + private String message; + + /** + * 空构造方法,避免反序列化问题 + */ + public ServerException() { + } + + public ServerException(ErrorCode errorCode) { + this.code = errorCode.getCode(); + this.message = errorCode.getMsg(); + } + + public ServerException(Integer code, String message) { + this.code = code; + this.message = message; + } + + public Integer getCode() { + return code; + } + + public ServerException setCode(Integer code) { + this.code = code; + return this; + } + + @Override + public String getMessage() { + return message; + } + + public ServerException setMessage(String message) { + this.message = message; + return this; + } + +} diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/enums/GlobalErrorCodeConstants.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/enums/GlobalErrorCodeConstants.java index 69b97e5ae..a132e63d7 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/enums/GlobalErrorCodeConstants.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/enums/GlobalErrorCodeConstants.java @@ -36,9 +36,15 @@ public interface GlobalErrorCodeConstants { ErrorCode UNKNOWN = new ErrorCode(999, "未知错误"); - static boolean isMatch(Integer code) { + /** + * 是否为服务端错误,参考 HTTP 5XX 错误码段 + * + * @param code 错误码 + * @return 是否 + */ + static boolean isServerErrorCode(Integer code) { return code != null - && code >= SUCCESS.getCode() && code <= UNKNOWN.getCode(); + && code >= INTERNAL_SERVER_ERROR.getCode() && code <= INTERNAL_SERVER_ERROR.getCode() + 99; } } diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/pojo/CommonResult.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/pojo/CommonResult.java index eb6124b8b..bfb291b6d 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/pojo/CommonResult.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/pojo/CommonResult.java @@ -95,6 +95,16 @@ public class CommonResult implements Serializable { throw new ServiceException(code, msg); } + /** + * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常 + * 如果没有,则返回 {@link #data} 数据 + */ + @JsonIgnore // 避免 jackson 序列化 + public T getCheckedData() { + checkError(); + return data; + } + public static CommonResult error(ServiceException serviceException) { return error(serviceException.getCode(), serviceException.getMessage()); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java b/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java index 801db32f4..85067439e 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/dept/DeptDataPermissionRule.java @@ -106,9 +106,7 @@ public class DeptDataPermissionRule implements DataPermissionRule { DeptDataPermissionRespDTO deptDataPermission = loginUser.getContext(CONTEXT_KEY, DeptDataPermissionRespDTO.class); // 从上下文中拿不到,则调用逻辑进行获取 if (deptDataPermission == null) { - CommonResult getDeptDataPermissionResult = permissionApi.getDeptDataPermission(loginUser.getId()); - getDeptDataPermissionResult.checkError(); - deptDataPermission = getDeptDataPermissionResult.getData(); + deptDataPermission = permissionApi.getDeptDataPermission(loginUser.getId()).getData(); if (deptDataPermission == null) { log.error("[getExpression][LoginUser({}) 获取数据权限为 null]", JsonUtils.toJsonString(loginUser)); throw new NullPointerException(String.format("LoginUser(%d) Table(%s/%s) 未返回数据权限", diff --git a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java index 0ea5a7608..e56139514 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java @@ -34,9 +34,8 @@ public class DictFrameworkUtils { @Override public DictDataRespDTO load(KeyValue key) { - CommonResult getDictDataResult = dictDataApi.getDictData(key.getKey(), key.getValue()); - getDictDataResult.checkError(); - return ObjectUtil.defaultIfNull(getDictDataResult.getData(), DICT_DATA_NULL); + return ObjectUtil.defaultIfNull(dictDataApi.getDictData(key.getKey(), key.getValue()).getCheckedData(), + DICT_DATA_NULL); } }); @@ -50,9 +49,8 @@ public class DictFrameworkUtils { @Override public DictDataRespDTO load(KeyValue key) { - CommonResult parseDictDataResult = dictDataApi.parseDictData(key.getKey(), key.getValue()); - parseDictDataResult.checkError(); - return ObjectUtil.defaultIfNull(parseDictDataResult.getData(), DICT_DATA_NULL); + return ObjectUtil.defaultIfNull(dictDataApi.parseDictData(key.getKey(), key.getValue()).getCheckedData(), + DICT_DATA_NULL); } }); diff --git a/yudao-framework/yudao-spring-boot-starter-biz-operatelog/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-operatelog/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java index b7d49c9eb..845889d83 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-operatelog/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-operatelog/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java @@ -23,8 +23,7 @@ public class OperateLogFrameworkServiceImpl implements OperateLogFrameworkServic @Async public void createOperateLog(OperateLog operateLog) { OperateLogCreateReqDTO reqDTO = BeanUtil.copyProperties(operateLog, OperateLogCreateReqDTO.class); - CommonResult result = operateLogApi.createOperateLog(reqDTO); - result.checkError(); + operateLogApi.createOperateLog(reqDTO).checkError(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java index a21107837..aff8965b1 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java @@ -30,9 +30,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService { @Override public List load(Object key) { - CommonResult> tenantIdsResult = tenantApi.getTenantIds(); - tenantIdsResult.checkError(); - return tenantIdsResult.getData(); + return tenantApi.getTenantIds().getCheckedData(); } }); diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/filter/TokenAuthenticationFilter.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/filter/TokenAuthenticationFilter.java index 8ccbb8fe6..6efd48340 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/filter/TokenAuthenticationFilter.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/filter/TokenAuthenticationFilter.java @@ -77,9 +77,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter { private LoginUser buildLoginUserByToken(String token, Integer userType) { try { // 校验访问令牌 - CommonResult accessTokenResult = oauth2TokenApi.checkAccessToken(token); - accessTokenResult.checkError(); - OAuth2AccessTokenCheckRespDTO accessToken = accessTokenResult.getData(); + OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token).getCheckedData(); if (accessToken == null) { return null; } diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java index 26975f633..a3e4ad98c 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java @@ -38,13 +38,12 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService { private final LoadingCache>, Boolean> hasAnyRolesCache = CacheUtils.buildAsyncReloadingCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader>, Boolean>() { + @Override public Boolean load(KeyValue> key) { - CommonResult hasAnyRolesResult = permissionApi.hasAnyRoles(key.getKey(), - key.getValue().toArray(new String[0])); - hasAnyRolesResult.checkError(); - return hasAnyRolesResult.getData(); + return permissionApi.hasAnyRoles(key.getKey(), key.getValue().toArray(new String[0])).getCheckedData(); } + }); /** @@ -53,13 +52,12 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService { private final LoadingCache>, Boolean> hasAnyPermissionsCache = CacheUtils.buildAsyncReloadingCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader>, Boolean>() { + @Override public Boolean load(KeyValue> key) { - CommonResult hasAnyPermissionsResult = permissionApi.hasAnyPermissions(key.getKey(), - key.getValue().toArray(new String[0])); - hasAnyPermissionsResult.checkError(); - return hasAnyPermissionsResult.getData(); + return permissionApi.hasAnyPermissions(key.getKey(), key.getValue().toArray(new String[0])).getCheckedData(); } + }); @Override diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java index 8ea5ebbc1..826897263 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java @@ -23,8 +23,7 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe @Async public void createApiAccessLog(ApiAccessLog apiAccessLog) { ApiAccessLogCreateReqDTO reqDTO = BeanUtil.copyProperties(apiAccessLog, ApiAccessLogCreateReqDTO.class); - CommonResult result = apiAccessLogApi.createApiAccessLog(reqDTO); - result.checkError(); + apiAccessLogApi.createApiAccessLog(reqDTO).checkError(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java index 4db4f59ea..d400faa7f 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java @@ -23,8 +23,7 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ @Async public void createApiErrorLog(ApiErrorLog apiErrorLog) { ApiErrorLogCreateReqDTO reqDTO = BeanUtil.copyProperties(apiErrorLog, ApiErrorLogCreateReqDTO.class); - CommonResult result = apiErrorLogApi.createApiErrorLog(reqDTO); - result.checkError(); + apiErrorLogApi.createApiErrorLog(reqDTO).checkError(); } } diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java index e58d14833..ddb73046c 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java @@ -50,9 +50,7 @@ public interface FileApi { default String createFile(@RequestParam("name") String name, @RequestParam("path") String path, @RequestParam("content") byte[] content) { - CommonResult result = createFile(new FileCreateReqDTO().setName(name).setPath(path).setContent(content)); - result.checkError(); - return result.getData(); + return createFile(new FileCreateReqDTO().setName(name).setPath(path).setContent(content)).getCheckedData(); } @PostMapping(PREFIX + "/create") diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApi.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApi.java index a5dbf3c12..b52346a77 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApi.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApi.java @@ -44,9 +44,7 @@ public interface DeptApi { * @return 部门 Map */ default Map getDeptMap(Set ids) { - CommonResult> result = getDepts(ids); - result.checkError(); - return CollectionUtils.convertMap(result.getData(), DeptRespDTO::getId); + return CollectionUtils.convertMap(getDepts(ids).getCheckedData(), DeptRespDTO::getId); } } diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java new file mode 100644 index 000000000..e12da0078 --- /dev/null +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java @@ -0,0 +1,40 @@ +package cn.iocoder.yudao.module.system.api.errorcode; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; +import cn.iocoder.yudao.module.system.enums.ApiConstants; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.validation.Valid; +import java.util.Date; +import java.util.List; + +@FeignClient(name = ApiConstants.NAME) // TODO 芋艿:fallbackFactory = +@Api(tags = "RPC 服务 - 错误码") +public interface ErrorCodeApi { + + String PREFIX = ApiConstants.PREFIX + "/oauth2/token"; + + @PostMapping(PREFIX + "/auto-generate") + @ApiOperation("自动创建错误码") + CommonResult autoGenerateErrorCodes(@Valid @RequestBody List autoGenerateDTOs); + + @GetMapping(PREFIX + "/list") + @ApiOperation(value = "增量获得错误码数组", notes = "如果 minUpdateTime 为空时,则获取所有错误码") + @ApiImplicitParams({ + @ApiImplicitParam(name = "applicationName", value = "应用名", example = "system-server", required = true, dataTypeClass = String.class), + @ApiImplicitParam(name = "minUpdateTime", value = "最小更新时间", dataTypeClass = Date.class) + }) + CommonResult> getErrorCodeList(@RequestParam(value = "applicationName") String applicationName, + @RequestParam(value = "minUpdateTime", required = false) Date minUpdateTime); + +} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/dto/ErrorCodeAutoGenerateReqDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java similarity index 90% rename from yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/dto/ErrorCodeAutoGenerateReqDTO.java rename to yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java index 6f5425869..da4df83ca 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/dto/ErrorCodeAutoGenerateReqDTO.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.module.system.framework.errorcode.core.dto; +package cn.iocoder.yudao.module.system.api.errorcode.dto; import lombok.Data; import lombok.experimental.Accessors; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/dto/ErrorCodeRespDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java similarity index 83% rename from yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/dto/ErrorCodeRespDTO.java rename to yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java index c0b0cbaa3..1f5a27f8a 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/dto/ErrorCodeRespDTO.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.module.system.framework.errorcode.core.dto; +package cn.iocoder.yudao.module.system.api.errorcode.dto; import lombok.Data; diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApi.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApi.java index 91dd07dfd..86fe82c7e 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApi.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/user/AdminUserApi.java @@ -49,9 +49,7 @@ public interface AdminUserApi { * @return 用户 Map */ default Map getUserMap(Collection ids) { - CommonResult> getUsersResult = getUsers(ids); - getUsersResult.checkError(); - return CollectionUtils.convertMap(getUsersResult.getData(), AdminUserRespDTO::getId); + return CollectionUtils.convertMap(getUsers(ids).getCheckedData(), AdminUserRespDTO::getId); } @GetMapping(PREFIX + "/valid") diff --git a/yudao-module-system/yudao-module-system-biz/pom.xml b/yudao-module-system/yudao-module-system-biz/pom.xml index de52bf4f9..50c31f866 100644 --- a/yudao-module-system/yudao-module-system-biz/pom.xml +++ b/yudao-module-system/yudao-module-system-biz/pom.xml @@ -61,6 +61,10 @@ cn.iocoder.cloud yudao-spring-boot-starter-biz-tenant + + cn.iocoder.cloud + yudao-spring-boot-starter-biz-error-code + diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java new file mode 100644 index 000000000..b82ebe3e4 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.module.system.api.errorcode; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; +import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService; +import org.apache.dubbo.config.annotation.DubboService; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION; + +@RestController // 提供 RESTful API 接口,给 Feign 调用 +@DubboService(version = VERSION) // 提供 Dubbo RPC 接口,给 Dubbo Consumer 调用 +@Validated +public class ErrorCodeApiImpl implements ErrorCodeApi { + + @Resource + private ErrorCodeService errorCodeService; + + @Override + public CommonResult autoGenerateErrorCodes(List autoGenerateDTOs) { + errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs); + return success(true); + } + + @Override + public CommonResult> getErrorCodeList(String applicationName, Date minUpdateTime) { + return success(errorCodeService.getErrorCodeList(applicationName, minUpdateTime)); + } +} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/errorcode/ErrorCodeConvert.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/errorcode/ErrorCodeConvert.java index 0402aca8b..3e1abd86b 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/errorcode/ErrorCodeConvert.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/errorcode/ErrorCodeConvert.java @@ -1,12 +1,12 @@ package cn.iocoder.yudao.module.system.convert.errorcode; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExcelVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeRespVO; import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeRespDTO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeUpdateReqVO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/service/ErrorCodeFrameworkService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/service/ErrorCodeFrameworkService.java deleted file mode 100644 index 2d93f75f1..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/core/service/ErrorCodeFrameworkService.java +++ /dev/null @@ -1,35 +0,0 @@ -package cn.iocoder.yudao.module.system.framework.errorcode.core.service; - -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeRespDTO; - -import javax.validation.Valid; -import java.util.Date; -import java.util.List; - -/** - * 错误码 Framework Service 接口 - * - * @author 芋道源码 - */ -public interface ErrorCodeFrameworkService { - - /** - * 自动创建错误码 - * - * @param autoGenerateDTOs 错误码信息 - */ - void autoGenerateErrorCodes(@Valid List autoGenerateDTOs); - - /** - * 增量获得错误码数组 - * - * 如果 minUpdateTime 为空时,则获取所有错误码 - * - * @param applicationName 应用名 - * @param minUpdateTime 最小更新时间 - * @return 错误码数组 - */ - List getErrorCodeList(String applicationName, Date minUpdateTime); - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/package-info.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/package-info.java deleted file mode 100644 index 012bcb33c..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/errorcode/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -/** - * 错误码组件 - * - * 将错误码缓存在内存中,同时通过定时器每 n 分钟更新 - */ -package cn.iocoder.yudao.module.system.framework.errorcode; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java index 18875f628..16025645e 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java @@ -1,14 +1,16 @@ package cn.iocoder.yudao.module.system.service.errorcode; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.service.ErrorCodeFrameworkService; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeUpdateReqVO; import javax.validation.Valid; +import java.util.Date; import java.util.List; /** @@ -16,7 +18,25 @@ import java.util.List; * * @author 芋道源码 */ -public interface ErrorCodeService extends ErrorCodeFrameworkService { +public interface ErrorCodeService { + + /** + * 自动创建错误码 + * + * @param autoGenerateDTOs 错误码信息 + */ + void autoGenerateErrorCodes(@Valid List autoGenerateDTOs); + + /** + * 增量获得错误码数组 + * + * 如果 minUpdateTime 为空时,则获取所有错误码 + * + * @param applicationName 应用名 + * @param minUpdateTime 最小更新时间 + * @return 错误码数组 + */ + List getErrorCodeList(String applicationName, Date minUpdateTime); /** * 创建错误码 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java index a34f098cd..2997e32ec 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java @@ -2,14 +2,14 @@ package cn.iocoder.yudao.module.system.service.errorcode; import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeUpdateReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeRespDTO; -import cn.iocoder.yudao.module.system.convert.errorcode.ErrorCodeConvert; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; +import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeUpdateReqVO; +import cn.iocoder.yudao.module.system.convert.errorcode.ErrorCodeConvert; +import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; import cn.iocoder.yudao.module.system.dal.mysql.errorcode.ErrorCodeMapper; import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; import com.google.common.annotations.VisibleForTesting; @@ -24,9 +24,10 @@ import java.util.List; import java.util.Map; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_DUPLICATE; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_NOT_EXISTS; /** * 错误码 Service 实现类 diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java index 37d542c14..c8181d8a2 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java @@ -1,8 +1,8 @@ package cn.iocoder.yudao.module.system.service.errorcode; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.framework.errorcode.core.dto.ErrorCodeAutoGenerateReqDTO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; diff --git a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MathUtil.java b/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MathUtil.java deleted file mode 100644 index 4aafa732e..000000000 --- a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MathUtil.java +++ /dev/null @@ -1,33 +0,0 @@ -package cn.iocoder.common.framework.util; - -import java.util.Random; - -public class MathUtil { - - /** - * 随机对象 - */ - private static final Random RANDOM = new Random(); // TODO 后续优化 - - /** - * 随机[min, max]范围内的数字 - * - * @param min 随机开始 - * @param max 随机结束 - * @return 数字 - */ - public static int random(int min, int max) { - if (min == max) { - return min; - } - if (min > max) { - int temp = min; - min = max; - max = temp; - } - // 随即开始 - int diff = max - min + 1; - return RANDOM.nextInt(diff) + min; - } - -} diff --git a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/util/OSUtils.java b/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/util/OSUtils.java deleted file mode 100644 index 930902c7d..000000000 --- a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/util/OSUtils.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.iocoder.common.framework.util; - -import cn.hutool.system.SystemUtil; - -/** - * 操作系统工具类 - */ -public class OSUtils { - - public static String getHostName() { - return SystemUtil.getHostInfo().getName(); - } - -} diff --git a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java b/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java deleted file mode 100644 index dfaec09f9..000000000 --- a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java +++ /dev/null @@ -1,154 +0,0 @@ -package cn.iocoder.common.framework.vo; - -import cn.iocoder.common.framework.exception.ErrorCode; -import cn.iocoder.common.framework.exception.GlobalException; -import cn.iocoder.common.framework.exception.ServiceException; -import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants; -import com.alibaba.fastjson.annotation.JSONField; -import org.springframework.util.Assert; - -import java.io.Serializable; - -/** - * 通用返回 - * - * @param 数据泛型 - */ -public final class CommonResult implements Serializable { - - /** - * 错误码 - * - * @see ErrorCode#getCode() - */ - private Integer code; - /** - * 返回数据 - */ - private T data; - /** - * 错误提示,用户可阅读 - * - * @see ErrorCode#getMessage() () - */ - private String message; - /** - * 错误明细,内部调试错误 - */ - private String detailMessage; - - /** - * 将传入的 result 对象,转换成另外一个泛型结果的对象 - * - * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。 - * - * @param result 传入的 result 对象 - * @param 返回的泛型 - * @return 新的 CommonResult 对象 - */ - public static CommonResult error(CommonResult result) { - return error(result.getCode(), result.getMessage(), result.detailMessage); - } - - public static CommonResult error(Integer code, String message) { - return error(code, message, null); - } - - public static CommonResult error(Integer code, String message, String detailMessage) { - Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!"); - CommonResult result = new CommonResult<>(); - result.code = code; - result.message = message; - result.detailMessage = detailMessage; - return result; - } - - public static CommonResult success(T data) { - CommonResult result = new CommonResult<>(); - result.code = GlobalErrorCodeConstants.SUCCESS.getCode(); - result.data = data; - result.message = ""; - return result; - } - - public Integer getCode() { - return code; - } - - public void setCode(Integer code) { - this.code = code; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public T getData() { - return data; - } - - public void setData(T data) { - this.data = data; - } - - public String getDetailMessage() { - return detailMessage; - } - - public CommonResult setDetailMessage(String detailMessage) { - this.detailMessage = detailMessage; - return this; - } - - @JSONField(serialize = false) // 避免序列化 - public boolean isSuccess() { - return GlobalErrorCodeConstants.SUCCESS.getCode().equals(code); - } - - @JSONField(serialize = false) // 避免序列化 - public boolean isError() { - return !isSuccess(); - } - - @Override - public String toString() { - return "CommonResult{" + - "code=" + code + - ", data=" + data + - ", message='" + message + '\'' + - ", detailMessage='" + detailMessage + '\'' + - '}'; - } - - // ========= 和 Exception 异常体系集成 ========= - - /** - * 判断是否有异常。如果有,则抛出 {@link GlobalException} 或 {@link ServiceException} 异常 - */ - public void checkError() throws GlobalException, ServiceException { - if (isSuccess()) { - return; - } - // 全局异常 - if (GlobalErrorCodeConstants.isMatch(code)) { - throw new GlobalException(code, message).setDetailMessage(detailMessage); - } - // 业务异常 - throw new ServiceException(code, message).setDetailMessage(detailMessage); - } - - public static CommonResult error(ServiceException serviceException) { - return error(serviceException.getCode(), serviceException.getMessage(), - serviceException.getDetailMessage()); - } - - public static CommonResult error(GlobalException globalException) { - return error(globalException.getCode(), globalException.getMessage(), - globalException.getDetailMessage()); - } - -} diff --git a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/PageParam.java b/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/PageParam.java deleted file mode 100644 index 6f371430f..000000000 --- a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/PageParam.java +++ /dev/null @@ -1,46 +0,0 @@ -package cn.iocoder.common.framework.vo; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import org.hibernate.validator.constraints.Range; - -import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; -import java.io.Serializable; - -@ApiModel("分页参数") -public class PageParam implements Serializable { - - @ApiModelProperty(value = "页码,从 1 开始", required = true,example = "1") - @NotNull(message = "页码不能为空") - @Min(value = 1, message = "页码最小值为 1") - private Integer pageNo; - - @ApiModelProperty(value = "每页条数,最大值为 100", required = true, example = "10") - @NotNull(message = "每页条数不能为空") - @Range(min = 1, max = 100, message = "条数范围为 [1, 100]") - private Integer pageSize; - - public Integer getPageNo() { - return pageNo; - } - - public PageParam setPageNo(Integer pageNo) { - this.pageNo = pageNo; - return this; - } - - public Integer getPageSize() { - return pageSize; - } - - public PageParam setPageSize(Integer pageSize) { - this.pageSize = pageSize; - return this; - } - -// public final int getOffset() { -// return (pageNo - 1) * pageSize; -// } - -} diff --git a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/PageResult.java b/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/PageResult.java deleted file mode 100644 index eb60ea714..000000000 --- a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/PageResult.java +++ /dev/null @@ -1,36 +0,0 @@ -package cn.iocoder.common.framework.vo; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; - -import java.io.Serializable; -import java.util.List; - -@ApiModel("分页结果") -public final class PageResult implements Serializable { - - @ApiModelProperty(value = "数据", required = true) - private List list; - - @ApiModelProperty(value = "总量", required = true) - private Long total; - - public List getList() { - return list; - } - - public PageResult setList(List list) { - this.list = list; - return this; - } - - public Long getTotal() { - return total; - } - - public PageResult setTotal(Long total) { - this.total = total; - return this; - } - -} diff --git a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/SortingField.java b/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/SortingField.java deleted file mode 100644 index e592c0470..000000000 --- a/归档/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/SortingField.java +++ /dev/null @@ -1,56 +0,0 @@ -package cn.iocoder.common.framework.vo; - -import java.io.Serializable; - -/** - * 排序字段 DTO - * - * 类名加了 ing 的原因是,避免和 ES SortField 重名。 - */ -public class SortingField implements Serializable { - - /** - * 顺序 - 升序 - */ - public static final String ORDER_ASC = "asc"; - /** - * 顺序 - 降序 - */ - public static final String ORDER_DESC = "desc"; - - /** - * 字段 - */ - private String field; - /** - * 顺序 - */ - private String order; - - // 空构造方法,解决反序列化 - public SortingField() { - } - - public SortingField(String field, String order) { - this.field = field; - this.order = order; - } - - public String getField() { - return field; - } - - public SortingField setField(String field) { - this.field = field; - return this; - } - - public String getOrder() { - return order; - } - - public SortingField setOrder(String order) { - this.order = order; - return this; - } -} diff --git a/归档/common/mall-spring-boot-starter-system-error-code/pom.xml b/归档/common/mall-spring-boot-starter-system-error-code/pom.xml deleted file mode 100644 index 632b313ef..000000000 --- a/归档/common/mall-spring-boot-starter-system-error-code/pom.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - common - cn.iocoder.mall - 1.0-SNAPSHOT - - 4.0.0 - - mall-spring-boot-starter-system-error-code - - 错误码 ErrorCode 的自动配置功能,提供如下功能: - 1. 远程读取:项目启动时,从 system-service 服务,读取数据库中的 ErrorCode 错误码,实现错误码的提水可配置; - 2. 自动更新:管理员在管理后台修数据库中的 ErrorCode 错误码时,项目自动从 system-service 服务加载最新的 ErrorCode 错误码; - 3. 自动写入:项目启动时,将项目本地的错误码写到 system-service 服务中,方便管理员在管理后台编辑; - - - - - - cn.iocoder.mall - system-service-api - 1.0-SNAPSHOT - - - - - org.springframework.boot - spring-boot-starter - - - org.springframework.boot - spring-boot-configuration-processor - true - - - - - org.apache.dubbo - dubbo - - - - diff --git a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java b/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java deleted file mode 100644 index 1d929e97d..000000000 --- a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeAutoConfiguration.java +++ /dev/null @@ -1,26 +0,0 @@ -package cn.iocoder.mall.system.errorcode.config; - -import cn.iocoder.mall.system.errorcode.core.ErrorCodeAutoGenerator; -import cn.iocoder.mall.system.errorcode.core.ErrorCodeRemoteLoader; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.scheduling.annotation.EnableScheduling; - -@Configuration -@EnableConfigurationProperties(ErrorCodeProperties.class) -@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码 -public class ErrorCodeAutoConfiguration { - -// @Bean -// public ErrorCodeAutoGenerator errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) { -// return new ErrorCodeAutoGenerator(errorCodeProperties.getGroup()) -// .setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass()); -// } -// -// @Bean -// public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) { -// return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup()); -// } - -} diff --git a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java b/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java deleted file mode 100644 index 7e9aef0b1..000000000 --- a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/config/ErrorCodeProperties.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.iocoder.mall.system.errorcode.config; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.validation.annotation.Validated; - -import javax.validation.constraints.NotNull; - -@ConfigurationProperties("mall.error-code") -@Validated -public class ErrorCodeProperties { - - /** - * 应用分组 - */ - @NotNull(message = "应用分组不能为空,请设置 mall.error-code.group 配置项,推荐直接使用 spring. application.name 配置项") - private String group; - /** - * 错误码枚举类 - */ - private String constantsClass; - - public String getGroup() { - return group; - } - - public ErrorCodeProperties setGroup(String group) { - this.group = group; - return this; - } - - public String getConstantsClass() { - return constantsClass; - } - - public ErrorCodeProperties setConstantsClass(String constantsClass) { - this.constantsClass = constantsClass; - return this; - } -} diff --git a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java b/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java deleted file mode 100644 index 5b82c8b6b..000000000 --- a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeAutoGenerator.java +++ /dev/null @@ -1,84 +0,0 @@ -package cn.iocoder.mall.system.errorcode.core; - -import cn.iocoder.common.framework.exception.ErrorCode; -import cn.iocoder.common.framework.util.StringUtils; -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign; -import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.event.EventListener; -import org.springframework.scheduling.annotation.Async; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class ErrorCodeAutoGenerator { -// -// private Logger logger = LoggerFactory.getLogger(ErrorCodeAutoGenerator.class); -// -// /** -// * 应用分组 -// */ -// private final String group; -// /** -// * 错误码枚举类 -// */ -// private String errorCodeConstantsClass; -// -// -// @Autowired -// private ErrorCodeFeign errorCodeFeign; -// public ErrorCodeAutoGenerator(String group) { -// this.group = group; -// } -// -// public ErrorCodeAutoGenerator setErrorCodeConstantsClass(String errorCodeConstantsClass) { -// this.errorCodeConstantsClass = errorCodeConstantsClass; -// return this; -// } -// -// @EventListener(ApplicationReadyEvent.class) -// @Async // 异步,保证项目的启动过程,毕竟非关键流程 -// public void execute() { -// // 校验 errorCodeConstantsClass 参数 -// if (!StringUtils.hasText(errorCodeConstantsClass)) { -// logger.info("[execute][未配置 mall.error-code.constants-class 配置项,不进行自动写入到 system-service 服务]"); -// return; -// } -// Class errorCodeConstantsClazz; -// try { -// errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass); -// } catch (ClassNotFoundException e) { -// logger.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass); -// return; -// } -// // 写入 system-service 服务 -// logger.info("[execute][自动将 ({}) 类的错误码,准备写入到 system-service 服务]", errorCodeConstantsClass); -// List autoGenerateDTOs = new ArrayList<>(); -// Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> { -// if (field.getType() != ErrorCode.class) { -// return; -// } -// try { -// // TODO 芋艿:校验是否重复了; -// ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz); -// autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group) -// .setCode(errorCode.getCode()).setMessage(errorCode.getMessage())); -// } catch (IllegalAccessException e) { -// throw new RuntimeException(e); -// } -// }); -// CommonResult autoGenerateErrorCodesResult = errorCodeFeign.autoGenerateErrorCodes(autoGenerateDTOs); -// if (autoGenerateErrorCodesResult.isSuccess()) { -// logger.info("[execute][自动将 ({}) 类的错误码,成功写入到 system-service 服务]", errorCodeConstantsClass); -// } else { -// logger.error("[execute][自动将 ({}) 类的错误码,失败写入到 system-service 服务,原因为 ({}/{}/{})]", errorCodeConstantsClass, -// autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMessage(), autoGenerateErrorCodesResult.getDetailMessage()); -// } -// } - -} diff --git a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java b/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java deleted file mode 100644 index 2fd5d8e3b..000000000 --- a/归档/common/mall-spring-boot-starter-system-error-code/src/main/java/cn/iocoder/mall/system/errorcode/core/ErrorCodeRemoteLoader.java +++ /dev/null @@ -1,70 +0,0 @@ -package cn.iocoder.mall.system.errorcode.core; - -import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil; -import cn.iocoder.common.framework.util.CollectionUtils; -import cn.iocoder.common.framework.util.DateUtil; -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign; -import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.event.EventListener; -import org.springframework.scheduling.annotation.Scheduled; - -import java.util.Date; -import java.util.List; - -public class ErrorCodeRemoteLoader { -// -// private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000; -// -// private Logger logger = LoggerFactory.getLogger(ErrorCodeRemoteLoader.class); -// -// /** -// * 应用分组 -// */ -// private final String group; -// -// @Autowired -// private ErrorCodeFeign errorCodeFeign; -// private Date maxUpdateTime; -// -// public ErrorCodeRemoteLoader(String group) { -// this.group = group; -// } -// -// @EventListener(ApplicationReadyEvent.class) -// public void loadErrorCodes() { -// // 从 errorCodeFeign 全量加载 ErrorCode 错误码 -// CommonResult> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, null); -// listErrorCodesResult.checkError(); -// logger.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size()); -// // 写入到 ServiceExceptionUtil 到 -// listErrorCodesResult.getData().forEach(errorCodeVO -> { -// ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage()); -// // 记录下更新时间,方便增量更新 -// maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime()); -// }); -// } -// -// @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) -// public void refreshErrorCodes() { -// // 从 errorCodeFeign 增量加载 ErrorCode 错误码 -// // TODO 优化点:假设删除错误码的配置,会存在问题; -// CommonResult> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, maxUpdateTime); -// listErrorCodesResult.checkError(); -// if (CollectionUtils.isEmpty(listErrorCodesResult.getData())) { -// return; -// } -// logger.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size()); -// // 写入到 ServiceExceptionUtil 到 -// listErrorCodesResult.getData().forEach(errorCodeVO -> { -// ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage()); -// // 记录下更新时间,方便增量更新 -// maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime()); -// }); -// } - -}