微信小程序同一个登录按钮上触发获取微信用户信息和电话号码
因为微信小程序获取用户信息getuserinfo方法和获取电话号码open-type=“getPhoneNumber” @getphonenumber="onGetPhoneNumber"必须绑定在组件上,无法在script中触发,但是如果绑定在同一个按钮上,两个事件就会互相冲突,没有办法按顺序执行,所以需要用冒泡的优先级。在按钮外面再包裹一层div,在div上绑定获取微信用户信息的事件,在内部按钮上绑定获取电话号码的事件,这样就可以先触发获取微信用户信息,再获取电话号码了
<view @click="getuserinfo"><button class="btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">授权登录</button></view>
getuserinfo() {
var _this = this;
wx.getUserProfile({
desc: '用于完善客户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
console.log(res,'微信用户信息')
if (!res.encryptedData || !res.iv) return false;
this.userInfo = res.userInfo;
}
})
},
onGetPhoneNumber(e) {
let encryptedData = e.detail.encryptedData;
let iv = e.detail.iv;
let pc = new WXBizDataCrypt(this.appid, this.session_key)
let phoneNum = pc.decryptData(encryptedData , iv)
this.phoneNum = phoneNum.phoneNumber;
if(this.phoneNum && this.phoneNum != ""){
uni.switchTab({
url:"/pages/order/index"
})
}
}