小程序端代码
onLoad: function(i) {
let that = this;
wx.login({
success:login=>{
getApp().request({
s:'User.getSesson',
code:login.code
}).then(res=>{
that.setData({
session_key: res.data.data.session_key,
});
});
}
})
},
getPhoneNumber: function(phone) {
getApp().request({
's': 'User.getPhone',
session_key: that.data.session_key,
encryptedData: phone.detail.encryptedData,
iv: phone.detail.iv,
}).then(res => {
if (res && res.data && res.data.data) {
} else {
wx.showModal({
title: '提示',
content: '网络异常,请重新获取手机号',
})
}
});
},
request: function(param, methods = 'post') {
return new Promise(success => {
wx.request({
method: methods ? methods : 'post',
url: 'https://yld.angyakeji.com/www/public/index.php',
data: param,
complete: function(res) {
success(res);
}
})
});
},
PHP
public function getSesson()
{
header("Content-Type:text/html;charset=utf8");
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:*');
error_reporting(E_ALL);
$code = $_REQUEST['code'];
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=wx231bd3f08954da9e&secret=edf8c4929f2ad86db08e0cd3c588a14c&js_code=' . $code . '&grant_type=authorization_code';
$html = file_get_contents($url);
$html = json_decode($html);
return $html;
}
public function getPhone()
{
$aesKey = base64_decode($_REQUEST['session_key']);
$aesIV = base64_decode($_REQUEST['iv']);
$aesCipher = base64_decode($_REQUEST['encryptedData']);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
return json_decode($result, true);
}