mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 优化 字典项校验注解增加分隔符属性
This commit is contained in:
		@@ -84,13 +84,4 @@ public interface DictService {
 | 
			
		||||
     */
 | 
			
		||||
    List<DictDataDTO> getDictData(String dictType);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 校验指定的字典类型下是否存在指定的字典值
 | 
			
		||||
     *
 | 
			
		||||
     * @param dictType  字典类型
 | 
			
		||||
     * @param dictValue 字典值
 | 
			
		||||
     * @return true 表示该字典值在指定字典类型中有效;false 表示无效
 | 
			
		||||
     */
 | 
			
		||||
    Boolean isValidDictValue(String dictType, String dictValue);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,6 +23,11 @@ public @interface DictPattern {
 | 
			
		||||
     */
 | 
			
		||||
    String dictType();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 分隔符
 | 
			
		||||
     */
 | 
			
		||||
    String separator();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 默认校验失败提示信息
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import jakarta.validation.ConstraintValidator;
 | 
			
		||||
import jakarta.validation.ConstraintValidatorContext;
 | 
			
		||||
import org.dromara.common.core.service.DictService;
 | 
			
		||||
import org.dromara.common.core.utils.SpringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.StringUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 自定义字典值校验器
 | 
			
		||||
@@ -17,6 +18,11 @@ public class DictPatternValidator implements ConstraintValidator<DictPattern, St
 | 
			
		||||
     */
 | 
			
		||||
    private String dictType;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 分隔符
 | 
			
		||||
     */
 | 
			
		||||
    private String separator = ",";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 初始化校验器,提取注解上的字典类型
 | 
			
		||||
     *
 | 
			
		||||
@@ -25,6 +31,9 @@ public class DictPatternValidator implements ConstraintValidator<DictPattern, St
 | 
			
		||||
    @Override
 | 
			
		||||
    public void initialize(DictPattern annotation) {
 | 
			
		||||
        this.dictType = annotation.dictType();
 | 
			
		||||
        if (StringUtils.isNotBlank(annotation.separator())) {
 | 
			
		||||
            this.separator = annotation.separator();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -36,7 +45,11 @@ public class DictPatternValidator implements ConstraintValidator<DictPattern, St
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isValid(String value, ConstraintValidatorContext context) {
 | 
			
		||||
        return SpringUtils.getBean(DictService.class).isValidDictValue(dictType, value);
 | 
			
		||||
        if (StringUtils.isBlank(dictType) || StringUtils.isBlank(value)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        String dictLabel = SpringUtils.getBean(DictService.class).getDictLabel(dictType, value, separator);
 | 
			
		||||
        return StringUtils.isNotBlank(dictLabel);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -294,24 +294,4 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService, DictService
 | 
			
		||||
        return BeanUtil.copyToList(list, DictDataDTO.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 校验指定的字典类型下是否存在指定的字典值
 | 
			
		||||
     *
 | 
			
		||||
     * @param dictType  字典类型
 | 
			
		||||
     * @param dictValue 字典值
 | 
			
		||||
     * @return true 表示该字典值在指定字典类型中有效;false 表示无效
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Boolean isValidDictValue(String dictType, String dictValue) {
 | 
			
		||||
        if (StringUtils.isBlank(dictType) || StringUtils.isBlank(dictValue)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        List<SysDictDataVo> datas = SpringUtils.getAopProxy(this).selectDictDataByType(dictType);
 | 
			
		||||
        if (CollUtil.isEmpty(datas)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        Map<String, String> map = StreamUtils.toMap(datas, SysDictDataVo::getDictValue, SysDictDataVo::getDictLabel);
 | 
			
		||||
        return map.containsKey(dictValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user