官方说明源文档:
https://developers.weixin.qq.com/miniprogram/dev/api/getPhoneNumber.html
1.我们先处理js部分,先调用login接口
onLoad: function () {
var that = this;
wx.login({
success: function (loginCode) {
wx.request({
url: ‘https://********/login.php’,
data: {
code: loginCode.code,
type: ‘openid’
},
header: {
‘content-type’: ‘application/x-www-form-urlencoded’
},
method: ‘POST’,
success: function (res) {
that.setData({
openid: res.data.openid,
})
wx.setStorageSync(‘openid’, res.data.openid);
}
})
}
})
var openid2 = wx.getStorageSync(‘openid’);
},
complete: function () {
setTimeout(function () {
wx.hideLoading()
}, 1000)
wx.hideNavigationBarLoading()
}
})
}
2.获取电话接口
getPhoneNumber: function (e) {
console.log(e.detail.errMsg)
console.log(e.detail.iv)
console.log(e.detail.encryptedData)
wx.request({
url: “https://*******tel.php”,
data: {
iv: e.detail.iv,
encryptedData: e.detail.encryptedData,
session_key: wx.getStorageSync(‘session_key’)
},
header: {
‘content-type’: ‘application/x-www-form-urlencoded’
},
method: ‘POST’,
success: function (res) {
console.log(res);
},
fail: function () {
// fail
},
complete: function (openid) {
// complete
}
});
},
3.在wxml里面调用
<button open-type=”getPhoneNumber” bindgetphonenumber=”getPhoneNumber”> 获取手机号</button>
4.后端代码:
是压缩包,可以留言索取。