From e5a08afb8c2718aaff10b15ab5075b8c32660d96 Mon Sep 17 00:00:00 2001 From: wangxianzhang Date: Mon, 22 Aug 2022 10:18:31 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=AF=81=E4=BB=B6=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E3=80=91=E6=8A=A4=E7=85=A7-=E5=AE=8C=E6=88=90=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E5=B9=B6=E4=B8=94?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B1=85=E6=B0=91=E5=AF=BC=E5=85=A5=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=B8=BA=E6=AD=A3=E5=88=99=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/utils/IdCardRegexUtils.java | 123 ++++++++++++++++++ .../impl/IcResiUserImportServiceImpl.java | 101 +++++++------- 2 files changed, 173 insertions(+), 51 deletions(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java new file mode 100644 index 0000000000..deaf9997c1 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java @@ -0,0 +1,123 @@ +package com.epmet.commons.tools.utils; + +import com.epmet.commons.tools.enums.IdCardTypeEnum; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 唯一整件正则工具 + */ +public class IdCardRegexUtils { + + /** + * 15位身份证号的正则表达式 + */ + private static final Pattern PATTERN_15_ID = Pattern.compile("^\\d{6}(?\\d{2})(?0[1-9]|1[0-2])(?[0-2][0-9]|3[0-1])\\d{2}(?\\d)$"); + /** + * 18位身份证号的正则表达式 + */ + private static final Pattern PATTERN_18_ID = Pattern.compile("^\\d{6}(?\\d{4})(?0[1-9]|1[0-2])(?[0-2][0-9]|3[0-1])\\d{2}(?\\d)[0-9a-xA-X]$"); + + /** + * 9位护照 + */ + private static final Pattern PATTERN_9_PASSPORT = Pattern.compile("^\\w{2}\\d{7}$|^\\w{1}\\d{8}$"); + + private String inputText; + + private Matcher matcher; + + private IdCardTypeEnum idCardType; + + private IdCardRegexUtils(IdCardTypeEnum idCardType, Matcher matcher, String inputText) { + this.idCardType = idCardType; + this.matcher = matcher; + this.inputText = inputText; + } + + /** + * 正则解析结果 + */ + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class ParsedContent { + private String birthdayYear; + private String birthdayMonth; + private String birthdayDay; + private String sex; + } + + /** + * 解析正则 + * @param input + * @return + */ + public static IdCardRegexUtils parse(String input) { + if (input == null) { + return null; + } + + if (input.length() == 15) { + Matcher matcher = PATTERN_15_ID.matcher(input); + if (matcher.matches()) { + return new IdCardRegexUtils(IdCardTypeEnum.SFZH, matcher, input); + } + } + + if (input.length() == 18) { + Matcher matcher = PATTERN_18_ID.matcher(input); + if (matcher.matches()) { + return new IdCardRegexUtils(IdCardTypeEnum.SFZH, matcher, input); + } + } + + if (input.length() == 9) { + Matcher matcher = PATTERN_9_PASSPORT.matcher(input); + if (matcher.matches()) { + return new IdCardRegexUtils(IdCardTypeEnum.PASSPORT, matcher, input); + } + } + return null; + } + + /** + * 获取解析结果 + * @return + */ + public ParsedContent getParsedResult() { + if (matcher == null || idCardType == null) { + return null; + } + + if (IdCardTypeEnum.SFZH == idCardType) { + //是身份证号,可以解析 + String year; + if (inputText.length() == 15) { + // 15位身份证号,years前需要拼上19 + year = "19".concat(matcher.group("year")); + } else { + year = matcher.group("year"); + } + String month = matcher.group("month"); + String day = matcher.group("day"); + String sex = matcher.group("sex"); + return new ParsedContent(year, month, day, sex); + } + + // 其他类型暂时不可解析 + return null; + } + + /** + * 获取类型枚举 + * @return + */ + public IdCardTypeEnum getTypeEnum() { + return idCardType; + } +} diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserImportServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserImportServiceImpl.java index adc9a94eb4..342fb23760 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserImportServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserImportServiceImpl.java @@ -90,20 +90,6 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res public static final List controlGroup1 = Arrays.asList("input", "textarea", "datepicker", "daterange"); public static final List controlGroup2 = Arrays.asList("select", "radio", "cascader"); - /** - * 15位身份证号的正则表达式 - */ - private final Pattern PATTERN_15_ID = Pattern.compile("^\\d{6}(?\\d{2})(?0[1-9]|1[0-2])(?[0-2][0-9]|3[0-1])\\d{2}(?\\d)$"); - /** - * 18位身份证号的正则表达式 - */ - private final Pattern PATTERN_18_ID = Pattern.compile("^\\d{6}(?\\d{4})(?0[1-9]|1[0-2])(?[0-2][0-9]|3[0-1])\\d{2}(?\\d)[0-9a-xA-X]$"); - - /** - * 9位护照 - */ - private final Pattern PATTERN_9_PASSPORT = Pattern.compile("^\\w{2}\\d{7}|\\w{1}\\d{8}$"); - /** * 日期解析,不含时间 */ @@ -646,48 +632,61 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res // ================== 数据补充 =================== String year = null, month = null, day = null, sex = null; - // 证件类型,默认是1身份证号 - String idCardType = IdCardTypeEnum.SFZH.getType(); - - if (idCard.length() == 15) { - // 身份证 - Matcher matcher = PATTERN_15_ID.matcher(idCard); - if (matcher.matches()) { - year = "19".concat(matcher.group("year")); - month = matcher.group("month"); - day = matcher.group("day"); - sex = matcher.group("sex"); - } else { - String s = "证件号解析错误"; - throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), s, s); - } - } else if (idCard.length() == 18) { - // 身份证 - Matcher matcher = PATTERN_18_ID.matcher(idCard); - if (matcher.matches()) { - year = matcher.group("year"); - month = matcher.group("month"); - day = matcher.group("day"); - sex = matcher.group("sex"); - } else { - String s = "证件号解析错误"; - throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), s, s); - } - } else if (idCard.length() == 9) { - // 护照 - Matcher matcher = PATTERN_9_PASSPORT.matcher(idCard); - if (matcher.matches()) { - idCardType = IdCardTypeEnum.PASSPORT.getType(); - } - } else { - String s = "证件号解析错误"; + //if (idCard.length() == 15) { + // // 身份证 + // Matcher matcher = PATTERN_15_ID.matcher(idCard); + // if (matcher.matches()) { + // year = "19".concat(matcher.group("year")); + // month = matcher.group("month"); + // day = matcher.group("day"); + // sex = matcher.group("sex"); + // } else { + // String s = "证件号解析错误"; + // throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), s, s); + // } + //} else if (idCard.length() == 18) { + // // 身份证 + // Matcher matcher = PATTERN_18_ID.matcher(idCard); + // if (matcher.matches()) { + // year = matcher.group("year"); + // month = matcher.group("month"); + // day = matcher.group("day"); + // sex = matcher.group("sex"); + // } else { + // String s = "证件号解析错误"; + // throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), s, s); + // } + //} else if (idCard.length() == 9) { + // // 护照 + // Matcher matcher = PATTERN_9_PASSPORT.matcher(idCard); + // if (matcher.matches()) { + // idCardType = IdCardTypeEnum.PASSPORT.getType(); + // } + //} else { + // String s = "证件号解析错误"; + // throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), s, s); + //} + + IdCardRegexUtils regexUtilInstance = IdCardRegexUtils.parse(idCard); + IdCardTypeEnum idCardType = regexUtilInstance.getTypeEnum(); + + if (idCardType == null || IdCardTypeEnum.OTHERS == idCardType) { + String s = "证件号解析错误,或不支持的证件类型。(请使用身份证号或者护照号)"; throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), s, s); } + IdCardRegexUtils.ParsedContent parsedResult = regexUtilInstance.getParsedResult(); + if (parsedResult != null) { + year = parsedResult.getBirthdayYear(); + month = parsedResult.getBirthdayMonth(); + day = parsedResult.getBirthdayDay(); + sex = parsedResult.getSex(); + } + // 存储证件类型 - columnAndValues.put("ID_CARD_TYPE", idCardType); + columnAndValues.put("ID_CARD_TYPE", idCardType.getType()); - if (idCardType.equals(IdCardTypeEnum.SFZH.getType())) { + if (idCardType == IdCardTypeEnum.SFZH) { //只有证件类型是身份证号才做相关解析 // 出生日期 & 年龄 LocalDate birthday = null;