已解决,不知道靠不靠谱贴上代码。 button 组件授权后在调用wx.login 进行解密
userInfoHandler: function (e) {
let that = this;
if (e.detail.errMsg ==‘getUserInfo:ok’) {
wx.login({
success: function ® {
let code = r.code;
if (code) {
// 解密用户数据
wx.request({
url: url,
method: ‘post’,
header: {‘content-type’: ‘application/x-www-form-urlencoded’},
data: {encryptedData: e.detail.encryptedData, iv: e.detail.iv, code: code},
success: function (data) {
console.log(data);
if (data.data.status == 1) {
app.globalData.userInfo = data.data.userInfo;
wx.setStorageSync(‘userInfo’, data.data.userInfo); // 缓存用户信息
typeof cb == “function” && cb(app.globalData.userInfo);
app.navigateBack(‘navigateTo’, ‘/pages/index/index’);
} else {
// 失败
that.showMsg(data.data.msg);
}
},
fail: function (err) {
that.showMsg(err.errMsg);
console.log(err)
}
})
} else {
that.showMsg(err.errMsg);
console.log(‘获取用户登录态失败!’, err)
}
},
fail: function (err) {
that.showMsg(err.errMsg);
console.log(‘登陆失败’, err);
}
})
}
}