微信登录

前台调用:
 
后台接口:
//微信登录
public function wxlogin(){
$code = input('code');
// $code = '021FTdCd1wHSHs0LuWBd1d8jCd1FTdCc';
if(!empty($code)){
//获取access_token
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appId.'&secret='.$this->appSecret.'&code='.$code.'&grant_type=authorization_code';
 
$cont = file_get_contents($url);
$request = json_decode($cont,true);
if(isset($request['errcode']) || !empty($request['errcode'])){
print_r($request['errmsg']);die;
}
$access_token = $request['access_token'];
$expires_in = $request['expires_in'];
$refresh_token = $request['refresh_token'];
$openid = $request['openid'];
$unionid = $request['unionid'];
 
//刷新access_token值
$url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$this->appId.'&grant_type=refresh_token&refresh_token='.$refresh_token;
$cont = file_get_contents($url);
$request = json_decode($cont,true);
if(isset($request['errcode']) || !empty($request['errcode'])){
print_r($request['errmsg']);die;
}
// echo '<pre/>';print_r($request);die;
// 获取用户个人信息(UnionID机制)
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$request['access_token'].'&openid='.$request['openid'];
 
$cont = file_get_contents($url);
$request = json_decode($cont,true);
if(isset($request['errcode']) || !empty($request['errcode'])){
print_r($request['errmsg']);die;
}
 
//处理用户信息 如果用户已存在就更改,不存在就添加用户
 
// echo '<pre/>';print_r($request);die;
 
$userinfo = $this->wxuserinfo($request);
// echo '<pre/>';print_r($userinfo);die;
$userinfo = json_decode($userinfo,true);
 
 
 
if(!empty($userinfo['errcode'])){
print_r($userinfo['errmsg']);die;
}else{
$ural = 'http://mw1950.2om.cn/index.php/index/index';
 
header('location:'.$ural);
}
 
 
return json(['code' => 200, 'msg' => '登录成功' , 'data' =>$userinfo]);
}else{
dump('没有拉取到code信息'); die();
}
}
 
 
//微信用户处理
public function wxuserinfo($canshu){
 
if(empty($canshu) || empty($canshu['unionid'])){
return json_encode(['errcode'=>102,'errmsg'=>'参数不能为空']);
}
 
$userdb = db('user');
$user_opendb = db('user_open');
 
//判断user_open表是否存在用户信息
$openinfo = $user_opendb->where("unionid='".$canshu['unionid']."' ")->find();
// echo '<pre/>';print_r($openinfo);die;
if(!$openinfo || empty($openinfo['userid'])){
 
$salt = substr(uniqid(rand()), -6); //安全码
//添加用户
$rea = [
'nickname' => $canshu['nickname'],
'username' => $canshu['nickname'],
'face' => $canshu['headimgurl'],
'country' => $canshu['country'],
'gender' => $canshu['sex'],
'language' => $canshu['language'],
'city' => $canshu['city'],
'province' => $canshu['province'],
'salt' => $salt,
'addtime' => time(),
'lastlogintime' => time()
 
];
 
$userid = $userdb->insertGetId($rea);
// die;
 
 
if(!$userid){
return json_encode(['errcode'=>102,'errmsg'=>'用户注册失败']);
}
 
 
//添加user_open信息
$data = [
'userid' => $userid,
'unionid' => $canshu['unionid'],
'openid' => $canshu['openid'],
'appid' => $this->appId,
'opentype' => 'wechat',
'addtime' => time()
];
// echo '<pre/>';print_r($data);die;
$openid = $user_opendb->insert($data);
 
if(!$openid){
return json_encode(['errcode'=>102,'errmsg'=>'用户注册失败!']);
}
 
$userdata = $userdb->where('userid='.$userid.' and del=0')->find();
if(!$userdata){
$user_opendb->where('userid='.$userid)->delete();
return json_encode(['errcode'=>102,'errmsg'=>'用户注册失败重新注册']);
}
$userdatas = json_encode($userdata);
$userids = $userdata['userid'];
 
session(null, 'api');
$token=session_id();
 
Cookie::set('userdata',$userdatas);
Cookie::set('userid',$userids);
Cookie::set('token',$token);
 
return json_encode(['errcode'=>0,'errmsg'=>'SUCCESS','data'=>['__token'=>$token,'userid'=>$userids,'userdata'=>$userdatas]]);
}else{
 
$userdata = $userdb->where('userid='.$openinfo['userid'].' and del=0')->find();
if(!$userdata){
$user_opendb->where('userid='.$openinfo['userid'])->delete();
return json_encode(['errcode'=>'102','errmsg'=>'登录信息异常,请重新登录']);
}
 
if($userdata['status'] != 1){
return json_encode(['errcode'=>102,'errmsg'=>'该用户已被禁止登陆']);
}
 
$data = array();
$data['lastlogintime'] = time();
$data['nickname'] = $canshu['nickname'];
$data['username'] = $canshu['nickname'];
$data['face'] = $canshu['headimgurl'];
 
$userins = $userdb->where('userid='.$userdata['userid'])->update($data);
if(!$userins){
return json_encode(['errcode'=>102,'errmsg'=>'用户信息获取失败']);
}
$userdatas = json_encode($userdata);
$userids = $userdata['userid'];
 
session(null, 'api');
$token=session_id();
 
// echo '<pre/>';print_r($token);die;
 
Cookie::set('userdata',$userdatas);
Cookie::set('userid',$userids);
Cookie::set('token',$token);
 
return json_encode(['errcode'=>0,'errmsg'=>'SUCCESS','data'=>['__token'=>$token,'userid'=>$userids,'userdata'=>$userdatas]]);
}
 
}

posted @ 2020-12-04 20:32  诸天一根草凡尘一粒沙  阅读(66)  评论(0编辑  收藏  举报