352 lines
14 KiB
PHP
352 lines
14 KiB
PHP
![]() |
<?php
|
|||
|
|
|||
|
namespace app\api\controller;
|
|||
|
|
|||
|
use app\api\BaseController;
|
|||
|
use app\api\middleware\Auth;
|
|||
|
use think\Exception;
|
|||
|
use think\facade\Db;
|
|||
|
use fast\Random;
|
|||
|
use think\exception\ValidateException;
|
|||
|
use think\facade\Request;
|
|||
|
|
|||
|
/**
|
|||
|
* 用户信息相关接口.
|
|||
|
*/
|
|||
|
class Userinfo extends BaseController
|
|||
|
{
|
|||
|
/**
|
|||
|
* 控制器中间件 [不需要鉴权]
|
|||
|
* @var array
|
|||
|
*/
|
|||
|
protected $middleware = [
|
|||
|
Auth::class => ['except' => ['isBinding'] ]
|
|||
|
];
|
|||
|
/**
|
|||
|
* 微信是否绑定用户
|
|||
|
*
|
|||
|
* @ApiTitle (微信是否绑定用户)
|
|||
|
* @ApiSummary (微信是否绑定用户)
|
|||
|
* @ApiMethod (POST)
|
|||
|
* @ApiRoute (/api/Userinfo/isBinding)
|
|||
|
* @ApiParams (name="openid", type="integer", required=fasle, description="微信openID")
|
|||
|
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
|||
|
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
|||
|
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
|
|||
|
* @ApiReturn ({
|
|||
|
* 'code':'1',
|
|||
|
* 'msg':'返回成功'
|
|||
|
* })
|
|||
|
*/
|
|||
|
public function isBinding()
|
|||
|
{
|
|||
|
$post = get_params();
|
|||
|
// if(!$post['openid']){
|
|||
|
// $this->apiError('缺少参数');
|
|||
|
// }
|
|||
|
// $where['openid'] = $post['openid'];
|
|||
|
// $user = Db::table('fa_user')->where($where)->find();
|
|||
|
// if ($user) {
|
|||
|
//// 调登录接口返回信息
|
|||
|
// $ret = $this->auth->login($user['mobile'], '123456');
|
|||
|
// if ($ret) {
|
|||
|
// $Userinfo = $this->auth->getUserinfo();
|
|||
|
// $map['user_id'] = $user['id'];
|
|||
|
// $is_wgy = Db::table('fa_szxc_information_usermsg')->where($map)->value('is_wgy');
|
|||
|
// $Userinfo['is_wgy'] = $is_wgy;
|
|||
|
// $userinfo_data['userinfo'] = $Userinfo;
|
|||
|
// $this->apiSuccess('已完善,登录成功', $userinfo_data,1);
|
|||
|
// } else {
|
|||
|
// $this->apiError($this->auth->getError());
|
|||
|
// }
|
|||
|
// }else{
|
|||
|
// $this->apiError('未完善信息');
|
|||
|
// }
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 授权登录后完善用户信息
|
|||
|
*
|
|||
|
* @ApiTitle (授权登录后完善用户信息)
|
|||
|
* @ApiSummary (授权登录后完善用户信息)
|
|||
|
* @ApiMethod (POST)
|
|||
|
* @ApiRoute (/api/Userinfo/Binding)
|
|||
|
* @ApiParams (name="openID", type="integer", required=fasle, description="微信openID")
|
|||
|
* @ApiParams (name="phone", type="integer", required=fasle, description="电话")
|
|||
|
* @ApiParams (name="nickname", type="integer", required=fasle, description="微信昵称")
|
|||
|
* @ApiParams (name="avatar", type="integer", required=fasle, description="微信头像")
|
|||
|
* @ApiParams (name="name", type="integer", required=fasle, description="姓名")
|
|||
|
* @ApiParams (name="marital_status", type="integer", required=fasle, description="婚姻状态")
|
|||
|
* @ApiParams (name="idcard", type="integer", required=fasle, description="身份证号")
|
|||
|
* @ApiParams (name="address_name", type="string", required=true, description="地区中文")
|
|||
|
* @ApiParams (name="area_id", type="string", required=true, description="区id")
|
|||
|
* @ApiParams (name="street_id", type="string", required=true, description="镇id")
|
|||
|
* @ApiParams (name="village_id", type="string", required=true, description="村id")
|
|||
|
* @ApiParams (name="brigade_id", type="string", required=true, description="大队id")
|
|||
|
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
|||
|
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
|||
|
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
|
|||
|
* @ApiReturn ({
|
|||
|
* 'code':'1',
|
|||
|
* 'msg':'返回成功'
|
|||
|
* })
|
|||
|
*/
|
|||
|
public function Binding()
|
|||
|
{
|
|||
|
|
|||
|
$post = get_params();
|
|||
|
|
|||
|
if(!$post['idcard'] || !$post['area_id'] || !$post['street_id'] || !$post['village_id'] || !$post['name']){
|
|||
|
$this->apiError('缺少参数');
|
|||
|
}
|
|||
|
$where['id'] = JWT_UID;
|
|||
|
$post['user_id'] = JWT_UID;
|
|||
|
$user = Db::table('fa_user')->where($where)->field('id,nickname,group_id,mobile,avatar,username,createtime,score')->find();
|
|||
|
// 已绑定
|
|||
|
if ($user) {
|
|||
|
Db::startTrans();
|
|||
|
try {
|
|||
|
// 判断手机号是否已经注册
|
|||
|
// $is_mobile = Db::table('fa_user')->getByMobile($post['phone']);
|
|||
|
// if ($is_mobile) {
|
|||
|
// $this->apiError('手机号已被使用');
|
|||
|
// }
|
|||
|
//判断身份证号是否已被使用
|
|||
|
$InformationUsermsg = Db::table('fa_szxc_information_usermsg')->where('idcard',$post['idcard'])->field('idcard')->find();
|
|||
|
if ($InformationUsermsg) {
|
|||
|
$this->apiError('身份证号已被使用');
|
|||
|
}
|
|||
|
|
|||
|
// $validateIDCard = $this->validateIDCard($post['idcard']);
|
|||
|
// if($validateIDCard == false){
|
|||
|
// $this->apiError('身份证号错误');
|
|||
|
// }
|
|||
|
// $ip = request()->ip();
|
|||
|
// $time =
|
|||
|
// $salt = Random::alnum(); //随机字符串
|
|||
|
|
|||
|
$userid = $post['user_id'];
|
|||
|
|
|||
|
// 组装地址名
|
|||
|
if ($post['area_id'] != '') {
|
|||
|
$area_name = Db::table('fa_geo_area')->where('area_code', $post['area_id'])->value('area_name');
|
|||
|
}
|
|||
|
if ($post['street_id'] != '') {
|
|||
|
$street_name = Db::table('fa_geo_street')->where('street_code', $post['street_id'])->value('street_name');
|
|||
|
}
|
|||
|
if ($post['village_id'] != '') {
|
|||
|
$village = Db::table('fa_geo_village')->where('village_code', $post['village_id'])->value('village_name');
|
|||
|
}
|
|||
|
if ($post['brigade_id'] != '') {
|
|||
|
$brigade_name = Db::table('fa_geo_brigade')->where('id', $post['brigade_id'])->value('brigade_name');
|
|||
|
}
|
|||
|
$post['address_name'] = $area_name.$street_name.$village.$brigade_name;
|
|||
|
|
|||
|
// 更新用户表
|
|||
|
$header = Request::header('x-Token');
|
|||
|
if(isset($header['x-token']) && !empty($header['x-token'])){
|
|||
|
$phone['mobile'] = $post['phone']??'';
|
|||
|
Db::table('fa_user')->where($where)->update($phone);
|
|||
|
}else{
|
|||
|
$post['phone'] = $user['mobile']??'';
|
|||
|
}
|
|||
|
|
|||
|
//写入用户信息表
|
|||
|
$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'] = $post['phone']??'';
|
|||
|
$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::table('fa_szxc_information_usermsg')->where('user_id',$post['user_id'])->find();
|
|||
|
if (!$msg){
|
|||
|
Db::table('fa_szxc_information_usermsg')->strict(false)->insert($data);
|
|||
|
}
|
|||
|
// else{
|
|||
|
// $this->apiError('请勿重新提交');
|
|||
|
// }
|
|||
|
//写入用户地区表
|
|||
|
$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::table('fa_szxc_information_useraddress')->where('user_id',$post['user_id'])->find();
|
|||
|
if (!$add){
|
|||
|
Db::table('fa_szxc_information_useraddress')->strict(false)->insert($address_data);
|
|||
|
}
|
|||
|
// else{
|
|||
|
// $this->apiError('请勿重新提交');
|
|||
|
// }
|
|||
|
// $ret = $this->auth->login($user['username'], '');
|
|||
|
// if ($ret) {
|
|||
|
//创建新Token
|
|||
|
$token = Random::uuid();
|
|||
|
\app\common\library\Token::set($token, $user['id'], 2592000);
|
|||
|
$tokenInfo = \app\common\library\Token::get($token);
|
|||
|
$Userinfo = $user;
|
|||
|
$Userinfo['token'] = $tokenInfo['token'];
|
|||
|
$Userinfo['expires_in'] = $tokenInfo['expires_in'];
|
|||
|
$Userinfo['user_id'] = $post['user_id'];
|
|||
|
$userinfo_data['userinfo'] = $Userinfo;
|
|||
|
|
|||
|
$find=Db::table('fa_szxc_information_usermsg')->where('user_id',$post['user_id'])->find();
|
|||
|
$group=Db::table('fa_user_group')->where('id',$userinfo_data['userinfo']['group_id'])->find();
|
|||
|
$userinfo_data['userinfo']['group_name']=$group['name'];
|
|||
|
if ($find){
|
|||
|
$userinfo_data['userinfo']['name']=$find['name'];
|
|||
|
$userinfo_data['userinfo']['no_update']=0;
|
|||
|
$userinfo_data['userinfo']['address_name']=$find['address_name'];
|
|||
|
}else{
|
|||
|
$userinfo_data['userinfo']['no_update']=1;
|
|||
|
$userinfo_data['userinfo']['address_name']='';
|
|||
|
}
|
|||
|
Db::commit();
|
|||
|
$this->apiSuccess('已完善,登录成功', $userinfo_data);
|
|||
|
// } else {
|
|||
|
// $this->apiError($this->auth->getError());
|
|||
|
// }
|
|||
|
} catch (ValidateException | PDOException | Exception $e) {
|
|||
|
Db::rollback();
|
|||
|
$this->apiError($e->getMessage());
|
|||
|
}
|
|||
|
} else {
|
|||
|
$this->apiError('未注册不能绑定');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
|
|||
|
* 根据身份证号码获取性别
|
|||
|
|
|||
|
* 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;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//验证省份证格式是否正确
|
|||
|
|
|||
|
/**
|
|||
|
* 获取密码加密后的字符串
|
|||
|
* @param string $password 密码
|
|||
|
* @param string $salt 密码盐
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
private function getEncryptPassword($password, $salt = '')
|
|||
|
{
|
|||
|
return md5(md5($password) . $salt);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 身份证号搜索户主
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
public function getUserByIdcard()
|
|||
|
{
|
|||
|
//身份证号
|
|||
|
$idcard = get_params('idcard');
|
|||
|
if (empty($idcard)) {
|
|||
|
$this->apiError('缺少参数');
|
|||
|
}
|
|||
|
$where[] = ['idcard','like','%'.$idcard.'%'];
|
|||
|
$where[] = ['status','=','1'];
|
|||
|
$where[] = ['is_hz','=','1'];
|
|||
|
$hz_arr = Db::table('fa_szxc_information_usermsg')->where($where)->field('user_id,name,idcard,gender,age,phone,address_name')->select()->toArray();
|
|||
|
if ($hz_arr) {
|
|||
|
$this->apiSuccess('获取成功', $hz_arr);
|
|||
|
} else {
|
|||
|
$this->apiError('暂无数据');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public function validateIDCard($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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|