** 授权用户信息和授权手机号,必须用按钮触发,获取手机号信息必须使用企业得appid,个人得appid获取不到手机号**
看代码
html
<button bindgetuserinfo="show" open-type="getUserInfo">获取授权</button>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">手机号授权</button>
show: function () {
var that = this;
wx.getSetting({
success: function (a) {
a.authSetting["scope.userInfo"] && wx.getUserInfo({
success: function (a) {
wx.showToast({ title: '授权成功' })
}
});
}
});
},
getPhoneNumber(e) {
console.log("授权用户手机号");
console.log(e,'eeeeee');
var that = this;
if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
wx.showToast({ title: '已取消授权' })
}
if (e.detail.errMsg == "getPhoneNumber:ok") {
var iv = e.detail.iv;
var encryptedData = e.detail.encryptedData;
app.util.request({ //手机号解密
url: "auth/session/mobile",
method: 'post',
data: {
version: app.globalData.version,
encryptedData: encryptedData,
iv: iv
},
success: function (a) {
if (a.data.errno == 0) {
wx.showToast({ title: '授权成功' })
}
}
});
}
},