使用 button 组件,并将 open-type 指定为 getUserInfo 类型,用户允许授权后,可获取用户基本信息
提示一下
wx.showModal({
title: '注意',
showCancel: false,
confirmText:'好去授权',
content: '为了您更好的体验,请先同意授权',
success: function (res) {
wx.navigateTo({
url: '../userinfo/index'
});
}
})
- open http://t.cn/R35ZdqX
<button open-type="getUserInfo" bindgetuserinfo="bindgetuserinfo">点击授权</button>
- 没有判断是否授权过,只是测试
bindgetuserinfo: function (e) {
var that = this;
if (e.detail.userInfo) {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.login({
success: res => {
console.log(res.code, e.detail.iv,e.detail.encryptedData)
wx.request({
//后台接口地址
url: '',
data: {
code:res.code,
iv:e.detail.iv,
encryptedData:e.detail.encryptedData,
},
method: 'GET',
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('请求成功')
}
})
}
})
} else {
console.log(333,'执行到这里,说明拒绝了授权')
wx.showToast({
title: "为了您更好的体验,请先同意授权",
icon: 'none',
duration: 2000
});
}
}