一个新站,重新与你们分享故事

微信小程序获取手机号实例(php后端)

小杂念 wangfa 418℃ 0评论

官方说明源文档:

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);
wx.setStorageSync(‘session_key’, res.data.session_key);
console.log(res.data.unionid);
}
})
}
})
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.后端代码:
获取电话号码

转载请注明:王三去-和你分享我们的故事|www.wangfa.org » 微信小程序获取手机号实例(php后端)

喜欢 (2)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址