微信小程序获取用户微信绑定的手机号码|ThinkPHP|getPhoneNumber

,getPhoneNumber:function(e){
if(e.detail.iv&&e.detail.encryptedData){
// console.log(e.detail.iv)
// console.log(e.detail.encryptedData)
wx.login({
success:(res)=>{
wx.request({
url:`https://SERVER/Api/Index/get_session_key`
,header:{'content-type':'application/json'}
,data:{
code:res.code
}
,success:(res)=>{
console.log(res)
let session_key=res.data.session_key
wx.request({
url:`https://SERVER/Api/Index/decryptData2`
,header:{'content-type':'application/json'}
,data:{
'encryptedData':encodeURIComponent(e.detail.encryptedData)
,'iv':e.detail.iv
,'session_key':session_key
}
,success:(res)=>{
console.log(res.data)
if(!res.data){
console.log('获取失败,请重试')
wx.showToast({
title:'网络错误,请重试…'
,icon:'none'
})
}else{
this.setData({
phone:res.data.purePhoneNumber
})
}
}
})
}
})
}
})
}else{
console.log('拒绝获取手机号码')
}
}


function get_session_key($code){
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".C('MP_APPID')."&secret=".C('MP_SECRET')."&js_code=$code&grant_type=authorization_code";
$res=jsondecode($this->httpGet($url));
$out=[
'session_key'=>$res[session_key],
];
$this->ajaxReturn($out);
}

function decryptData2($encryptedData,$iv,$session_key){
$aesKey=base64_decode($session_key);
$aesIV=base64_decode(str_replace(' ','+',$iv));
$aesCipher=base64_decode(urldecode($encryptedData));
$result=openssl_decrypt($aesCipher,"AES-128-CBC",$aesKey,1,$aesIV);
$this->ajaxReturn(jsondecode($result));
}

function httpGet($url){
$curl=curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_TIMEOUT,500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl,CURLOPT_URL,$url);
$res=curl_exec($curl);
curl_close($curl);
return $res;
---------------------
作者:sr_www
来源:CSDN
原文:https://blog.csdn.net/sr_www/article/details/81323588
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/shaozhu520/p/11199013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值