更新手机号绑定 新增农科绑定2

This commit is contained in:
mkm 2023-01-04 19:29:30 +08:00
parent ee464cf5f5
commit 86d60887ef

View File

@ -26,10 +26,13 @@ use app\validate\api\UserBaseInfoValidate;
use crmeb\basic\BaseController; use crmeb\basic\BaseController;
use crmeb\services\MiniProgramService; use crmeb\services\MiniProgramService;
use crmeb\services\SmsService; use crmeb\services\SmsService;
use Exception;
use PDOException;
use think\App; use think\App;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\facade\Db; use think\facade\Db;
class User extends BaseController class User extends BaseController
@ -188,20 +191,23 @@ class User extends BaseController
*/ */
public function binding() public function binding()
{ {
$data = $this->request->params(['phone', 'sms_code']); $data = $this->request->params(['phone', 'sms_code','idcard','area_id','street_id','village_id','name']);
$sms_code = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['sms_code'], 'binding'); if(!$data['idcard'] || !$data['area_id'] || !$data['street_id'] || !$data['village_id'] || !$data['name']){
if (!$data['sms_code'] || !$sms_code) return app('json')->fail('缺少参数');
return app('json')->fail('验证码不正确');
$user = $this->repository->accountByUser($data['phone']);
if ($user) {
if (systemConfig('is_phone_login') === '1') {
return app('json')->fail('手机号已被绑定');
} }
$data = ['phone' => $data['phone']]; // $sms_code = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['sms_code'], 'binding');
} else { // if (!$data['sms_code'] || !$sms_code)
$data = ['account' => $data['phone'], 'phone' => $data['phone']]; // return app('json')->fail('验证码不正确');
} // $user = $this->repository->accountByUser($data['phone']);
$this->repository->update($this->request->uid(), $data); // if ($user) {
// if (systemConfig('is_phone_login') === '1') {
// return app('json')->fail('手机号已被绑定');
// }
// $dataa = ['phone' => $data['phone']];
// } else {
// $dataa = ['account' => $data['phone'], 'phone' => $data['phone']];
// }
// $this->repository->update($this->request->uid(), $dataa);
$msg=Db::connect('nongke')->name('szxc_information_usermsg')->where('phone',$data['phone'])->find(); $msg=Db::connect('nongke')->name('szxc_information_usermsg')->where('phone',$data['phone'])->find();
//绑定农科用户 //绑定农科用户
if ($msg){ if ($msg){
@ -218,14 +224,106 @@ class User extends BaseController
Db::name('nk_user')->insert($datas); Db::name('nk_user')->insert($datas);
} }
}else{ }else{
$this->NongKeBinding($data);
}
return app('json')->success('绑定成功');
}
public function NongKeBinding($post){
$nk_user=Db::name('nk_user')->where('user_id',$this->request->uid())->find();
if ($nk_user && $nk_user['n_user_id']!=0){
$user = Db::connect('nongke')->name('user')->where('id',$nk_user['user_id'])->field('id,nickname,group_id,mobile,avatar,username,createtime,score')->find();
}else{
$user=false;
}
// 已绑定
Db::startTrans();
try {
if (!$user){
$time=time();
$data=[
'openid'=>'wx'.$time,
'group_id'=>1,
'username'=>'wx'.$time,
'nickname'=>'微信用户'.$time,
'avatar'=>'https://t7.baidu.com/it/u=2531125946,3055766435&fm=193&f=GIF',
'level'=>1,
'createtime'=>$time,
'updatetime'=>$time,
'status'=>'normal'
];
$post['user_id']= Db::name('user')->insertGetId($data);
}
//判断身份证号是否已被使用
$InformationUsermsg = Db::connect('nongke')->name('szxc_information_usermsg')->where('idcard',$post['idcard'])->field('idcard')->find();
if ($InformationUsermsg) {
return app('json')->fail('身份证号已被使用');
}
$validateIDCard = validateIDCard($post['idcard']);
if($validateIDCard == false){
return app('json')->fail('身份证号错误');
}
$userid = $post['user_id'];
// 组装地址名
if ($post['area_id'] != '') {
$area_name = Db::connect('nongke')->name('geo_area')->where('area_code', $post['area_id'])->value('area_name');
}
if ($post['street_id'] != '') {
$street_name = Db::connect('nongke')->name('geo_street')->where('street_code', $post['street_id'])->value('street_name');
}
if ($post['village_id'] != '') {
$village = Db::connect('nongke')->name('geo_village')->where('village_code', $post['village_id'])->value('village_name');
}
if ($post['brigade_id'] != '') {
$brigade_name = Db::connect('nongke')->name('geo_brigade')->where('id', $post['brigade_id'])->value('brigade_name');
}
$post['address_name'] = $area_name.$street_name.$village.$brigade_name;
//写入用户信息表
$data['user_id'] = $userid;
$data['area_id'] = $post['area_id'];
$data['name'] = $post['name'];
$data['gender'] = $this->get_sex($post['idcard']);
$data['age'] = $this->getAgeFromIdNo($post['idcard']);
$data['marital_status'] = $post['marital_status'];
$data['phone'] = $user['mobile'];
$data['idcard'] = $post['idcard'];
$data['address_name'] = $post['address_name'];
$data['createtime'] = time();
$data['street_id'] = $post['street_id'];
$data['village_id'] = $post['village_id_id'];
$data['brigade_id'] = $post['brigade_id'];
$msg=Db::connect('nongke')->name('szxc_information_usermsg')->where('user_id',$post['user_id'])->find();
if (!$msg){
Db::connect('nongke')->name('szxc_information_usermsg')->insert($data);
}
//写入用户地区表
$address_data['user_id'] = $userid;
$address_data['area_id'] = $post['area_id'];
$address_data['street_id'] = $post['street_id'];
$address_data['village_id'] = $post['village_id_id'];
$address_data['village_code'] = $post['village_id'];
$address_data['brigade_id'] = $post['brigade_id'];
$address_data['createtime'] = date('Y-m-d H:i:s');
$add=Db::connect('nongke')->name('szxc_information_useraddress')->where('user_id',$post['user_id'])->find();
if (!$add){
Db::connect('nongke')->name('szxc_information_useraddress')->insert($address_data);
}
$datas=[ $datas=[
'user_id'=>$this->request->uid(), 'user_id'=>$this->request->uid(),
'n_user_id'=>0, 'n_user_id'=>$userid,
'group_id'=>1 'group_id'=>1
]; ];
Db::name('nk_user')->insert($datas); Db::name('nk_user')->insert($datas);
Db::commit();
} catch (ValidateException | PDOException | Exception $e) {
Db::rollback();
return app('json')->fail($e->getMessage());
} }
return app('json')->success('绑定成功');
} }
/** /**
@ -540,5 +638,44 @@ class User extends BaseController
$user->save($data); $user->save($data);
return app('json')->success('修改成功'); return app('json')->success('修改成功');
} }
/**
* 根据身份证号码获取性别
* author:xiaochuan
* @param string $idcard 身份证号码
* @return int $sex 性别 1 2 0未知
*/
function get_sex($idcard) {
if(empty($idcard)) return null;
$sexint = (int) substr($idcard, 16, 1);
return $sexint % 2 === 0 ? '2' : '1';
} }
// 计算年龄
function getAgeFromIdNo($idno=''){
$btime = strtotime(substr($idno, 6, 8));//idno是身份证号 截取日期并转为时间戳
$byear =date('Y',$btime );
$bmonth =date('m',$btime );
$bday =date('d',$btime );
$curYear=date('Y');
$curMoth = date('m');
$curDay = date('d');
$age = $curYear - $byear;
if( $curMoth < $bmonth || ($curMoth ==$bmonth && $curDay < $bday)){
$age--;
}
return $age ??0;
}
}