Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
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
head_group
xyqb-user2
Commits
32e0366f
Commit
32e0366f
authored
Oct 29, 2019
by
技术部-任文超
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🥚
疼碎的IdNo身份证解析service系列魔法值
parent
bcf72378
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
36 deletions
+47
-36
Constants.java
src/main/java/cn/quantgroup/xyqb/Constants.java
+11
-1
IdCardServiceImpl.java
.../quantgroup/xyqb/service/auth/impl/IdCardServiceImpl.java
+36
-35
No files found.
src/main/java/cn/quantgroup/xyqb/Constants.java
View file @
32e0366f
...
...
@@ -36,6 +36,10 @@ public interface Constants {
int
HTTP_CODE_200
=
200
;
int
PAGE_SIZE_MAX
=
200
;
int
PAGE_SIZE_MIN
=
30
;
int
DAY_NO_MAX
=
200
;
int
DAY_NO_MIN
=
30
;
int
MONTH_NO_MAX
=
200
;
int
MONTH_NO_MIN
=
30
;
/** 垃圾,前辈竟然用这个办法来识别UUID */
int
UUID_MIN_LENGTH
=
10
;
String
AUTO_SORT_TYPE
=
"auto"
;
...
...
@@ -133,8 +137,14 @@ public interface Constants {
* 默认随机密码长度
*/
int
RANDOM_PWD_LEN
=
15
;
/** 旧的大陆身份证号长度 */
int
ID_NO_OLD_LENGTH
=
15
;
/** 标准大陆身份证号长度 */
int
IDNO_LENGTH
=
18
;
int
ID_NO_STANDARD_LENGTH
=
18
;
int
ID_NO_CHECK_LENGTH
=
17
;
int
ID_NO_AREA_CODE_LENGTH
=
2
;
int
ID_NO_CHECK_MASK
=
11
;
int
GENDER_MASK
=
2
;
interface
Channel
{
long
BAITIAO
=
222L
;
...
...
src/main/java/cn/quantgroup/xyqb/service/auth/impl/IdCardServiceImpl.java
View file @
32e0366f
package
cn
.
quantgroup
.
xyqb
.
service
.
auth
.
impl
;
import
cn.quantgroup.xyqb.Constants
;
import
cn.quantgroup.xyqb.exception.IdCardException
;
import
cn.quantgroup.xyqb.model.Gender
;
import
cn.quantgroup.xyqb.model.IdCardInfo
;
...
...
@@ -80,10 +81,10 @@ public class IdCardServiceImpl implements IIdCardService {
return
false
;
}
String
actualId
;
if
(
idCardStr
.
length
()
==
18
)
{
actualId
=
idCardStr
.
substring
(
0
,
17
);
}
else
if
(
idCardStr
.
length
()
==
15
)
{
actualId
=
idCardStr
.
substring
(
0
,
6
)
+
"19"
+
idCardStr
.
substring
(
6
,
15
);
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_STANDARD_LENGTH
)
{
actualId
=
idCardStr
.
substring
(
0
,
Constants
.
ID_NO_CHECK_LENGTH
);
}
else
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_OLD_LENGTH
)
{
actualId
=
idCardStr
.
substring
(
0
,
6
)
+
"19"
+
idCardStr
.
substring
(
6
,
Constants
.
ID_NO_OLD_LENGTH
);
}
else
{
return
false
;
}
...
...
@@ -107,23 +108,23 @@ public class IdCardServiceImpl implements IIdCardService {
if
(
gc
.
get
(
Calendar
.
YEAR
)
-
year
>
150
||
gc
.
getTime
().
getTime
()
-
date
.
getTime
()
<
0
)
{
return
false
;
}
if
(
month
<
1
||
month
>
12
)
{
if
(
month
<
Constants
.
MONTH_NO_MIN
||
month
>
Constants
.
MONTH_NO_MAX
)
{
return
false
;
}
if
(
day
<
1
||
day
>
31
)
{
if
(
day
<
Constants
.
DAY_NO_MIN
||
day
>
Constants
.
DAY_NO_MAX
)
{
return
false
;
}
String
areaCode
=
actualId
.
substring
(
0
,
2
);
String
areaCode
=
actualId
.
substring
(
0
,
Constants
.
ID_NO_AREA_CODE_LENGTH
);
if
(!
areaCodes
.
containsKey
(
areaCode
))
{
return
false
;
}
// 校验码
int
acurateCode
=
0
;
for
(
int
i
=
0
;
i
<
17
;
++
i
)
{
for
(
int
i
=
0
;
i
<
Constants
.
ID_NO_CHECK_LENGTH
;
++
i
)
{
acurateCode
+=
((
actualId
.
charAt
(
i
)
-
'0'
)
*
wi
[
i
]);
}
actualId
+=
validCodes
[
acurateCode
%
11
];
return
idCardStr
.
length
()
!=
18
||
actualId
.
equalsIgnoreCase
(
idCardStr
);
actualId
+=
validCodes
[
acurateCode
%
Constants
.
ID_NO_CHECK_MASK
];
return
idCardStr
.
length
()
!=
Constants
.
ID_NO_STANDARD_LENGTH
||
actualId
.
equalsIgnoreCase
(
idCardStr
);
}
@Override
...
...
@@ -135,11 +136,11 @@ public class IdCardServiceImpl implements IIdCardService {
}
String
actualId
;
String
lastChar
;
if
(
idCardStr
.
length
()
==
18
)
{
actualId
=
idCardStr
.
substring
(
0
,
17
);
lastChar
=
idCardStr
.
substring
(
16
,
17
).
toLowerCase
();
}
else
if
(
idCardStr
.
length
()
==
15
)
{
actualId
=
idCardStr
.
substring
(
0
,
6
)
+
"19"
+
idCardStr
.
substring
(
6
,
15
);
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_STANDARD_LENGTH
)
{
actualId
=
idCardStr
.
substring
(
0
,
Constants
.
ID_NO_CHECK_LENGTH
);
lastChar
=
idCardStr
.
substring
(
16
,
Constants
.
ID_NO_CHECK_LENGTH
).
toLowerCase
();
}
else
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_OLD_LENGTH
)
{
actualId
=
idCardStr
.
substring
(
0
,
6
)
+
"19"
+
idCardStr
.
substring
(
6
,
Constants
.
ID_NO_OLD_LENGTH
);
lastChar
=
idCardStr
.
substring
(
13
,
14
).
toLowerCase
();
}
else
{
return
cardInfo
;
...
...
@@ -171,25 +172,25 @@ public class IdCardServiceImpl implements IIdCardService {
if
(
gc
.
get
(
Calendar
.
YEAR
)
-
year
>
150
||
gc
.
getTime
().
getTime
()
-
date
.
getTime
()
<
0
)
{
return
cardInfo
;
}
if
(
month
<
1
||
month
>
12
)
{
if
(
month
<
Constants
.
MONTH_NO_MIN
||
month
>
Constants
.
MONTH_NO_MAX
)
{
return
cardInfo
;
}
if
(
day
<
1
||
day
>
31
)
{
if
(
day
<
Constants
.
DAY_NO_MIN
||
day
>
Constants
.
DAY_NO_MAX
)
{
return
cardInfo
;
}
String
areaCode
=
actualId
.
substring
(
0
,
2
);
String
areaCode
=
actualId
.
substring
(
0
,
Constants
.
ID_NO_AREA_CODE_LENGTH
);
if
(!
areaCodes
.
containsKey
(
areaCode
))
{
return
cardInfo
;
}
// 校验码
int
checkCode
=
0
;
for
(
int
i
=
0
;
i
<
17
;
++
i
)
{
for
(
int
i
=
0
;
i
<
Constants
.
ID_NO_CHECK_LENGTH
;
++
i
)
{
checkCode
+=
((
actualId
.
charAt
(
i
)
-
'0'
)
*
wi
[
i
]);
}
actualId
+=
validCodes
[
checkCode
%
11
];
actualId
+=
validCodes
[
checkCode
%
Constants
.
ID_NO_CHECK_MASK
];
if
(
idCardStr
.
length
()
==
18
)
{
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_STANDARD_LENGTH
)
{
if
(!
actualId
.
equalsIgnoreCase
(
idCardStr
))
{
return
cardInfo
;
}
...
...
@@ -197,7 +198,7 @@ public class IdCardServiceImpl implements IIdCardService {
cardInfo
.
setIsValid
(
true
);
// 判断男女
if
(
Integer
.
parseInt
(
lastChar
)
%
2
==
0
)
{
if
(
Integer
.
parseInt
(
lastChar
)
%
Constants
.
GENDER_MASK
==
0
)
{
cardInfo
.
setGender
(
Gender
.
FEMALE
);
}
else
{
cardInfo
.
setGender
(
Gender
.
MALE
);
...
...
@@ -215,11 +216,11 @@ public class IdCardServiceImpl implements IIdCardService {
}
String
actualId
;
String
lastValue
;
if
(
idCardStr
.
length
()
==
18
)
{
actualId
=
idCardStr
.
substring
(
0
,
17
);
lastValue
=
idCardStr
.
substring
(
16
,
17
).
toLowerCase
();
}
else
if
(
idCardStr
.
length
()
==
15
)
{
actualId
=
idCardStr
.
substring
(
0
,
6
)
+
"19"
+
idCardStr
.
substring
(
6
,
15
);
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_STANDARD_LENGTH
)
{
actualId
=
idCardStr
.
substring
(
0
,
Constants
.
ID_NO_CHECK_LENGTH
);
lastValue
=
idCardStr
.
substring
(
16
,
Constants
.
ID_NO_CHECK_LENGTH
).
toLowerCase
();
}
else
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_OLD_LENGTH
)
{
actualId
=
idCardStr
.
substring
(
0
,
6
)
+
"19"
+
idCardStr
.
substring
(
6
,
Constants
.
ID_NO_OLD_LENGTH
);
lastValue
=
idCardStr
.
substring
(
13
,
14
).
toLowerCase
();
}
else
{
throw
new
IdCardException
(
"身份证号码必须为18位或15位"
);
...
...
@@ -228,7 +229,7 @@ public class IdCardServiceImpl implements IIdCardService {
throw
new
IdCardException
(
"身份证格式不正确"
);
}
String
yearStr
=
actualId
.
substring
(
6
,
10
);
String
monthStr
=
actualId
.
substring
(
10
,
12
);
String
monthStr
=
actualId
.
substring
(
10
,
Constants
.
MONTH_NO_MAX
);
String
dayStr
=
actualId
.
substring
(
12
,
14
);
int
year
=
Integer
.
parseInt
(
yearStr
);
int
month
=
Integer
.
parseInt
(
monthStr
);
...
...
@@ -243,25 +244,25 @@ public class IdCardServiceImpl implements IIdCardService {
if
(
gc
.
get
(
Calendar
.
YEAR
)
-
year
>
150
||
gc
.
getTime
().
getTime
()
-
date
.
getTime
()
<
0
)
{
throw
new
IdCardException
(
"身份证出生年份不正确"
);
}
if
(
month
<
1
||
month
>
12
)
{
if
(
month
<
Constants
.
MONTH_NO_MIN
||
month
>
Constants
.
MONTH_NO_MAX
)
{
throw
new
IdCardException
(
"身份证出生月份不正确"
);
}
if
(
day
<
1
||
day
>
31
)
{
if
(
day
<
Constants
.
DAY_NO_MIN
||
day
>
Constants
.
DAY_NO_MAX
)
{
throw
new
IdCardException
(
"身份证出生日期不正确"
);
}
String
areaCode
=
actualId
.
substring
(
0
,
2
);
String
areaCode
=
actualId
.
substring
(
0
,
Constants
.
ID_NO_AREA_CODE_LENGTH
);
if
(!
areaCodes
.
containsKey
(
areaCode
))
{
throw
new
IdCardException
(
"身份证省份不正确"
);
}
// 校验码
int
checkCode
=
0
;
for
(
int
i
=
0
;
i
<
17
;
++
i
)
{
for
(
int
i
=
0
;
i
<
Constants
.
ID_NO_CHECK_LENGTH
;
++
i
)
{
checkCode
+=
((
actualId
.
charAt
(
i
)
-
'0'
)
*
wi
[
i
]);
}
actualId
+=
validCodes
[
checkCode
%
11
];
actualId
+=
validCodes
[
checkCode
%
Constants
.
ID_NO_CHECK_MASK
];
if
(
idCardStr
.
length
()
==
18
)
{
if
(
idCardStr
.
length
()
==
Constants
.
ID_NO_STANDARD_LENGTH
)
{
if
(!
actualId
.
equalsIgnoreCase
(
idCardStr
))
{
throw
new
IdCardException
(
"身份证校验不正确"
);
}
...
...
@@ -270,7 +271,7 @@ public class IdCardServiceImpl implements IIdCardService {
IdCardInfo
cardInfo
=
new
IdCardInfo
();
cardInfo
.
setIsValid
(
true
);
// 判断男女
if
(
Integer
.
parseInt
(
lastValue
)
%
2
==
0
)
{
if
(
Integer
.
parseInt
(
lastValue
)
%
Constants
.
GENDER_MASK
==
0
)
{
cardInfo
.
setGender
(
Gender
.
FEMALE
);
}
else
{
cardInfo
.
setGender
(
Gender
.
MALE
);
...
...
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