更新 Base.php

根据本地文件修改
This commit is contained in:
2019-05-21 16:01:05 +08:00
parent a20dd5b086
commit 3e4db2d5a5
+316 -311
View File
@@ -10,361 +10,366 @@ namespace wchat;
abstract class Base abstract class Base
{ {
/** /**
* @var string * @var string
* *
* 小程序ID * 小程序ID
*/ */
public $app_id = ''; public $appid = '';
/** /**
* @var string * @var string
* *
* 商户号ID * 商户号ID
*/ */
public $mch_id = ''; public $mch_id = '';
/** /**
* @var string * @var string
* *
* 设备号 * 设备号
*/ */
public $device_info = 'WEB'; public $device_info = 'WEB';
/** /**
* @var string * @var string
* *
* 随机字符串 * 随机字符串
*/ */
public $nonce_str = ''; public $nonce_str = '';
/** /**
* @var string * @var string
* *
* 商品简单描述 * 商品简单描述
*/ */
public $body = '好友默契Pk充值!'; public $body = '好友默契Pk充值!';
/** /**
* @var string * @var string
* *
* 商户订单号 * 商户订单号
*/ */
public $out_trade_no = ""; public $out_trade_no = "";
/** /**
* @var int * @var int
* *
* 金额 * 金额
*/ */
public $total_fee = 0; public $total_fee = 0;
/** /**
* @var string * @var string
* *
* 终端IP * 终端IP
*/ */
public $spbill_create_ip = ""; public $spbill_create_ip = "";
/** /**
* @var string * @var string
* *
* 异步回调地址 * 异步回调地址
*/ */
public $notify_url = "https://game-slave-trade-api.zhuangb123.com/recharge/notify"; public $notify_url = "https://game-slave-trade-api.zhuangb123.com/recharge/notify";
/** /**
* @var string * @var string
* *
* 交易类型 * 交易类型
*/ */
public $trade_type = 'JSAPI'; public $trade_type = 'JSAPI';
/** /**
* @var string * @var string
* *
* 签名方式 * 签名方式
*/ */
public $sign_type = 'MD5'; public $sign_type = 'MD5';
/** /**
* @var string * @var string
* *
* 商户接口地址 * 商户接口地址
*/ */
public $mch_host = 'https://api.mch.weixin.qq.com'; public $mch_host = 'https://api.mch.weixin.qq.com';
/** /**
* @var string * @var string
*/ */
public $app_secret = ''; public $appsecret = '';
public $ssl_cert = ''; public $ssl_cert = '';
public $ssl_key = ''; public $ssl_key = '';
/** /**
* @var string * @var string
*/ */
public $key = ''; public $key = '';
/** @var static */ /** @var static */
protected static $base; protected static $base;
/** /**
* @param $configPath * @param $configPath
* @return static * @return static
* @throws \Exception * @throws \Exception
*/ */
public static function call($configPath) public static function call($configPath)
{ {
if (!static::$base instanceof Base) { if (!static::$base instanceof Base) {
static::$base = new static(); static::$base = new static();
} }
$class = static::$base; $class = static::$base;
$class->loadConfig($configPath); $class->loadConfig($configPath);
return $class; return $class;
} }
/** /**
* *
*/ */
public function loadConfig($configPath) public function loadConfig($config)
{ {
$config = require realpath($configPath); if (empty($config)) {
if (empty($config)) { return;
return; }
} if (is_string($config)) {
foreach ($config as $key => $val) { $config = require_once $config;
if (!property_exists($this, $key)) { }
continue; foreach ($config as $key => $val) {
} if (!property_exists($this, $key)) {
$this->$key = $val; continue;
} }
} $this->$key = $val;
}
}
/** /**
* @param $url * @param $url
* @param array $data * @param array $data
* @param callable|null $callback * @param callable|null $callback
* @return Result * @return Result
*/ * @throws
public function push($url, $data = [], callable $callback = NULL) */
{ public function push($url, $data = [], callable $callback = NULL)
return Http::post($url, $data, $callback); {
} return Http::post($url, $data, $callback);
}
/** /**
* @param int $length * @param int $length
* @return string * @return string
* *
* 随机字符串 * 随机字符串
*/ */
public function random($length = 20) public function random($length = 20)
{ {
$res = []; $res = [];
$str = 'abcdefghijklmnopqrstuvwxyz'; $str = 'abcdefghijklmnopqrstuvwxyz';
$str .= strtoupper($str) . '1234567890'; $str .= strtoupper($str) . '1234567890';
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
$rand = substr($str, rand(0, strlen($str) - 2), 1); $rand = substr($str, rand(0, strlen($str) - 2), 1);
if (empty($rand)) { if (empty($rand)) {
$rand = substr($str, strlen($str) - 3, 1); $rand = substr($str, strlen($str) - 3, 1);
} }
array_push($res, $rand); array_push($res, $rand);
} }
return $this->nonce_str = implode($res); return $this->nonce_str = implode($res);
} }
/** /**
* @param array $data * @param array $data
* @return string * @return string
*/ */
protected function toXml(array $data) public static function toXml(array $data)
{ {
$xml = "<xml>"; $xml = "<xml>";
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
if (is_numeric($val)) { if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">"; $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else { } else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"; $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
} }
} }
$xml .= "</xml>"; $xml .= "</xml>";
return $xml; return $xml;
} }
/** /**
* @return string * @return string
*/ */
public function sign(array $array) public function sign(array $array)
{ {
ksort($array, SORT_STRING); ksort($array, SORT_STRING);
$string = ''; $string = '';
foreach ($array as $key => $val) { foreach ($array as $key => $val) {
if (empty($string)) { if (empty($string)) {
$string = $key . '=' . $val; $string = $key . '=' . $val;
} else { } else {
$string .= '&' . $key . '=' . $val; $string .= '&' . $key . '=' . $val;
} }
} }
$string .= '&key=' . $this->key; $string .= '&key=' . $this->key;
// var_dump($string); // var_dump($string);
if ($this->sign_type == 'MD5') { if ($this->sign_type == 'MD5') {
return strtoupper(md5($string)); return strtoupper(md5($string));
} else { } else {
return hash('sha256', $string); return hash('sha256', $string);
} }
} }
/** /**
* @param $xml * @param $xml
* @return mixed * @return mixed
*/ */
public function toArray($xml) public function toArray($xml)
{ {
if (!is_null($json = json_decode($xml, TRUE))) { if (!is_null($json = json_decode($xml, TRUE))) {
return $json; return $json;
} }
if (is_array($json)) { if (is_array($json)) {
return $json; return $json;
} }
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return json_decode(json_encode($data), TRUE); return json_decode(json_encode($data), TRUE);
} }
/** /**
* @return bool|mixed|string * @return bool|mixed|string
* @throws \Exception * @throws \Exception
*/ */
protected function getAccessToken() protected function getAccessToken()
{ {
$data = Http::get('https://api.weixin.qq.com/cgi-bin/token', [ $data = Http::get('https://api.weixin.qq.com/cgi-bin/token', [
'grant_type' => 'client_credential', 'grant_type' => 'client_credential',
'appid' => $this->app_id, 'appid' => $this->appid,
'secret' => $this->app_secret, 'secret' => $this->appsecret,
]); ]);
if (!$data->isResultsOK()) { if (!$data->isResultsOK()) {
throw new \Exception($data->getMessage()); throw new \Exception($data->getMessage());
} }
return $data->getData('access_token'); return $data->getData('access_token');
} }
/** /**
* @param $data * @param $data
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
protected function buildResult($data, $body = NULL) protected function buildResult($data, $body = NULL)
{ {
$data = $this->checkSign($data); $data = $this->checkSign($data);
if (!$data) { if (!$data) {
$return['code'] = -1; $return['code'] = -1;
$return['message'] = '签名错误.'; $return['message'] = '签名错误.';
} else { } else {
if (isset($data['return_code'])) { if (isset($data['return_code'])) {
if ($data['return_code'] == 'FAIL') { if ($data['return_code'] == 'FAIL') {
$return['code'] = -1; $return['code'] = -1;
$return['message'] = $data['return_msg']; $return['message'] = $data['return_msg'];
} else { } else {
$return['code'] = 0; $return['code'] = 0;
$return['data'] = $data; $return['data'] = $data;
$return['data']['postBody'] = $body; $return['data']['postBody'] = $body;
} }
} else { } else {
if ($data['errcode'] == 'FAIL') { if ($data['errcode'] == 'FAIL') {
$return['code'] = -1; $return['code'] = -1;
$return['message'] = $data['errmsg']; $return['message'] = $data['errmsg'];
} else { } else {
$return['code'] = 0; $return['code'] = 0;
$return['data'] = $data; $return['data'] = $data;
$return['data']['postBody'] = $body; $return['data']['postBody'] = $body;
} }
} }
} }
return $return; return $return;
} }
/** /**
* @param $result * @param $result
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
protected function checkSign($result) protected function checkSign($result)
{ {
$data = $this->toArray($result); $data = $this->toArray($result);
if (!isset($data['sign'])) { if (!isset($data['sign'])) {
return $data; return $data;
} }
$sign = $data['sign']; $sign = $data['sign'];
unset($data['sign']); unset($data['sign']);
$_sign = $this->sign($data); $_sign = $this->sign($data);
if ($sign != $_sign) { if ($sign != $_sign) {
return FALSE; return FALSE;
} }
return $data; return $data;
} }
public static $OK = 0; public static $OK = 0;
public static $IllegalAesKey = -41001; public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002; public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003; public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004; public static $DecodeBase64Error = -41004;
public static function d($code) public static function d($code)
{ {
$messages = [ $messages = [
static::$OK => '', static::$OK => '',
static::$IllegalAesKey => '', static::$IllegalAesKey => '',
static::$IllegalIv => '', static::$IllegalIv => '',
static::$IllegalBuffer => '', static::$IllegalBuffer => '',
static::$DecodeBase64Error => '', static::$DecodeBase64Error => '',
]; ];
return $messages[$code] ?? static::$DecodeBase64Error; return $messages[$code] ?? static::$DecodeBase64Error;
} }
/** /**
* @param $encryptedData * @param $encryptedData
* @param $iv * @param $iv
* @param $data * @param $data
* @return int * @return int
*/ */
public static function decode($encryptedData, $iv, $sessionKey, &$data) public static function decode($encryptedData, $iv, $sessionKey, &$data, $appId = null)
{ {
if (strlen($sessionKey) != 24) { if (strlen($sessionKey) != 24) {
return self::$IllegalAesKey; return self::$IllegalAesKey;
} }
$aesKey = base64_decode($sessionKey); $aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) { if (strlen($iv) != 24) {
return self::$IllegalIv; return self::$IllegalIv;
} }
$aesIV = base64_decode($iv); $aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData); $aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj = json_decode($result);
$dataObj = json_decode($result); if ($dataObj == NULL) {
return self::$IllegalBuffer;
if ($dataObj == NULL) { }
return self::$IllegalBuffer; if (empty($appId)) {
} $appId = static::$app_id;
if ($dataObj->watermark->appid != static::$app_id) { }
return self::$IllegalBuffer; if ($dataObj->watermark->appid != $appId) {
} return self::$IllegalBuffer;
$data = $dataObj; }
return self::$OK; $data = $dataObj;
} return self::$OK;
}
} }