Commit 87c4527c by lujunyi

身份证验证

parent 2874dde9
......@@ -8,7 +8,6 @@
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Jxlwqq\IdValidator\IdValidator;
// 医师
class DoctorController extends AdminController
......@@ -147,9 +146,7 @@ protected function form()
if ($form->mobile && ! preg_match('/^1[3456789]{1}[0-9]{9}$/', $form->mobile)) {
return $form->response()->error('手机号格式错误');
}
$idValidator = new IdValidator();
//if ($form->id_card && ! $idValidator->isValid($form->id_card)) {
if ((! empty($form->id_card)) && strlen($form->id_card) != 18) {
if ($form->id_card && ! validateIDCard($form->id_card)) {
return $form->response()->error('身份证格式错误');
}
});
......
......@@ -106,9 +106,7 @@ protected function form()
if ($form->mobile && ! preg_match('/^1[3456789]{1}[0-9]{9}$/', $form->mobile)) {
return $form->response()->error('手机号格式错误');
}
$idValidator = new IdValidator();
// if ($form->id_card && ! $idValidator->isValid($form->id_card)) {
if (! empty($form->id_card) && strlen($form->id_card) != 18) {
if ($form->id_card && ! validateIDCard($form->id_card)) {
return $form->response()->error('身份证格式错误');
}
});
......
......@@ -15,7 +15,6 @@
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Jxlwqq\IdValidator\IdValidator;
// 药师
class PharmacistController extends AdminController
......@@ -193,10 +192,7 @@ protected function form()
if ($form->mobile && ! preg_match('/^1[3456789]{1}[0-9]{9}$/', $form->mobile)) {
return $form->response()->error('手机号格式错误');
}
$idValidator = new IdValidator();
// if ($form->id_card && ! $idValidator->isValid($form->id_card)) {
if (! empty($form->id_card) && strlen($form->id_card) != 18) {
if ($form->id_card && ! validateIDCard($form->id_card)) {
return $form->response()->error('身份证格式错误');
}
});
......
......@@ -133,7 +133,7 @@ protected function form()
});
// 右上角按钮控制
# $form->disableDeleteButton(); // 去掉删除按钮
// $form->disableDeleteButton(); // 去掉删除按钮
$form->disableViewButton(); // 去掉跳转详情页按钮
});
}
......
......@@ -68,7 +68,7 @@ public function add(Request $request)
$idValidator = new IdValidator();
// if (! $idValidator->isValid($data['id_card'])) {
// if( !in_array(strlen($data['id_card']),[15,18])){
if (isset($data['id_card']) && !validateIDCard($data['id_card'])) { // 身份证非必填项
if (isset($data['id_card']) && ! validateIDCard($data['id_card'])) { // 身份证非必填项
return $this->failed('身份证格式错误');
}
......@@ -116,7 +116,7 @@ public function update(Request $request)
// 验证身份证格式
$idValidator = new IdValidator();
// if (! $idValidator->isValid($id_card)) {
if (isset($id_card) && !validateIDCard($id_card)) {
if (isset($id_card) && ! validateIDCard($id_card)) {
return $this->failed('身份证格式错误');
}
......
......@@ -85,7 +85,7 @@ public function add(Request $request)
$pharmacy = PharmacyModel::where('user_id', $authInfo->id)->first();
$id_card = $request->input('id_card');
if( isset($id_card) && !validateIDCard($id_card) ) {
if (isset($id_card) && ! validateIDCard($id_card)) {
return $this->failed('身份证号格式错误');
}
$pharmacist = new PharmacistModel();
......@@ -156,7 +156,7 @@ public function update(Request $request)
return $this->failed('该药师不存在');
}
$id_card = $request->input('id_card');
if( isset($id_card) && !validateIDCard($id_card) ) {
if (isset($id_card) && ! validateIDCard($id_card)) {
return $this->failed('身份证号格式错误');
}
......@@ -175,7 +175,7 @@ public function update(Request $request)
}
$pharmacist->name = str_replace(' ', '', $request->input('name'));
$pharmacist->id_card = str_replace(' ', '', $id_card );
$pharmacist->id_card = str_replace(' ', '', $id_card);
$pharmacist->mobile = str_replace(' ', '', $request->input('mobile'));
$pharmacist->license_number = str_replace(' ', '', $request->input('license_number'));
$pharmacist->practicing_license = $practicing_license_path; // 执业注册证书链接
......
......@@ -135,6 +135,10 @@ function data_masking($str, string $strType)
$maskStr = substr($str, 0, 4).'************'.substr($str, -4, 3);
break;
case 'idcard': // 只展示前6位和后4位
$idCardCount = strlen($str);
if ($idCardCount != 15 || $idCardCount != 18) {
return $str;
}
$maskStr = substr($str, 0, 6).str_repeat('*', strlen($str) - 10).substr($str, -4);
break;
case 'mobile': // 展示前3位和后4位
......@@ -180,9 +184,10 @@ function validateBirthday($birthday)
if (! function_exists('validateIDCard')) {
function validateIDCard($idCard) {
function validateIDCard($idCard)
{
// 长度检查
if (!preg_match('/^\d{15}$|^\d{17}[\dXx]$/', $idCard)) {
if (! preg_match('/^\d{15}$|^\d{17}[\dXx]$/', $idCard)) {
return false;
}
......@@ -197,7 +202,7 @@ function validateIDCard($idCard) {
$month = substr($birthDate, 4, 2);
$day = substr($birthDate, 6, 2);
if (!checkdate((int)$month, (int)$day, (int)$year)) {
if (! checkdate((int) $month, (int) $day, (int) $year)) {
return false;
}
......@@ -205,21 +210,26 @@ function validateIDCard($idCard) {
return checkIDCardChecksum($idCard);
}
function convertIDCard15to18($idCard15) {
function convertIDCard15to18($idCard15)
{
// 15位身份证转换为18位
$prefix = substr($idCard15, 0, 6) . '19' . substr($idCard15, 6);
$prefix = substr($idCard15, 0, 6).'19'.substr($idCard15, 6);
$checksum = calculateChecksum($prefix);
return $prefix . $checksum;
return $prefix.$checksum;
}
function checkIDCardChecksum($idCard18) {
function checkIDCardChecksum($idCard18)
{
// 检查身份证校验码
$base = substr($idCard18, 0, 17);
$checksum = calculateChecksum($base);
return strtoupper($checksum) === strtoupper($idCard18[17]);
}
function calculateChecksum($base) {
function calculateChecksum($base)
{
// 计算校验码
$weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
$checksumMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
......@@ -231,4 +241,4 @@ function calculateChecksum($base) {
return $checksumMap[$sum % 11];
}
}
\ No newline at end of file
}
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