我搜索过社区中,查看了其他很多人的解决方案,一直没有找到解决方法,我是用户信息和手机号一起获取,是这个方向不能实现吗?
前台代码:
授权登陆
js代码:
login: function (res) {
var that=this;
wx.login({
success: function (e) {
var code = encodeURIComponent(e.code);
wx.getUserInfo({
lang: "zh_CN",
success: function (result) {
var encryptedData= encodeURIComponent(res.detail.encryptedData);
var iv=encodeURIComponent(res.detail.iv);
console.log(res.detail.iv)
wx.request({
url: 'xxxxxxxx',
data: {
code: code,
encryptedData:encryptedData,
iv:iv,
distribution:that.data.distribution
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "post",
success: function (res) {}
})
}
})
}
})
// console.log(res)
},
后台php代码:
$post = $_POST;
$code = urldecode($post['code']);
$encryptedData = urldecode($post['encryptedData']);
$iv = urldecode($post['iv']);
// $phone = $post['phone'];
// $sendCode = $post['sendCode'];
$verifyUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->APPID . "&secret=" . $this->secret . "&js_code=" . $code . "&grant_type=authorization_code";
$verify = $this->curl($verifyUrl);
$verify1 = json_decode($verify, true);
$errCode = $this->decryptData($encryptedData, $iv, $verify1['session_key'], $data);
偶尔会出现-41003错误
可以尝试使用checkSession来进行登录态的检查,如果失败的话,重新走wx.login来获取code给后端来解密手机号
具体的官方参考文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
我也遇到这个问题,希望高手解答
具体的官方参考文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html