public function oauth_qq()
{
$conf=config("qq");
$callbackUrl="https://www.wkcto.com/callback/qq";
$urlArr=[
"response_type"=>"code",
"client_id"=>$conf['appId'],
"redirect_uri"=>$callbackUrl,
"scope"=>"get_user_info"
];
$uri=http_build_query($urlArr);
$url="https://graph.qq.com/oauth2.0/authorize?".$uri;
header("location: ".$url);
}
public function requestQQ($url){
$response =HttpClient::get($url);
$result=[];
//如果用户临时改变主意取消登录,返回true!==false,否则执行step3
if (strpos($response, "callback") !== FALSE) {
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos - 1);
$msg = json_decode($response);
if (isset($msg->error)) {
$result['errorNum']=$msg->error;
$result['msg']=$msg->error_description;
$result['status']=0;
$result['data']="";
return $result;
}else{
$result['msg']="获取成功";
$result['errorNum']=0;
$result['status']=1;
$result['data']=get_object_vars($msg);
return $result;
}
}
if(substr($response,0,1)=="{"){
$accessTokenArr=json_decode($response,true);
}else{
parse_str($response,$accessTokenArr);
}
$result['msg']="获取成功";
$result['errorNum']=0;
$result['status']=1;
$result['data']=$accessTokenArr;
return $result;
}
public function qq(Request $request)
{
$code=$request->param("code");
$conf=config('qq');
$callbackUrl="https://www.wkcto.com/callback/qq";
if(!empty($code)) {
$uriArr = [
"grant_type" => "authorization_code",
"client_id" => $conf['appId'],
"client_secret" => $conf['appKey'],
"code" => $code,
"state" => "123",
"redirect_uri" => $callbackUrl
];
$uri = http_build_query($uriArr);
$url = "https://graph.qq.com/oauth2.0/token?".$uri;
$accessData=$this->requestQQ($url);
if($accessData['status']){
$url="https://graph.qq.com/oauth2.0/me?access_token=".$accessData['data']['access_token'];
$openIdData=$this->requestQQ($url);
if(!$openIdData['status']){
die($openIdData['msg']);
}
$member=Db::name("oauth_member_bind")->where(['openid'=>$openIdData['data']['openid'],'type'=>2])->find();
if(is_array($member) && !empty($member['member_id'])){
$redis=redisPool::getRedis();
//如果存在用户的时候直接登录
$info=Db::name('user_info')->where(['pid'=>$member['member_id']])->field('avatar,nickname')
->find();
$sessionData= [];
$sessionData["id"]=$member['user_id'];
$sessionData["nickname"]=$info['nickname'];
$sessionData["avatar"]=$info['avatar'];
$this->UserUniqueLogin($sessionData);
session('user',$sessionData);
$redis->select(3);
$redis->setBit("login:".date("Y-m-d",time()),$member['user_id'],1);
$redis->close();
}
if(empty($member)){
$date=date('Y-m-d H:i:s');
$data=[
'openid'=>$openIdData['data']['openid'],
'client_type'=>"qq",
"type"=>2,
"extra"=>json_encode($accessData['data']),
"updated_time"=>$date,
'created_time'=>$date
];
Db::name("oauth_member_bind")->insert($data);
}
$uriArr=[
"access_token"=>$accessData['data']['access_token'],
"oauth_consumer_key"=>$conf['appId'],
"openid"=>$openIdData['data']['openid']
];
$uri=http_build_query($uriArr);
$url="https://graph.qq.com/user/get_user_info?".$uri;
$userInfo=$this->requestQQ($url);
if($userInfo['status']){
cookie('openid',$openIdData['data']['openid']);
if($userInfo['data']['gender']=="男"){$sex=1;}else{$sex=0;}
$cache=[
'nickname'=>$userInfo['data']['nickname'],
'avatar'=>$userInfo['data']['figureurl_2'],
'sex'=>$sex,
];
cache($openIdData['data']['openid'],json_encode($cache),array('type'=>'file','expire'=>3600));
//跳转到资料补充页面
echo "<script>window.opener.location.href='/user/basic';window.close();</script>";
}
}else{
$error="[".date('Y-m-d H:i:s')."]"." QQ授权登录:".$accessData['msg']."、错误号:".$accessData['errorNum'];
Log::error($error);
$this->error($accessData['msg']);
die;
}
}
}
Last modification:January 30, 2020
© Allow specification reprint