1、绑定按钮
<button class="weiBtn" open-type="getUserInfo" bindgetuserinfo="getlogin" bindtap="tozhangLogin">微信登录</button>
2、获取用户信息
getlogin: function(e) {
if (e.detail.userInfo) {
this.setData({
usertx: e.detail.userInfo.avatarUrl
})
}
},
3、wx.login 获取code、根据code获取openID
wx.login({
//获取code
success: function (res) {
var code = res.code; //返回code
console.log(code);
var appId = '。。。';//微信小程序AppID
var secret = '。。。';//可在微信开发设置根据二维码获取
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appId+'&secret='+secret+'&js_code='+code+'&grant_type=authorization_code',
data: {},
header: {
'content-type': 'json'
},
success: function (res) {
var openid = res.data.openid //返回openid
}
})
}
})
注:代码仅供参考,如有不对,请留言。
标注:微信一键登录只能测试使用、微信平台子域名暂不支持配置、可使用后台服务获取openID