微信小程序获取手机号实例(php后端)
发布于 4 年前 作者 liaoxiuying 5365 次浏览 来自 分享

官方说明源文档:

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.后端代码:

是压缩包,可以留言索取。

2 回复

session_key不应该返回。通过checkSession判断session_key是否失效。如果没失效传

iv和encryptedData即可找后端换取解密后的信息,如果失效 重新wx.login一次,使用code去换取解密后的信息。

厉害了,把session_key送到前端

回到顶部