fix(系统设置): 修复用户导入的错误提示

--bug=1039245 --user=宋天阳 【系统设置】系统用户列表-导入用户错误-查看错误详情-换行显示问题 https://www.tapd.cn/55049933/s/1496764
This commit is contained in:
Jianguo-Genius 2024-04-15 14:17:51 +08:00 committed by 刘瑞斌
parent bc163fbf32
commit 0ba700fcbe
6 changed files with 49 additions and 10 deletions

View File

@ -18,7 +18,7 @@ import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.excel.ExcelValidateHelper;
import io.metersphere.system.dto.excel.FunctionalCaseExcelValidateHelper;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
import io.metersphere.system.excel.domain.ExcelErrData;
import org.apache.commons.collections.CollectionUtils;
@ -171,7 +171,7 @@ public class FunctionalCaseCheckEventListener extends AnalysisEventListener<Map<
StringBuilder errMsg;
try {
//根据excel数据实体中的javax.validation + 正则表达式来校验excel数据
errMsg = new StringBuilder(ExcelValidateHelper.validateEntity(functionalCaseExcelData));
errMsg = new StringBuilder(FunctionalCaseExcelValidateHelper.validateEntity(functionalCaseExcelData));
//自定义校验规则
if (StringUtils.isEmpty(errMsg)) {
//开始校验

View File

@ -19,7 +19,7 @@ import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.excel.ExcelValidateHelper;
import io.metersphere.system.dto.excel.FunctionalCaseExcelValidateHelper;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
@ -220,7 +220,7 @@ public class FunctionalCaseImportEventListener extends AnalysisEventListener<Map
StringBuilder errMsg;
try {
//根据excel数据实体中的javax.validation + 正则表达式来校验excel数据
errMsg = new StringBuilder(ExcelValidateHelper.validateEntity(functionalCaseExcelData));
errMsg = new StringBuilder(FunctionalCaseExcelValidateHelper.validateEntity(functionalCaseExcelData));
//自定义校验规则
if (StringUtils.isEmpty(errMsg)) {
validate(functionalCaseExcelData, errMsg);

View File

@ -12,9 +12,9 @@ import java.lang.reflect.Field;
import java.util.Set;
@Component
public class ExcelValidateHelper {
public class FunctionalCaseExcelValidateHelper {
private static ExcelValidateHelper excelValidateHelper;
private static FunctionalCaseExcelValidateHelper excelValidateHelper;
@Resource
Validator validator;

View File

@ -14,7 +14,7 @@ public class UserExcel {
@ExcelProperty(index = 0)
private String name;
@NotBlank(message = "{cannot_be_null}")
@NotBlank(message = "{user.email.not_blank}")
@Size(min = 1, max = 64, message = "{user.email.length_range}")
@Email(message = "{user.email.invalid}")
@ExcelProperty(index = 1)

View File

@ -0,0 +1,39 @@
package io.metersphere.system.dto.excel;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import jakarta.validation.groups.Default;
import org.springframework.stereotype.Component;
import java.util.Set;
@Component
public class UserExcelValidateHelper {
private static UserExcelValidateHelper excelValidateHelper;
@Resource
Validator validator;
public static <T> String validateEntity(T obj) throws NoSuchFieldException {
StringBuilder result = new StringBuilder();
Set<ConstraintViolation<T>> set = excelValidateHelper.validator.validate(obj, Default.class);
if (set != null && !set.isEmpty()) {
for (ConstraintViolation<T> cv : set) {
result.append(cv.getMessage()).append("; ");
}
}
return result.toString();
}
/**
* 在静态方法中调用
*/
@PostConstruct
public void initialize() {
excelValidateHelper = this;
excelValidateHelper.validator = this.validator;
}
}

View File

@ -2,13 +2,13 @@ package io.metersphere.system.utils;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.metersphere.system.dto.sdk.ExcelParseDTO;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.excel.ExcelValidateHelper;
import io.metersphere.system.dto.excel.UserExcel;
import io.metersphere.system.dto.excel.UserExcelRowDTO;
import io.metersphere.system.dto.excel.UserExcelValidateHelper;
import io.metersphere.system.dto.sdk.ExcelParseDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -25,7 +25,7 @@ public class UserImportEventListener extends AnalysisEventListener<UserExcel> {
Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
try {
//使用javax.validation校验excel数据
errMsg = ExcelValidateHelper.validateEntity(data);
errMsg = UserExcelValidateHelper.validateEntity(data);
if (StringUtils.isEmpty(errMsg)) {
errMsg = businessValidate(data);
}