Commit fb633471 by lujunyi

备份

parent d1222654
...@@ -153,57 +153,57 @@ public static function noAuth(): Alert ...@@ -153,57 +153,57 @@ public static function noAuth(): Alert
return $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位) // 校验身份证号码长度(15位或18位)
if (strlen($idCard) == 15) { if (strlen($idCard) == 15) {
// 15位身份证号码转换为18位 // 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) { } elseif (strlen($idCard) != 18) {
return ['error' => 'Invalid ID card number']; return ['error' => 'Invalid ID card number'];
} }
// 提取出生日期 // 提取出生日期
$birthDate = substr($idCard, 6, 8); $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); $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']; return ['error' => 'Invalid birth date in ID card number'];
} }
// 提取性别(第17位奇数为男性,偶数为女性) // 提取性别(第17位奇数为男性,偶数为女性)
$gender = (int)substr($idCard, 16, 1) % 2 === 0 ? 2 : 1; $gender = (int) substr($idCard, 16, 1) % 2 === 0 ? 2 : 1;
// 计算年龄 // 计算年龄
$today = new \DateTime(); $today = new \DateTime();
$age = $today->diff($birthDateTime)->y; $age = $today->diff($birthDateTime)->y;
return [ return [
'birthDate' => $birthDate, 'birthDate' => $birthDate,
'gender' => $gender, '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]; $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']; $checkDigits = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
$sum = 0; $sum = 0;
for ($i = 0; $i < 17; $i++) { for ($i = 0; $i < 17; $i++) {
$sum += intval(substr($idCard17, $i, 1)) * $weights[$i]; $sum += intval(substr($idCard17, $i, 1)) * $weights[$i];
} }
$mod = $sum % 11; $mod = $sum % 11;
return $checkDigits[$mod]; return $checkDigits[$mod];
} }
} }
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 sign in to comment