layui+Thinkphp5实现QQ互联一键登录代码教程

讨论 未结 精帖
0 1335
Templet360
Templet360 2017-6-13
悬赏:5飞吻
首先创建layui做弹窗登录
<script type="text/javascript">
layui.use(['layer','jquery'], function(){
var layer = layui.layer
,jq = layui.jquery;
jq('.login').click(function(){
layer.open({
type: 2,
title: 'QQ登陆',
maxmin: false, //开启最大化最小化按钮
shadeClose: true,
shade: 0.1,
area: ['500px', '500px'],
content: '__ROOT__/user/login.html',
end: function(){location.reload();}
});
});
})
</script>
创建模板文件:user/login.html
<script type="text/javascript" src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js" data-appid="填写QQ互联ID" data-redirecturi="填写域名回调地址" charset="utf-8"></script>
<script type="text/javascript">
layui.use(['layer'], function(){
var layer = layui.layer,
$ = layui.jquery;
var is_login = QC.Login.check();
if(is_login){
var access_token = '';
var openid = '';
QC.Login.getMe(function(openId, accessToken){
access_token = accessToken;
openid = openId;
});
var paras = {oauth_consumer_key:'填写QQ互联ID',access_token:access_token,openid:openid};
QC.api('get_user_info', paras).success(function(s){
//成功回调,通过s.data获取OpenAPI的返回数据
var param ={openid:openid,username:s.data.nickname,userhead:s.data.figureurl_qq_2,sex:s.data.gender};
$.post('{:url("index/login/loginqq")}',param,function(res){
if(res.code == 200){
QC.Login.signOut()//注销qq登陆
layer.msg(res.msg, {icon: 1, anim: 6, time: 1000});
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
parent.layer.close(index); //再执行关闭
}else{
layer.msg(res.msg, {icon: 2, anim: 6, time: 1000});
}
});

//alert("获取用户信息成功!当前用户昵称为:"+s.data.nickname);
});

}else{
self.location.href='https://graph.qq.com/oauth2.0/authorize?client_id=填写QQ互联ID&response_type=token&scope=all&redirect_uri='+encodeURIComponent(self.location.href);
}
});
</script>
创建控制器文件 如:loginqq
public function loginqq()
{
$member = new MemberModel();
if (request()->isPost()) {
$data = input('post.');
$user = $admin->where('openid', $data['openid'])->find();
if ($user) {
//这里填写你需要写入的数据库
$admin->edit($data);
} else {
//这里填写你需要写入的数据库
$admin->add($data);
}
return json(array('code' => 200, 'msg' => '登录成功'));
}
}
代码案例: http://www.tpt360.com/ 交流群:423348395
回帖
  • 消灭零回复