网站qq登陆
在QQ互联注册好你的网站:https://connect.qq.com/
拿到:APP ID 与 APP Key 与 设定好的网站回调域
我使用的是 JS_SDK 登陆,这个比较简单,不需要像php需要引入sdk类文件
手册:https://wiki.connect.qq.com/js_sdk%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e
1、引入 js
<script type="text/javascript" charset="utf-8" src="http://connect.qq.com/qc_jssdk.js" data-appid="你的appId" data-redirecturi="网站回调域"></script> <div class="icon iconfont " id="qqLoginBtn" title="QQ登陆"><span class="qq iconfont icon-qq-copy"></span></div> //qq登陆 if(typeof QC != 'undefined'){ QC.Login({ btnId:"qqLoginBtn" //插入按钮的节点id },function(reqData, opts){ userinfo = {}; QC.Login.getMe(function(openId, accessToken){ //提取信息 userinfo.openid = openId; userinfo.username = reqData.nickname; userinfo.headimg = reqData.figureurl_2; userinfo.sex = reqData.gender=='男'?1:2; userinfo.province = reqData.province; userinfo.city = reqData.city; //发送 if(QC.Login.check()){ $.ajax({ url:'/index/member/qqlogin', type: 'POST', data: userinfo, success : function(data){ if(data.sta == 1){ layer.msg(data.msg,{time:2000},function() { var backUrl = getUrlParam('backUrl'); if(backUrl){ window.location.replace(backUrl); //返回指定地址 }else{ window.location.replace('/index/member.html');//返回会员中心 } }); $('#loginForm .submit').removeClass('dis').text('登陆成功'); }else{ layer.alert(data.msg,{icon:0}); } } }); //退出登陆 QC.Login.signOut(); } }) }); //QQ登陆弹窗居中优化 setTimeout(function(){ var js = $('#qqLoginBtn a').attr('onclick'); var l = ($(window).width()-710)/2; var t = ($(window).height()-405)/2; js = js.replace('height=525,width=585,', 'height=405,width=710,top='+t+', left='+l+','); $('#qqLoginBtn a').attr('onclick',js); },500); }
2 网站回调域 里输出
echo '登陆成功,正在跳转...'; echo '<script type="text/javascript" src="//connect.qq.com/qc_jssdk.js" charset="utf-8" data-callback="true"></script>';
3 接收QQ登陆用户信息
if(request()->isPost()){ $post = input('post.','','strip_tags'); $user = Db::name('member')->where(['openid'=>$post['openid']])->find(); if($user){ $this->buildLogin($user); return ['sta'=>1,'msg'=>'登陆成功!']; }else{ $ip = request()->ip(); $addtime = $lasttime = time(); $user =['ip'=>$ip,'username'=>$post['username'],'headimg'=>$post['headimg'],'openid'=>$post['openid'],'sex'=>$post['sex'],'userType'=>'qq','addtime'=>$addtime,'lasttime'=>$lasttime]; $user['id'] = Db::name('member')->insertGetId($user); $this->buildLogin($user); return ['sta'=>1,'msg'=>'注册并登陆成功!']; } }