Skip to content

Commit

Permalink
1. 修改MiniProgram.php
Browse files Browse the repository at this point in the history
  • Loading branch information
shinn-lancelot committed Jan 7, 2019
1 parent fad0a98 commit b4dda18
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/MiniProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MiniProgram
private $appId = '';
private $appSecret = '';
private $accessToken = '';
private $defaultParamInfo = array();
public $accessTokenData = array();

const API_HOST = 'https://api.weixin.qq.com';
Expand All @@ -38,6 +39,7 @@ public function __construct($appId, $appSecret, $accessToken = '')
$this->accessTokenData = $this->getAccessTokenData();
$this->accessTokenData && $this->accessToken = $this->accessTokenData['access_token'];
}
$this->defaultParamInfo = include_once 'DefaultParamInfo.php';
}

/**
Expand Down Expand Up @@ -85,17 +87,20 @@ public function jscode2Session($code)
*/
public function decryptData($paramArr = array())
{
// 扩展原参数数组
$finalParamArr = Common::extendArrayData($this->defaultParamInfo[__FUNCTION__], $paramArr);

// 初始化返回数据
$res['code'] = -100;
$res['msg'] = '操作失败';
$res['data'] = array();

// 1.获取openid、session_key(若存在session_key,则默认理解为session_key未过期,直接使用其进行解密)
if (isset($paramArr['sessionKey']) && $paramArr['sessionKey']) {
$openId = isset($paramArr['openId']) ? $paramArr['openId'] : '';
$sessionKey = $paramArr['sessionKey'];
if ($finalParamArr['sessionKey']) {
$openId = $finalParamArr['openId'];
$sessionKey = $finalParamArr['sessionKey'];
} else {
$sessionData = $this->jscode2Session($paramArr['code']);
$sessionData = $this->jscode2Session($finalParamArr['code']);
if (isset($sessionData['errcode'])) {
$res['code'] = -101;
$res['msg'] = Common::getErrorMsg($sessionData['errcode']);
Expand All @@ -106,8 +111,8 @@ public function decryptData($paramArr = array())
}

// 2.计算签名并与传入签名进行校验
$newSignature = sha1($paramArr['rawData'] . $sessionKey);
if ($newSignature !== $paramArr['signature']) {
$newSignature = sha1($finalParamArr['rawData'] . $sessionKey);
if ($newSignature !== $finalParamArr['signature']) {
$res['code'] = -102;
$res['msg'] = '签名不匹配';
return $res;
Expand All @@ -116,7 +121,7 @@ public function decryptData($paramArr = array())
// 3.使用sessionKey解密加密数据包
include_once "wxBizDataCrypt/wxBizDataCrypt.php";
$pc = new \WXBizDataCrypt($this->appId, $sessionKey);
$errCode = $pc->decryptData($paramArr['encryptedData'], $paramArr['iv'], $data);
$errCode = $pc->decryptData($finalParamArr['encryptedData'], $finalParamArr['iv'], $data);
if (!empty($errCode)) {
$res['code'] = -103;
$res['msg'] = Common::getErrorMsg($errCode);
Expand All @@ -125,7 +130,7 @@ public function decryptData($paramArr = array())

// 4.生成3rd_session
$data = json_decode($data, true);
$session3rd = Common::getNonce();
$session3rd = Common::isWin() === true ? Common::getNonce() : Common::getNonceByURandom();

// 5.返回相关数据
$res['code'] = 100;
Expand All @@ -144,11 +149,12 @@ public function decryptData($paramArr = array())
*/
public function createWXAQRCode($postParamArr = array())
{
$finalParamArr = Common::extendArrayData($this->defaultParamInfo[__FUNCTION__], $postParamArr);
$urlParamArr = array(
'access_token'=>$this->accessToken
);
$url = self::API_HOST . self::WXAQRCODE_PATH . http_build_query($urlParamArr);
$res = Common::httpRequest($url, json_encode($postParamArr));
$res = Common::httpRequest($url, json_encode($finalParamArr));
return $res;
}

Expand All @@ -159,11 +165,12 @@ public function createWXAQRCode($postParamArr = array())
*/
public function getWXACode($postParamArr = array())
{
$finalParamArr = Common::extendArrayData($this->defaultParamInfo[__FUNCTION__], $postParamArr);
$urlParamArr = array(
'access_token'=>$this->accessToken
);
$url = self::API_HOST . self::WXACODE_PATH . http_build_query($urlParamArr);
$res = Common::httpRequest($url, json_encode($postParamArr));
$res = Common::httpRequest($url, json_encode($finalParamArr));
return $res;
}

Expand All @@ -174,11 +181,12 @@ public function getWXACode($postParamArr = array())
*/
public function getWxacodeUnlimit($postParamArr = array())
{
$finalParamArr = Common::extendArrayData($this->defaultParamInfo[__FUNCTION__], $postParamArr);
$urlParamArr = array(
'access_token'=>$this->accessToken
);
$url = self::API_HOST . self::WXACODE_UNLIMIT_PATH . http_build_query($urlParamArr);
$res = Common::httpRequest($url, json_encode($postParamArr));
$res = Common::httpRequest($url, json_encode($finalParamArr));
return $res;
}
}

0 comments on commit b4dda18

Please sign in to comment.