Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵增煜
/
tzt-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
fb633471
authored
Nov 12, 2024
by
lujunyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
备份
parent
d1222654
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
19 deletions
+19
-19
app/Common/Util.php
+19
-19
No files found.
app/Common/Util.php
View file @
fb633471
...
...
@@ -153,57 +153,57 @@ public static function noAuth(): Alert
return
$alert
;
}
/**
* 验证身份证号码
*/
public
static
function
checkIdCard
(
$idcard
){
}
public
static
function
checkIdCard
(
$idcard
)
{}
# 通过身份证号码获取性别、年龄等信息
public
static
function
getIdCardInfo
(
$idCard
)
{
// 通过身份证号码获取性别、年龄等信息
public
static
function
getIdCardInfo
(
$idCard
)
{
// 校验身份证号码长度(15位或18位)
if
(
strlen
(
$idCard
)
==
15
)
{
// 15位身份证号码转换为18位
$idCard
=
substr
(
$idCard
,
0
,
6
)
.
'19'
.
substr
(
$idCard
,
6
,
9
)
.
self
::
calcCheckDigit
(
$idCard
);
$idCard
=
substr
(
$idCard
,
0
,
6
)
.
'19'
.
substr
(
$idCard
,
6
,
9
)
.
self
::
calcCheckDigit
(
$idCard
);
}
elseif
(
strlen
(
$idCard
)
!=
18
)
{
return
[
'error'
=>
'Invalid ID card number'
];
}
// 提取出生日期
$birthDate
=
substr
(
$idCard
,
6
,
8
);
$birthDate
=
substr
(
$birthDate
,
0
,
4
)
.
'-'
.
substr
(
$birthDate
,
4
,
2
)
.
'-'
.
substr
(
$birthDate
,
6
,
2
);
$birthDate
=
substr
(
$birthDate
,
0
,
4
)
.
'-'
.
substr
(
$birthDate
,
4
,
2
)
.
'-'
.
substr
(
$birthDate
,
6
,
2
);
$birthDateTime
=
\DateTime
::
createFromFormat
(
'Y-m-d'
,
$birthDate
);
if
(
!
$birthDateTime
||
$birthDateTime
->
format
(
'Y-m-d'
)
!==
$birthDate
)
{
if
(
!
$birthDateTime
||
$birthDateTime
->
format
(
'Y-m-d'
)
!==
$birthDate
)
{
return
[
'error'
=>
'Invalid birth date in ID card number'
];
}
// 提取性别(第17位奇数为男性,偶数为女性)
$gender
=
(
int
)
substr
(
$idCard
,
16
,
1
)
%
2
===
0
?
2
:
1
;
$gender
=
(
int
)
substr
(
$idCard
,
16
,
1
)
%
2
===
0
?
2
:
1
;
// 计算年龄
$today
=
new
\DateTime
();
$age
=
$today
->
diff
(
$birthDateTime
)
->
y
;
return
[
'birthDate'
=>
$birthDate
,
'gender'
=>
$gender
,
'age'
=>
$age
'age'
=>
$age
,
];
}
public
static
function
calcCheckDigit
(
$idCard17
)
{
public
static
function
calcCheckDigit
(
$idCard17
)
{
$weights
=
[
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
];
$checkDigits
=
[
'1'
,
'0'
,
'X'
,
'9'
,
'8'
,
'7'
,
'6'
,
'5'
,
'4'
,
'3'
,
'2'
];
$sum
=
0
;
for
(
$i
=
0
;
$i
<
17
;
$i
++
)
{
$sum
+=
intval
(
substr
(
$idCard17
,
$i
,
1
))
*
$weights
[
$i
];
}
$mod
=
$sum
%
11
;
return
$checkDigits
[
$mod
];
}
}
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