mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
1、新增system服务国际化配置文件;2、资金管理请求校验国际化;3、全局异常GlobalError增加国际化提示;
This commit is contained in:
+13
-10
@@ -18,19 +18,23 @@ public class GlobalErrorCodeConstants {
|
||||
|
||||
// ========== 客户端错误段 ==========
|
||||
|
||||
public static final ErrorCode BAD_REQUEST = new ErrorCode(400, "请求参数不正确");
|
||||
public static final ErrorCode BAD_REQUEST = new ErrorCode(400, "global.error.bad.request");
|
||||
public static final ErrorCode REQUEST_ORGAN_ID_NOT_EXIST = new ErrorCode(400, "global.error.request.organId.notExist");
|
||||
public static final ErrorCode NO_PERMISSION_TO_VISIT_ORG = new ErrorCode(400, "global.error.no.permission.to.visit.org");
|
||||
public static final ErrorCode REQUEST_PARAM_TYPE_ERROR = new ErrorCode(400, "global.error.request.param.type.error");
|
||||
public static final ErrorCode UNAUTHORIZED = new ErrorCode(401, "账号未登录");
|
||||
public static final ErrorCode FORBIDDEN = new ErrorCode(403, "没有该操作权限");
|
||||
public static final ErrorCode NOT_FOUND = new ErrorCode(404, "请求未找到");
|
||||
public static final ErrorCode METHOD_NOT_ALLOWED = new ErrorCode(405, "请求方法不正确");
|
||||
public static final ErrorCode LOCKED = new ErrorCode(423, "请求失败,请稍后重试"); // 并发请求,不允许
|
||||
public static final ErrorCode TOO_MANY_REQUESTS = new ErrorCode(429, "请求过于频繁,请稍后重试");
|
||||
public static final ErrorCode ORG_PRODUCT_EXPIRED = new ErrorCode(410, "组织产品套餐已过期,请续费后继续使用");
|
||||
public static final ErrorCode USER_PRODUCTID_NOT_EXIST = new ErrorCode(411, "账号登录异常,套餐信息不存在,请重新登陆");
|
||||
public static final ErrorCode FORBIDDEN = new ErrorCode(403, "global.error.no.permission");
|
||||
public static final ErrorCode REQUEST_PARAM_MISSING = new ErrorCode(403, "global.error.request.param.missing");
|
||||
public static final ErrorCode NOT_FOUND = new ErrorCode(404, "global.error.request.not.found");
|
||||
public static final ErrorCode METHOD_NOT_ALLOWED = new ErrorCode(405, "global.error.request.method.error");
|
||||
public static final ErrorCode TOO_MANY_REQUESTS = new ErrorCode(429, "global.error.request.too.many");
|
||||
public static final ErrorCode ORG_PRODUCT_EXPIRED = new ErrorCode(410, "global.org.product.expired");
|
||||
public static final ErrorCode USER_PRODUCTID_NOT_EXIST = new ErrorCode(410, "global.org.product.not.exist");
|
||||
|
||||
// ========== 服务端错误段 ==========
|
||||
|
||||
public static final ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "系统繁忙,请稍后再试");
|
||||
public static final ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "global.error.default.error");
|
||||
public static final ErrorCode DUPLICATE_DATA_ERROR = new ErrorCode(500, "global.error.request.duplicate.data.error");
|
||||
public static final ErrorCode NOT_IMPLEMENTED = new ErrorCode(501, "功能未实现/未开启");
|
||||
|
||||
// ES文档更新失败
|
||||
@@ -41,6 +45,5 @@ public class GlobalErrorCodeConstants {
|
||||
public static final ErrorCode DATA_DATA_ERROR = new ErrorCode(506, "文档数据异常,请勿频繁操作,请稍后重试");
|
||||
|
||||
// ========== 自定义错误段 ==========
|
||||
public static final ErrorCode REPEATED_REQUESTS = new ErrorCode(900, "重复请求,请稍后重试"); // 重复请求
|
||||
public static final ErrorCode DEMO_DENY = new ErrorCode(901, "演示模式,禁止写操作");
|
||||
}
|
||||
|
||||
+24
@@ -48,6 +48,12 @@ public class CommonResult<T> implements Serializable {
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private String traceId;
|
||||
|
||||
/**
|
||||
* 占位补充参数
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Object[] args;
|
||||
|
||||
/**
|
||||
* 将传入的 result 对象,转换成另外一个泛型结果的对象
|
||||
*
|
||||
@@ -74,10 +80,28 @@ public class CommonResult<T> implements Serializable {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> error(Integer code, String message, Object... args) {
|
||||
Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
result.code = code;
|
||||
result.msg = message;
|
||||
result.args = args;
|
||||
|
||||
String traceId = MDC.get(TRACE_ID);
|
||||
if (StringUtils.hasText(traceId)) {
|
||||
result.traceId = traceId;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> error(ErrorCode errorCode) {
|
||||
return error(errorCode.getCode(), errorCode.getMsg());
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> error(ErrorCode errorCode, Object... args) {
|
||||
return error(errorCode.getCode(), errorCode.getMsg(), args);
|
||||
}
|
||||
|
||||
public static <T> CommonResult<T> success(T data) {
|
||||
CommonResult<T> result = new CommonResult<>();
|
||||
result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ public class CommonStatisticsReqVO {
|
||||
* 统计维度单位
|
||||
*/
|
||||
@Schema(description = "统计维度单位", example = "0", allowableValues = {"0", "1", "2", "3"}, type = "integer")
|
||||
@NotNull(message = "统计维度单位不能为空")
|
||||
@NotNull(message = "{statistics.unit.notNull}")
|
||||
@StatisticsUnitInEnum
|
||||
private Integer unit;
|
||||
}
|
||||
|
||||
+5
-5
@@ -23,14 +23,14 @@ public class PageParam implements Serializable {
|
||||
public static final Integer PAGE_SIZE_NONE = -1;
|
||||
|
||||
@Schema(description = "页码,从 1 开始", requiredMode = Schema.RequiredMode.REQUIRED,example = "1")
|
||||
@NotNull(message = "页码不能为空")
|
||||
@Min(value = 1, message = "页码最小值为 1")
|
||||
@NotNull(message = "{page.pageNo.notNull}")
|
||||
@Min(value = 1, message = "{page.pageNo.min}")
|
||||
private Integer pageNo = PAGE_NO;
|
||||
|
||||
@Schema(description = "每页条数,最大值为 100", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
|
||||
@NotNull(message = "每页条数不能为空")
|
||||
@Min(value = -1, message = "每页条数最小值为 -1")
|
||||
@Max(value = 100, message = "每页条数最大值为 100")
|
||||
@NotNull(message = "{page.pageSize.notNull}")
|
||||
@Min(value = -1, message = "{page.pageSize.min}")
|
||||
@Max(value = 100, message = "{page.pageSize.max}")
|
||||
private Integer pageSize = PAGE_SIZE;
|
||||
|
||||
}
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.cf.imes.framework.common.util.i18n.core.util;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 国际化工具类
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/11/18 11:18
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class I18nUtils {
|
||||
|
||||
private MessageSource messageSource;
|
||||
|
||||
/**
|
||||
* 获取国际化描述
|
||||
*
|
||||
* @param code
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public String getMessage(String code, Object... args) {
|
||||
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 针对 MessageSource 的基础封装
|
||||
*/
|
||||
package com.cf.imes.framework.common.util.i18n;
|
||||
+1
-1
@@ -29,7 +29,7 @@ import java.lang.annotation.Target;
|
||||
validatedBy = {StatisticsUnitInEnumValidator.class}
|
||||
)
|
||||
public @interface StatisticsUnitInEnum {
|
||||
String message() default "统计维度单位[unit]错误,请检查";
|
||||
String message() default "{statistics.unit.invalid}";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ public @interface InEnum {
|
||||
*/
|
||||
Class<? extends IntArrayValuable> value();
|
||||
|
||||
String message() default "必须在指定范围 {value}";
|
||||
String message() default "inEnum.message";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
|
||||
+14
-2
@@ -5,16 +5,26 @@ import com.cf.imes.framework.common.core.IntArrayValuable;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class InEnumCollectionValidator implements ConstraintValidator<InEnum, Collection<Integer>> {
|
||||
|
||||
private List<Integer> values;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
@Override
|
||||
public void initialize(InEnum annotation) {
|
||||
IntArrayValuable[] values = annotation.value().getEnumConstants();
|
||||
@@ -31,10 +41,12 @@ public class InEnumCollectionValidator implements ConstraintValidator<InEnum, Co
|
||||
if (CollUtil.containsAll(values, list)) {
|
||||
return true;
|
||||
}
|
||||
String defaultConstraintMessageTemplate = context.getDefaultConstraintMessageTemplate();
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String message = messageSource.getMessage(defaultConstraintMessageTemplate, new Object[]{CollUtil.join(list, ",")}, locale);
|
||||
// 校验不通过,自定义提示语句(因为,注解上的 value 是枚举类,无法获得枚举类的实际值)
|
||||
context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
|
||||
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()
|
||||
.replace("\\{value}", CollUtil.join(list, ","))).addConstraintViolation(); // 重新添加错误提示语句
|
||||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); // 重新添加错误提示语句
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+14
-2
@@ -4,15 +4,25 @@ import com.cf.imes.framework.common.core.IntArrayValuable;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class InEnumValidator implements ConstraintValidator<InEnum, Integer> {
|
||||
|
||||
private List<Integer> values;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
@Override
|
||||
public void initialize(InEnum annotation) {
|
||||
IntArrayValuable[] values = annotation.value().getEnumConstants();
|
||||
@@ -33,10 +43,12 @@ public class InEnumValidator implements ConstraintValidator<InEnum, Integer> {
|
||||
if (values.contains(value)) {
|
||||
return true;
|
||||
}
|
||||
String defaultConstraintMessageTemplate = context.getDefaultConstraintMessageTemplate();
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String message = messageSource.getMessage(defaultConstraintMessageTemplate, new Object[]{values.toString()}, locale);
|
||||
// 校验不通过,自定义提示语句(因为,注解上的 value 是枚举类,无法获得枚举类的实际值)
|
||||
context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
|
||||
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()
|
||||
.replace("\\{value}", values.toString())).addConstraintViolation(); // 重新添加错误提示语句
|
||||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); // 重新添加错误提示语句
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ public @interface NumberValid {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String message() default "数字不合法";
|
||||
String message() default "";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
|
||||
+32
-9
@@ -5,19 +5,26 @@ import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @author Gqr
|
||||
* @since 2024/10/17 11:27
|
||||
*/
|
||||
@Component
|
||||
public class NumberValidator implements ConstraintValidator<NumberValid, Number> {
|
||||
private static final String ZERO = "0";
|
||||
|
||||
private static final String GREATER_THAN_ZERO_NOTIFICATION = "%s必须大于0";
|
||||
private static final String GREATER_THAN_ZERO_NOTIFICATION = "number.greaterThanZero";
|
||||
|
||||
private static final String FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION = "%s长度不合法,总长度不超过%d位,小数点后不超过%d位";
|
||||
private static final String FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION = "number.float.length.error";
|
||||
|
||||
private String name;
|
||||
|
||||
@@ -25,13 +32,15 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
|
||||
private int fraction;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
@Override
|
||||
public void initialize(NumberValid constraintAnnotation) {
|
||||
ConstraintValidator.super.initialize(constraintAnnotation);
|
||||
this.name = constraintAnnotation.name();
|
||||
this.integer = constraintAnnotation.integer();
|
||||
this.fraction = constraintAnnotation.fraction();
|
||||
;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,7 +76,10 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
*/
|
||||
private String handleInteger(Integer value) {
|
||||
if (value < Integer.parseInt(ZERO)) {
|
||||
return String.format(GREATER_THAN_ZERO_NOTIFICATION, name);
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{
|
||||
messageSource.getMessage(name, null, locale)
|
||||
}, locale);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -80,7 +92,10 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
*/
|
||||
private String handleLong(Long value) {
|
||||
if (value < Long.parseLong(ZERO)) {
|
||||
return String.format(GREATER_THAN_ZERO_NOTIFICATION, name);
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{
|
||||
messageSource.getMessage(name, null, locale)
|
||||
}, locale);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -92,8 +107,10 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
* @return
|
||||
*/
|
||||
private String handleDouble(Double value) {
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String fieldName = messageSource.getMessage(name, null, locale);
|
||||
if (value < Double.parseDouble(ZERO)) {
|
||||
return String.format(GREATER_THAN_ZERO_NOTIFICATION, name);
|
||||
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale);
|
||||
}
|
||||
// 处理科学计数法,保留小数点后四位
|
||||
DecimalFormat decimalFormat = new DecimalFormat("#.####");
|
||||
@@ -110,7 +127,9 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
|
||||
// 总位数不超过 8 位,小数点后位数不超过 3 位
|
||||
if (integerPartLength > integer || fractionalPartLength > fraction) {
|
||||
return String.format(FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION, name, integer, fraction);
|
||||
return messageSource.getMessage(FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION, new Object[]{
|
||||
fieldName, integer, fraction
|
||||
}, locale);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -122,8 +141,10 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
* @return
|
||||
*/
|
||||
private String handleBigDecimal(BigDecimal value) {
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String fieldName = messageSource.getMessage(name, null, locale);
|
||||
if (value.compareTo(BigDecimal.ZERO) < 0) {
|
||||
return String.format(GREATER_THAN_ZERO_NOTIFICATION, name);
|
||||
return messageSource.getMessage(GREATER_THAN_ZERO_NOTIFICATION, new Object[]{fieldName}, locale);
|
||||
}
|
||||
|
||||
String[] split = value.toPlainString().split("\\.");
|
||||
@@ -137,7 +158,9 @@ public class NumberValidator implements ConstraintValidator<NumberValid, Number>
|
||||
|
||||
// 总位数不超过 8 位,小数点后位数不超过 3 位
|
||||
if (integerPartLength > integer || fractionalPartLength > fraction) {
|
||||
return String.format(FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION, name, integer, fraction);
|
||||
return messageSource.getMessage(FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION, new Object[]{
|
||||
fieldName, integer, fraction
|
||||
}, locale);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user