Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jiedao-api-boot-master
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lanbaoming
jiedao-api-boot-master
Commits
ce8a3a89
Commit
ce8a3a89
authored
Sep 04, 2024
by
joy
Committed by
332784038@qq.com
Sep 23, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
手机号验证
parent
97064616
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
13 deletions
+60
-13
pom.xml
yudao-framework/yudao-common/pom.xml
+4
-7
PhoneUtil.java
...der/yudao/framework/common/util/validation/PhoneUtil.java
+45
-3
MobileCodeValidator.java
...udao/framework/common/validation/MobileCodeValidator.java
+1
-3
DictFrameworkUtils.java
...er/yudao/framework/dict/core/util/DictFrameworkUtils.java
+10
-0
No files found.
yudao-framework/yudao-common/pom.xml
View file @
ce8a3a89
...
@@ -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.2
05
</version>
<version>
2.2
38
</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.2
05
</version>
<version>
2.2
38
</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>
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/validation/PhoneUtil.java
View file @
ce8a3a89
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"));;
...
...
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/validation/MobileCodeValidator.java
View file @
ce8a3a89
...
@@ -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
.
is
Empty
(
value
))
{
if
(
StrUtil
.
is
Blank
(
value
))
{
return
true
;
return
true
;
}
}
// 校验手机
// 校验手机
...
...
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java
View file @
ce8a3a89
...
@@ -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
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment