第三方微信登录
Posted 码农编程进阶笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三方微信登录相关的知识,希望对你有一定的参考价值。
https://mp.weixin.qq.com/s/cTaIv28GyFZvc-Gj15TYnA
1、首先引入
2、使用auth方法
3、配置
3 - 1 首页需要到微信开放平台去申请审核 https://open.weixin.qq.com/
注:如果需要ios 和 安卓 使用,请注意上图 将ios和android应用都勾选上
4、auth方法成功之后,使用getToken方法
5、在使用getToken方法成功后,使用getUserInfo方法
将获取到的用户信息按需要逻辑处理
附上小编的项目代码
小编的代码有处理是否绑定的逻辑以及登录等,大家只需参考wx登录部分即可
html部分
<div class="aui-col-xs-2" style="text-align: center;" tapmode onclick="wxauth()"> <i class="iconfont icon-liaotian icon_bg2"></i> <div class="aui-grid-label" style="display:block;margin-top:10px;">微信</div></div>
js部分
//微信登录 function wxauth() { api.showProgress({ style: \'default\', animationType: \'fade\', title: \'\', text: \'启动中...\', modal: false }); var wx = api.require(\'wx\'); wx.auth({ apiKey: \'wx***********\' }, function(ret, err) { api.hideProgress(); if (ret) { if (ret.status) { var code = ret.code; getToken(code); } else { api.toast({ msg: \'错误\', duration: 2000, location: \'middle\' }); } } else { switch(err.code) { case -1: api.toast({ msg: \'未知错误\', duration: 2000, location: \'middle\' }); break; case 1: api.toast({ msg: \'用户取消\', duration: 2000, location: \'middle\' }); break; case 2: api.toast({ msg: \'用户拒绝授权\', duration: 2000, location: \'middle\' }); break; case 3: api.toast({ msg: \'当前设备未安装微信客户端\', duration: 2000, location: \'middle\' }); break; } } }); }
/** * [getToken 获取用户token] * @param {[type]} code [授权成功后返回code 参数] * @return {[type]} [description] */ function getToken(code) { var wx = api.require(\'wx\'); wx.getToken({ apiKey: \'\', apiSecret: \'\', code: code }, function(ret, err) { if (ret) { if (ret.status) { var accessToken=ret.accessToken; var dynamicToken=ret.dynamicToken; var openId=ret.openId; getUserInfo(accessToken,openId); } else { api.toast({ msg: \'错误\', duration: 2000, location: \'middle\' }); } } else { switch(err.code) { case -1: api.toast({ msg: \'未知错误\', duration: 2000, location: \'middle\' }); break; case 1: api.toast({ msg: \'apiKey值为空或非法\', duration: 2000, location: \'middle\' }); break; case 2: api.toast({ msg: \'apiSecret值为空或非法\', duration: 2000, location: \'middle\' }); break; case 3: api.toast({ msg: \'code值为空或非法\', duration: 2000, location: \'middle\' }); break; case 4: api.toast({ msg: \'网络超时\', duration: 2000, location: \'middle\' }); break; } } }); }
/** * [getUserInfo 获取用户信息] * @param {[type]} accessToken [getToken 接口或 refreshToken 接口成功获取的 accessToken 值] * @param {[type]} openId [getToken 接口或 refreshToken 接口成功获取的 openId 值] */ function getUserInfo(accessToken,openId) { var wx = api.require(\'wx\'); wx.getUserInfo({ accessToken: accessToken, openId: openId }, function(ret, err) { if (ret) { if (ret.status) { var openid=ret.openid, unionid = ret.unionid, nickname=ret.nickname, head_pic=ret.headimgurl, source=\'weixin\'; api.ajax({ url: domainName + \'/index.php?g=User&m=Api&a=thirdLogin\', method: \'post\', data: { values: { openid: openid, unionid: unionid, nickname: nickname, head_pic: head_pic, source: source } } }, function(ret, err) { if (ret) { if (ret.msg == \'0\') { //已经存在该用户信息 if (ret.binding_status == \'1\') { /*1:未绑定 2已绑定*/ //未绑定,到绑定页面 api.openWin({ name: \'user_binding_win\', url: \'./user_binding_win.html\', pageParam: { openid: openid, source: source } }); } else { //已绑定,到登录页面 userInfoAll(openid,source); api.openWin({ name: \'index\', url: \'../main.html\' }); } } else if (ret.msg == \'1\'){ //添加用户信息成功 api.openWin({ name: \'user_binding_win\', url: \'./user_binding_win.html\', pageParam: { openid: openid } }); } else if (ret.msg ==\'2\'){ api.toast({ msg: \'添加用户失败\', duration: 2000, location: \'bottom\' }); } else { api.toast({ msg: \'未知错误\', duration: 2000, location: \'bottom\' }); } } else { api.toast({ msg: \'未知错误\', duration: 2000, location: \'middle\' }); } }); } else { api.toast({ msg: \'错误\', duration: 2000, location: \'middle\' }); } } else { switch(err.code) { case -1: api.toast({ msg: \'未知错误\', duration: 2000, location: \'middle\' }); break; case 1: api.toast({ msg: \'accessToken 过期\', duration: 2000, location: \'middle\' }); break; case 2: api.toast({ msg: \'openId非法\', duration: 2000, location: \'middle\' }); break; case 3: api.toast({ msg: \'openId值为空\', duration: 2000, location: \'middle\' }); break; case 4: api.toast({ msg: \'accessToken值为空\', duration: 2000, location: \'middle\' }); break; case 5: api.toast({ msg: \'accessToken非法\', duration: 2000, location: \'middle\' }); break; case 6: api.toast({ msg: \'网络超时\', duration: 2000, location: \'middle\' }); break; } } }); }
以上是关于第三方微信登录的主要内容,如果未能解决你的问题,请参考以下文章
公众号微信第三方登录(静默授权和非静默授权)(具体代码:U盘 新浪云SAE)