From b37d28f13220e46a32f470d1c5c486fcc1bf620f Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Thu, 5 Jan 2023 11:54:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=20=E6=96=B0=E5=A2=9E=E5=86=9C=E7=A7=91?= =?UTF-8?q?=E7=BB=91=E5=AE=9A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 51 +++++++++++++++++++++++++++++++- app/controller/api/user/User.php | 19 ++++++++---- config/database.php | 22 +++++++++++++- 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/app/common.php b/app/common.php index 798910b5..bdf5265a 100644 --- a/app/common.php +++ b/app/common.php @@ -1124,6 +1124,55 @@ if (!function_exists('aj_get_serevice')) { return $service; } } - +if (!function_exists('validateIDCard')) { + + function validateIDCard(string $idcard) + { + if(empty($idcard)){ + return false; + }else{ + $idcard = strtoupper($idcard); # 如果是小写x,转化为大写X + if(strlen($idcard) != 18 && strlen($idcard) != 15){ + return false; + } + # 如果是15位身份证,则转化为18位 + if(strlen($idcard) == 15){ + # 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 + if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) { + $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9); + } else { + $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9); + } + # 加权因子 + $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); + # 校验码对应值 + $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); + $checksum = 0; + for ($i = 0; $i < strlen($idcard); $i++) { + $checksum += substr($idcard, $i, 1) * $factor[$i]; + } + $idcard = $idcard . $code[$checksum % 11]; + } + # 验证身份证开始 + $IDCardBody = substr($idcard, 0, 17); # 身份证主体 + $IDCardCode = strtoupper(substr($idcard, 17, 1)); # 身份证最后一位的验证码 + + # 加权因子 + $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); + # 校验码对应值 + $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); + $checksum = 0; + for ($i = 0; $i < strlen($IDCardBody); $i++) { + $checksum += substr($IDCardBody, $i, 1) * $factor[$i]; + } + $validateIdcard = $code[$checksum % 11]; # 判断身份证是否合理 + if($validateIdcard != $IDCardCode){ + return false; + }else{ + return true; + } + } + } +} diff --git a/app/controller/api/user/User.php b/app/controller/api/user/User.php index a267dd98..4e17f72c 100644 --- a/app/controller/api/user/User.php +++ b/app/controller/api/user/User.php @@ -216,12 +216,19 @@ class User extends BaseController if ($find && $find['user_id']==0){ Db::name('nk_user')->where('id',$find['id'])->update(['user_id'=>$this->request->uid(),'group_id'=>$users['group_id']]); }else{ - $datas=[ - 'user_id'=>$this->request->uid(), - 'n_user_id'=>$users['id'], - 'group_id'=>$users['group_id'] - ]; - Db::name('nk_user')->insert($datas); + $nk_user=Db::name('nk_user')->where('user_id',$this->request->uid())->find(); + if (!$nk_user){ + $datas=[ + 'user_id'=>$this->request->uid(), + 'n_user_id'=>$users['id'], + 'group_id'=>$users['group_id'] + ]; + Db::name('nk_user')->insert($datas); + }else{ + Db::name('nk_user')->where('id',$nk_user['id'])->update(['n_user_id'=>$users['id'],'group_id'=>$users['group_id']]); + + } + } }else{ $nk_user=Db::name('nk_user')->where('user_id',$this->request->uid())->find(); diff --git a/config/database.php b/config/database.php index 39f1fce9..85e3be31 100644 --- a/config/database.php +++ b/config/database.php @@ -9,6 +9,7 @@ // | Author: CRMEB Team // +---------------------------------------------------------------------- +use think\facade\Env; return [ // 默认使用的数据库连接配置 @@ -66,7 +67,26 @@ return [ // 字段缓存路径 'schema_cache_path' => app()->getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR, ], - + 'nongke' => [ + // 数据库类型 + 'type' => Env::get('databasenk.type', 'mysql'), + // 服务器地址 + 'hostname' => Env::get('databasenk.hostname', '127.0.0.1'), + // 数据库名 + 'database' => Env::get('databasenk.database', ''), + // 用户名 + 'username' => Env::get('databasenk.username', 'root'), + // 密码 + 'password' => Env::get('databasenk.password', ''), + // 端口 + 'hostport' => Env::get('databasenk.hostport', '3306'), + // 数据库连接参数 + 'params' => [], + // 数据库编码默认采用utf8 + 'charset' => Env::get('databasenk.charset', 'utf8'), + // 数据库表前缀 + 'prefix' => Env::get('databasenk.prefix', ''), + ], // 更多的数据库配置信息 ], ];