Commit ce8a3a89 authored by joy's avatar joy Committed by 332784038@qq.com

手机号验证

parent 97064616
...@@ -291,28 +291,25 @@ ...@@ -291,28 +291,25 @@
<dependency> <dependency>
<groupId>com.googlecode.libphonenumber</groupId> <groupId>com.googlecode.libphonenumber</groupId>
<artifactId>geocoder</artifactId> <artifactId>geocoder</artifactId>
<version>2.205</version> <version>2.238</version>
</dependency> </dependency>
<!-- 国际号码库-手机号格式校验 --> <!-- 国际号码库-手机号格式校验 -->
<dependency> <dependency>
<groupId>com.googlecode.libphonenumber</groupId> <groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId> <artifactId>libphonenumber</artifactId>
<version>8.13.11</version> <version>8.13.44</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.googlecode.libphonenumber</groupId> <groupId>com.googlecode.libphonenumber</groupId>
<artifactId>prefixmapper</artifactId> <artifactId>prefixmapper</artifactId>
<version>2.205</version> <version>2.238</version>
</dependency> </dependency>
<!-- 国际号码库-运营商查询 --> <!-- 国际号码库-运营商查询 -->
<dependency> <dependency>
<groupId>com.googlecode.libphonenumber</groupId> <groupId>com.googlecode.libphonenumber</groupId>
<artifactId>carrier</artifactId> <artifactId>carrier</artifactId>
<version>1.195</version> <version>1.228</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
package cn.iocoder.yudao.framework.common.util.validation; package cn.iocoder.yudao.framework.common.util.validation;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.dict.core.dto.DictDataRespDTO;
import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper; import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.PhoneNumberUtil;
...@@ -10,10 +13,13 @@ import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; ...@@ -10,10 +13,13 @@ import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder; import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils; import lombok.extern.log4j.Log4j2;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
@Log4j2
public class PhoneUtil { public class PhoneUtil {
private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
...@@ -139,13 +145,47 @@ public class PhoneUtil { ...@@ -139,13 +145,47 @@ public class PhoneUtil {
if (mc.length != 2) { if (mc.length != 2) {
return false; return false;
} }
String code = mc[0]; // 国家区号 String code = mc[0]; // 国家区号
String mobile = mc[1]; // 手机号 String mobile = mc[1]; // 手机号
if (StrUtil.isBlank(code) || StrUtil.isBlank(mobile)) {
return false;
}
if (!mobile.matches("^[0-9]*$") || !code.matches("^[0-9]*$")) { if (!mobile.matches("^[0-9]*$") || !code.matches("^[0-9]*$")) {
// 纯数字校验 // 纯数字校验
return false; return false;
} }
switch (code) {
// 字典中获取验证规则
String rule = null;
try {
List<DictDataRespDTO> dtos = DictFrameworkUtils.listDictDatasFromCache("phone_number_rule");
if (CollUtil.isNotEmpty(dtos)) {
DictDataRespDTO dto = dtos.stream().filter(d -> d.getValue().trim().equals(code.trim())).findFirst().orElseGet(null);
if (Objects.nonNull(dto)) {
// 优先获取中文
rule = dto.getLabel();
if (StrUtil.isBlank(rule)) {
// 中文不存在,再获取英文
rule = dto.getLabelEn();
}
}
}
}
catch (Exception e) {
log.error(String.format("获取手机号规则失败, code: %s, mobile: %s->", code, mobile), e);
}
if (StrUtil.isNotBlank(rule)) {
log.info(String.format("获取手机号规则成功, code: %s, mobile: %s, rule: %s", code, mobile, rule));
return mobile.matches(rule);
}
// 通用验证
return checkPhoneNumber(mobile, code);
/*switch (code) {
case "86": case "86":
// 中国 // 中国
if (!mobile.matches("1[23456789]\\d{9}") if (!mobile.matches("1[23456789]\\d{9}")
...@@ -174,7 +214,7 @@ public class PhoneUtil { ...@@ -174,7 +214,7 @@ public class PhoneUtil {
} }
return checkPhoneNumber(mobile, code); return checkPhoneNumber(mobile, code);
} }
return true; return true;*/
} }
...@@ -210,6 +250,8 @@ public class PhoneUtil { ...@@ -210,6 +250,8 @@ public class PhoneUtil {
System.out.println(phone2+" Formatted number2: " + formattedNumber2); System.out.println(phone2+" Formatted number2: " + formattedNumber2);
String number2 = PhoneNumberUtil.normalizeDiallableCharsOnly(phone2); String number2 = PhoneNumberUtil.normalizeDiallableCharsOnly(phone2);
System.out.println(phone2+" Formatted number2: " + number2); System.out.println(phone2+" Formatted number2: " + number2);
System.out.println("15219293930".matches("1[23456789]\\d{9}"));
// //
// System.out.println("formatPhone1 234:8035875722>>>>"+PhoneUtil.formatPhone("234:8035875722"));; // System.out.println("formatPhone1 234:8035875722>>>>"+PhoneUtil.formatPhone("234:8035875722"));;
......
...@@ -2,8 +2,6 @@ package cn.iocoder.yudao.framework.common.validation; ...@@ -2,8 +2,6 @@ package cn.iocoder.yudao.framework.common.validation;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.validation.PhoneUtil; import cn.iocoder.yudao.framework.common.util.validation.PhoneUtil;
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext; import javax.validation.ConstraintValidatorContext;
...@@ -16,7 +14,7 @@ public class MobileCodeValidator implements ConstraintValidator<MobileCode, Stri ...@@ -16,7 +14,7 @@ public class MobileCodeValidator implements ConstraintValidator<MobileCode, Stri
@Override @Override
public boolean isValid(String value, ConstraintValidatorContext context) { public boolean isValid(String value, ConstraintValidatorContext context) {
// 如果手机号为空,默认不校验,即校验通过 // 如果手机号为空,默认不校验,即校验通过
if (StrUtil.isEmpty(value)) { if (StrUtil.isBlank(value)) {
return true; return true;
} }
// 校验手机 // 校验手机
......
...@@ -4,6 +4,8 @@ import cn.iocoder.yudao.framework.dict.core.dto.DictDataRespDTO; ...@@ -4,6 +4,8 @@ import cn.iocoder.yudao.framework.dict.core.dto.DictDataRespDTO;
import cn.iocoder.yudao.framework.dict.core.service.DictDataFrameworkService; import cn.iocoder.yudao.framework.dict.core.service.DictDataFrameworkService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.List;
/** /**
* 字典工具类 * 字典工具类
*/ */
...@@ -25,4 +27,12 @@ public class DictFrameworkUtils { ...@@ -25,4 +27,12 @@ public class DictFrameworkUtils {
return service.parseDictDataFromCache(type, label); return service.parseDictDataFromCache(type, label);
} }
/**
* 获得指定类型的字典数据,从缓存中
* @param type 字典类型
* @return 字典数据列表
*/
public static List<DictDataRespDTO> listDictDatasFromCache(String type) {
return service.listDictDatasFromCache(type);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment