mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
1、新增InDict注解校验字典;2、标签模板、标签入参type支持InDict=labelType检查;
This commit is contained in:
+16
@@ -3,6 +3,7 @@ package com.cf.imes.framework.dict.core.util;
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.cf.imes.framework.common.util.cache.CacheUtils;
|
||||
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||
import com.cf.imes.module.system.api.dict.DictDataApi;
|
||||
import com.cf.imes.module.system.api.dict.dto.DictDataRespDTO;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
@@ -102,4 +103,19 @@ public class DictFrameworkUtils {
|
||||
return PARSE_DICT_DATA_CACHE.get(new Pair<>(dictType, label)).getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定字典值是否存在且已启用。
|
||||
*
|
||||
* @param dictType 字典类型
|
||||
* @param value 字典值
|
||||
* @return 是否为有效字典值
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static boolean isDictDataValid(String dictType, String value) {
|
||||
DictDataRespDTO dictData = GET_DICT_DATA_CACHE.get(new Pair<>(dictType, value));
|
||||
return dictData != DICT_DATA_NULL
|
||||
&& dictData.getValue() != null
|
||||
&& CommonStatusEnum.ENABLE.getStatus().equals(dictData.getStatus());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.cf.imes.framework.dict.core.validation;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 校验字段值是否为指定类型下存在且启用的字典值。
|
||||
*/
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_USE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Constraint(validatedBy = InDictValidator.class)
|
||||
public @interface InDict {
|
||||
|
||||
/**
|
||||
* 字典类型。
|
||||
*/
|
||||
String value();
|
||||
|
||||
String message() default "inDict.message";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.cf.imes.framework.dict.core.validation;
|
||||
|
||||
import com.cf.imes.framework.common.constants.ResourceBundleConstants;
|
||||
import com.cf.imes.framework.common.util.i18n.core.util.ResourceBundleUtils;
|
||||
import com.cf.imes.framework.dict.core.util.DictFrameworkUtils;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 字典值校验器。
|
||||
*/
|
||||
public class InDictValidator implements ConstraintValidator<InDict, Object> {
|
||||
|
||||
private String dictType;
|
||||
|
||||
@Override
|
||||
public void initialize(InDict annotation) {
|
||||
this.dictType = annotation.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(Object value, ConstraintValidatorContext context) {
|
||||
// 是否必填由 @NotNull、@NotBlank 等注解负责。
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
if (DictFrameworkUtils.isDictDataValid(dictType, String.valueOf(value))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String messageKey = context.getDefaultConstraintMessageTemplate();
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String message = ResourceBundleUtils.getMessage(
|
||||
ResourceBundleConstants.MESSAGE_ENUM_PATH, messageKey, messageKey, locale, value);
|
||||
context.disableDefaultConstraintViolation();
|
||||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user