getPhoneNumber: function (e) {//点击获取手机号码按钮
var that = this;
wx.checkSession({
success: function () {
console.log(e.detail.errMsg)
console.log(e.detail.iv)
console.log(e.detail.encryptedData)
------------------------------------------
console.log(e.detail.iv)
console.log(e.detail.encryptedData)
以上这二个返回都是undefined
不知道是什么原因,麻烦指点
你好,麻烦提供下代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)
//WXML
<button class='pop_btn' plain="true" open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber">获取用户手机号</button>
// pages/try/try.js
Page({
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.login({
success: function (res) {
console.log(res)
if (res.code) {
//发起网络请求
wx.request({
url: 'https://XXXXXX',
data: {
'code': res.code
},
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': 'application/json'
}, // 设置请求的 header
success: function (res) {
console.log(res);
wx.setStorageSync('sessionKey', res.data);
},
fail: function (err) {
console.log(err);
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
},
getPhoneNumber: function (e) {//点击获取手机号码按钮
var that = this;
wx.checkSession({
success: function () {
console.log(e.detail.errMsg)
console.log(e.detail.iv)
console.log(e.detail.encryptedData)
var ency = e.detail.encryptedData;
var iv = e.detail.iv;
var sessionk = that.data.sessionKey;
if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
that.setData({
modalstatus: true
});
} else {//同意授权
wx.request({
method: "GET",
url: 'https://XXXXXX',
data: {
encrypdata: ency,
ivdata: iv,
sessionkey: sessionk
},
header: {
'content-type': 'application/json' // 默认值
},
success: (res) => {
console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");
console.log(res);
var phone = res.data.phoneNumber;
console.log(phone);
}, fail: function (res) {
console.log("解密失败~~~~~~~~~~~~~");
console.log(res);
}
});
}
},
fail: function () {
console.log("session_key 已经失效,需要重新执行登录流程");
that.wxlogin(); //重新登录
}
});
},
})
点允许后就出现这个界面
麻烦提供下这类型的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)
已解决!就是填写验证下手机号就行了