小程序获取手机号+php后台
敲完之后也懒得改了 不足的地方还请指教
话不多说直接上代码
图:
获取微信用户绑定的手机号,需先调用wx.login接口。
因为需要用户主动触发才能发起获取手机号接口,所以该功能不由 API 来调用,需用 button 组件的点击来触发。
代码如下
<!--wxml-->
<button class='bottom' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
授权登录
</button>
//js代码
getPhoneNumber: function(e) {
console.log(e.detail.errMsg)
if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
wx.showModal({
title: '提示',
showCancel: false,
content: '未授权',
success: function (res) { }
})
} else {
//调用接口获取登录凭证
wx.login({
success: function (res) {
//获取到的code
var code = res.code;
if (res.code) {
//发起网络请求
console.log(res.code)
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
var that = this;
console.log("同意授权")
//获取opendi、session_key
wx.request({
url: 'xxxxxxxxxx/good/getOpid',
data: {
code: code,
},
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log(res.data.session_key,"session_key")//获取到的session_key
console.log(e.detail.errMsg,"errMsg")
console.log(e.detail.iv,"iv")
console.log(e.detail.encryptedData,"encryptedData")//完整用户信息的加密数据
var se_key = res.data.session_key
var encryptedData = e.detail.encryptedData
var iv = e.detail.iv
//换取手机号
wx.request({
url: 'xxxxxxx/demo.php',
data:{
encryptedData:encryptedData,
iv:iv,
se_key:se_key
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method:'POST',
success:function(res){
console.log(res.data,"获取到的用户手机号")
}
})
},
});
}
});
}
},
获取的openidID、session_key (php后台)
//获取Openid
public function getOpid()
{
$appid = 'xxxxx'; // 小程序APPID
$secret = 'xxxxxx'; // 小程序secret
$code = Request()->param('code');//code
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
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;
}
解密算法开发文档有详情---->解密开发文档
如果懒的下载可以复制我的
demo.php
<?php
include_once "wxBizDataCrypt.php";
$appid = 'xxxx';
$sessionKey = $_POST['se_key'];//小程序传传过来的session_key
$encryptedData = $_POST['encryptedData'];//小程序传传过来的encryptedData
$iv = $_POST['iv'];//小程序传传过来的iv
$pc = new wxBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
echo $data;
} else {
echo $data;
}
wxBizDataCrypt.php
<?php
/**
* 对微信小程序用户加密数据的解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
include_once "errorCode.php";
class WXBizDataCrypt
{
private $appid;
private $sessionKey;
/**
* 构造函数
* @param $sessionKey string 用户在小程序登录后获取的会话密钥
* @param $appid string 小程序的appid
*/
public function __construct( $appid, $sessionKey)
{
$this->sessionKey = $sessionKey;
$this->appid = $appid;
}
/**
* 检验数据的真实性,并且获取解密后的明文.
* @param $encryptedData string 加密的用户数据
* @param $iv string 与用户数据一同返回的初始向量
* @param $data string 解密后的原文
*
* @return int 成功0,失败返回对应的错误码
*/
public function decryptData( $encryptedData, $iv, &$data )
{
if (strlen($this->sessionKey) != 24) {
return ErrorCode::$IllegalAesKey;
}
$aesKey=base64_decode($this->sessionKey);
if (strlen($iv) != 24) {
return ErrorCode::$IllegalIv;
}
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json_decode( $result );
if( $dataObj == NULL )
{
return ErrorCode::$IllegalBuffer;
}
if( $dataObj->watermark->appid != $this->appid )
{
return ErrorCode::$IllegalBuffer;
}
$data = $result;
return ErrorCode::$OK;
}
}
errorCode.php
<?php
/**
* error code 说明.
* <ul>
* <li>-41001: encodingAesKey 非法</li>
* <li>-41003: aes 解密失败</li>
* <li>-41004: 解密后得到的buffer非法</li>
* <li>-41005: base64加密失败</li>
* <li>-41016: base64解密失败</li>
* </ul>
*/
class ErrorCode
{
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
}
?>
最终获取到手机号