,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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!