From 65bfd52dcfe110efac2dff9aad17a9f5d1f05b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Fri, 10 Mar 2023 15:25:19 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=20sonar=E3=80=91SysCheckRuleServic?= =?UTF-8?q?eImpl=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/SysCheckRuleServiceImpl.java | 96 ++++++++++--------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysCheckRuleServiceImpl.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysCheckRuleServiceImpl.java index c1206215..29ab994d 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysCheckRuleServiceImpl.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysCheckRuleServiceImpl.java @@ -9,6 +9,7 @@ import org.apache.commons.lang.StringUtils; import com.jero.modules.system.entity.SysCheckRule; import com.jero.modules.system.mapper.SysCheckRuleMapper; import com.jero.modules.system.service.ISysCheckRuleService; +import org.jetbrains.annotations.Nullable; import org.springframework.stereotype.Service; import java.util.regex.Pattern; @@ -46,53 +47,60 @@ public class SysCheckRuleServiceImpl extends ServiceImpl value.length() ? value.length() : endIndex; - // 如果开始下标大于结束下标,则说明用户还尚未输入到该位置,直接赋空值 - if (beginIndex > endIndex) { - checkValue = ""; - } else { - checkValue = value.substring(beginIndex, endIndex); - } - result.put("beginIndex", beginIndex); - result.put("endIndex", endIndex); - beginIndex += num; - } - result.put("checkValue", checkValue); - boolean passed = Pattern.matches(pattern, checkValue); - result.put("passed", passed); - // 如果没有通过校验就返回错误信息 - if (!passed) { - return result; - } - } + JSONObject result = getJsonObject(value, ruleJson); + if (result != null) return result; } } return new JSONObject(); } + @Nullable + private JSONObject getJsonObject(String value, String ruleJson) { + // 开始截取的下标,根据规则的顺序递增,但是 * 号不计入递增范围 + int beginIndex = 0; + JSONArray rules = JSON.parseArray(ruleJson); + for (int i = 0; i < rules.size(); i++) { + JSONObject result = new JSONObject(); + JSONObject rule = rules.getJSONObject(i); + // 位数 + String digits = rule.getString("digits"); + result.put("digits", digits); + // 验证规则 + String pattern = rule.getString("pattern"); + result.put("pattern", pattern); + // 未通过时的提示文本 + String message = rule.getString("message"); + result.put("message", message); + + // 根据用户设定的区间,截取字符串进行验证 + String checkValue; + // 是否检查整个值而不截取 + if (CHECK_ALL_SYMBOL.equals(digits)) { + checkValue = value; + } else { + int num = Integer.parseInt(digits); + int endIndex = beginIndex + num; + // 如果结束下标大于给定的值的长度,则取到最后一位 + endIndex = endIndex > value.length() ? value.length() : endIndex; + // 如果开始下标大于结束下标,则说明用户还尚未输入到该位置,直接赋空值 + if (beginIndex > endIndex) { + checkValue = ""; + } else { + checkValue = value.substring(beginIndex, endIndex); + } + result.put("beginIndex", beginIndex); + result.put("endIndex", endIndex); + beginIndex += num; + } + result.put("checkValue", checkValue); + boolean passed = Pattern.matches(pattern, checkValue); + result.put("passed", passed); + // 如果没有通过校验就返回错误信息 + if (!passed) { + return result; + } + } + return null; + } + }