1. 接口改变用户授权后直接就能获取到对应的信息
代码示例如下:
<button class="submit-btn" type="primary" open-type="getUserInfo" withCredentials="true" @getuserinfo="wxGetUserInfo">微信授权</button>
// 对应的方法
wxGetUserInfo(e) {
console.log(e.detail)
}
2. 但是这样感觉体验不好,想要它还是出现对话框怎么做?
直接添加点击事件
<button class="submit-btn" type="primary" @click="getUserProfile">微信授权</button>
然后在methods中写对应方法,使用uni.getUserProfile()方法调用即可。
getUserProfile() {
uni.getUserProfile({
desc:'登录',
success:(res) => {
// 拿到encryptedData和iv后面的调用后台接口传递参数时需要用到
console.log('登录成功:', res);
this.resEncryptedData = res.encryptedData
this.resIv = res.iv
},
fail:(res) => {
console.log('登录失败:', res);
uni.showToast({
title: '您拒绝了微信授权,将无法使用部分功能!',
icon: 'none',
mask: true
})
}
});
}