1、 通过button触发微信授权:
<button class="button_registe" wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="onGotUserInfo">{{judgePage==0?"注册":"登录"}}
<!-- <view class="button_name" bindtap="jumpMy" wx.if="{{canIUse}}" bindgetuserinfo="onGotUserInfo"></view> -->
</button>
2、查看是否授权:
// 查看是否授权
wxGrant(e) {
// console.log(1111)
let that = this;
wx.getSetting({
success: function(res) {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function(res) {
// console.log(res.userInfo)
// 将微信授权信息存入缓存
wx.setStorageSync('userMessage', res.userInfo)
// console.log("user:" + wx.getStorageSync('user').name)
// console.log(1111)
wx.navigateBack({
changed: true
});
}
})
}
}
})
},
3、监听button事件——把 wxGrant() 方法运行到button监听方法中:
//验证注册登陆
onGotUserInfo(e) {
// console.log(e)
let that = this;
wx.request({
url: app.globalData.url + "/api/personal/add",
data: {
},
method: "post",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function(res) {
that.wxGrant();
}
})
}
})
}
},