//onLoad 里
wx.login({
success(res) {
console.log(res);
var code = res.code
var appid = '自己的appid';
var appSecret = '自己的appSecret'
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+appSecret+'&js_code='+code+'&grant_type=authorization_code',
method: "post",
success: function (res) {
console.log(res.data);
console.log(res.data.openid);
console.log(res.data.session_key);
}
})
}
})
//Page 里
getPhoneNumber: function (e) {
var that = this;
console.log(e.detail.errMsg == "getPhoneNumber:ok");
// 如果允许:true 如果拒绝:false
if (e.detail.errMsg == "getPhoneNumber:ok") {
//回调函数
wx.request({
url: 'http://www.baidu.com',
data: { },
method: "post",
success: function (res) {
console.log(res);
}
})
}
},
//html 里
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>