add clear

This commit is contained in:
as2252258@163.com
2019-07-12 19:25:47 +08:00
parent c4b43deb9f
commit 70eae1dc01
5 changed files with 398 additions and 243 deletions
+9 -2
View File
@@ -5,6 +5,7 @@
* Date: 2018/4/19 0019 * Date: 2018/4/19 0019
* Time: 16:12 * Time: 16:12
*/ */
namespace wchat; namespace wchat;
class Account extends Base class Account extends Base
{ {
@@ -15,12 +16,18 @@ class Account extends Base
*/ */
public function login($code) public function login($code)
{ {
return WxClient::get('sns/jscode2session', [ $param = [
'sns/jscode2session',
[
'appid' => $this->appid, 'appid' => $this->appid,
'secret' => $this->appsecret, 'secret' => $this->appsecret,
'js_code' => $code, 'js_code' => $code,
'grant_type' => 'authorization_code' 'grant_type' => 'authorization_code'
], null, ['Content-Type' => 'text/xml']); ],
null,
['Content-Type' => 'text/xml']
];
return WxClient::get(...$param);
} }
-113
View File
@@ -180,65 +180,6 @@ abstract class Base
return $this->nonce_str = implode($res); return $this->nonce_str = implode($res);
} }
/**
* @param array $data
* @return string
*/
public static function toXml(array $data)
{
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* @return string
*/
public function sign(array $array)
{
ksort($array, SORT_STRING);
$string = '';
foreach ($array as $key => $val) {
if (empty($string)) {
$string = $key . '=' . $val;
} else {
$string .= '&' . $key . '=' . $val;
}
}
$string .= '&key=' . $this->key;
// var_dump($string);
if ($this->sign_type == 'MD5') {
return strtoupper(md5($string));
} else {
return hash('sha256', $string);
}
}
/**
* @param $xml
* @return mixed
*/
public function toArray($xml)
{
if (!is_null($json = json_decode($xml, TRUE))) {
return $json;
}
if (is_array($json)) {
return $json;
}
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return json_decode(json_encode($data), TRUE);
}
/** /**
* @return bool|mixed|string * @return bool|mixed|string
@@ -317,58 +258,4 @@ abstract class Base
return $data; return $data;
} }
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
public static function d($code)
{
$messages = [
static::$OK => '',
static::$IllegalAesKey => '',
static::$IllegalIv => '',
static::$IllegalBuffer => '',
static::$DecodeBase64Error => '',
];
return $messages[$code] ?? static::$DecodeBase64Error;
}
/**
* @param $encryptedData
* @param $iv
* @param $sessionKey
* @param $data
* @param null $appId
* @return int
*/
public static function decode($encryptedData, $iv, $sessionKey, &$data, $appId = null)
{
if (strlen($sessionKey) != 24) {
return self::$IllegalAesKey;
}
flush();
$aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) {
return self::$IllegalIv;
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
if ($result === false) {
return self::$IllegalBuffer;
}
$dataObj = json_decode($result);
if ($dataObj->watermark->appid != $appId) {
return self::$IllegalBuffer;
}
$data = $dataObj;
return self::$OK;
}
} }
+126
View File
@@ -0,0 +1,126 @@
<?php
namespace wchat;
class Help extends Base
{
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
public static function d($code)
{
$messages = [
static::$OK => '',
static::$IllegalAesKey => '',
static::$IllegalIv => '',
static::$IllegalBuffer => '',
static::$DecodeBase64Error => '',
];
return $messages[$code] ?? static::$DecodeBase64Error;
}
/**
* @param $encryptedData
* @param $iv
* @param $sessionKey
* @param $data
* @param null $appId
* @return int
*/
public static function decode($encryptedData, $iv, $sessionKey, &$data, $appId = null)
{
if (strlen($sessionKey) != 24) {
return self::$IllegalAesKey;
}
flush();
$aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) {
return self::$IllegalIv;
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
if ($result === false) {
return self::$IllegalBuffer;
}
$dataObj = json_decode($result);
if ($dataObj->watermark->appid != $appId) {
return self::$IllegalBuffer;
}
$data = $dataObj;
return self::$OK;
}
/**
* @param array $data
* @return string
*/
public static function toXml(array $data)
{
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* @param $xml
* @return mixed
*/
public static function toArray($xml)
{
if (!is_null($json = json_decode($xml, TRUE))) {
return $json;
}
if (is_array($json)) {
return $json;
}
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return json_decode(json_encode($data), TRUE);
}
/**
* @param array $array
* @param $key
* @param $type
* @return string
*/
public static function sign(array $array, $key, $type)
{
ksort($array, SORT_STRING);
$string = '';
foreach ($array as $key => $val) {
if (empty($string)) {
$string = $key . '=' . $val;
} else {
$string .= '&' . $key . '=' . $val;
}
}
$string .= '&key=' . $key;
if ($type == 'MD5') {
return strtoupper(md5($string));
} else {
return hash('sha256', $string);
}
}
}
+43 -20
View File
@@ -27,20 +27,36 @@ class Recharge extends Base
*/ */
public function payment(int $money, string $orderNo, $openId = NULL) public function payment(int $money, string $orderNo, $openId = NULL)
{ {
$_this = $this;
if ($money < 0) { if ($money < 0) {
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']); return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
} }
$this->money = $money; $this->money = $money;
$this->orderNo = $orderNo; $this->orderNo = $orderNo;
$this->data['openid'] = $openId; $this->data['openid'] = $openId;
return WxClient::post($this->createPayUrl(), $this->builder(),
function ($result, $body) use ($_this) { $params = [
$data = $_this->toArray($result); $this->mch_host . '/pay/unifiedorder',
$this->builder(),
[$this, 'payCallback'],
['Content-Type' => 'text/xml']
];
return WxClient::post(...$params);
}
/**
* @param $result
* @param $body
* @return array
*/
public function payCallback($result, $body)
{
$data = Help::toArray($result);
if (isset($data['sign'])) { if (isset($data['sign'])) {
$sign = $data['sign']; $sign = $data['sign'];
unset($data['sign']); unset($data['sign']);
$_sign = $_this->sign($data); $_sign = Help::sign($data, $this->key, $this->sign_type);
} }
$return = []; $return = [];
if (!isset($sign) || $sign != $_sign) { if (!isset($sign) || $sign != $_sign) {
@@ -57,8 +73,6 @@ class Recharge extends Base
} }
} }
return $return; return $return;
}, ['Content-Type' => 'text/xml']
);
} }
@@ -82,14 +96,9 @@ class Recharge extends Base
$data = array_merge($data, $this->data); $data = array_merge($data, $this->data);
$data['sign'] = $this->sign($data); $data['sign'] = Help::sign($data, $this->key, $this->sign_type);
return static::toXml($data); return Help::toXml($data);
}
private function createPayUrl()
{
return $this->mch_host . '/pay/unifiedorder';
} }
/** /**
@@ -113,22 +122,36 @@ class Recharge extends Base
'check_name' => 'NO_CHECK', 'check_name' => 'NO_CHECK',
'amount' => $money * 100, 'amount' => $money * 100,
'spbill_create_ip' => $REMOTE_ADDR, 'spbill_create_ip' => $REMOTE_ADDR,
'desc' => $desc ?? '有大佬给你发红包啦.', 'desc' => $desc ?? '有大佬给你发红包啦 . ',
]; ];
$transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $array['sign'] = Help::sign($array, $this->key, $this->sign_type);
$prams = [
$this->mch_host . '/mmpaymkttransfers/promotion/transfers',
Help::toXml($array),
[$this, 'txCallback'],
NULL,
[$this->ssl_cert, $this->ssl_key]
];
$array['sign'] = $this->sign($array); return WxClient::post(...$prams);
}
return WxClient::post($transfers, static::toXml($array), function ($data) {
$array = $this->toArray($data); /**
* @param $data
* @return Result
* 提现回调
*/
public function txCallback($data)
{
$array = Help::toArray($data);
if ($array['result_code'] != 'SUCCESS') { if ($array['result_code'] != 'SUCCESS') {
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']]; $data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
} else { } else {
$data = ['code' => 0, 'message' => '支付成功']; $data = ['code' => 0, 'message' => '支付成功'];
} }
return new Result($data); return new Result($data);
}, NULL, [$this->ssl_cert, $this->ssl_key]);
} }
} }
+119 -7
View File
@@ -11,20 +11,132 @@ namespace wchat;
class Template extends Base class Template extends Base
{ {
private $keywords = [];
private $templateId = '';
private $formId = '';
private $openId = '';
private $defaultUrl = '';
private $page = 'pages/index/index';
private $emphasis_keyword = '';
/** @var Template $instance */
private static $instance = null;
/** /**
* @param $openId * @return Template
* @param $message */
* @param $form_id public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new Template();
}
return static::$instance;
}
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
* @param string $templateId
*/
public function setTemplateId(string $templateId)
{
$this->templateId = $templateId;
}
/**
* @param string $formId
*/
public function setFormId(string $formId)
{
$this->formId = $formId;
}
/**
* @param string $openId
*/
public function setOpenId(string $openId)
{
$this->openId = $openId;
}
/**
* @param string $defaultUrl
*/
public function setDefaultUrl(string $defaultUrl)
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param string $page
*/
public function setPage(string $page)
{
$this->page = $page;
}
/**
* @param string $emphasis_keyword
*/
public function setEmphasisKeyword(string $emphasis_keyword)
{
$this->emphasis_keyword = $emphasis_keyword;
}
/**
* @param $index
* @param $context
*/
public function replaceKeyword($index, $context)
{
$this->keywords['keyword' . $index] = [
'value' => $context
];
}
/**
* @param $index
* @param $context
*/
public function addKeyword($context)
{
$this->keywords['keyword' . count($this->keywords)] = [
'value' => $context
];
}
/**
* @param string $access
* @return Result * @return Result
* @throws \Exception * @throws \Exception
* *
* 奴隶交易通知 * 奴隶交易通知
*/ */
public function sendTemplate(string $access,array $postBody) public function sendTemplate(string $access)
{ {
$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access; $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access;
$postBody = json_encode($postBody);
return WxClient::post($url, $postBody, NULL, ['content-type' => 'application/json']) $params = [
->append('postBody', $postBody); "touser" => $this->openId,
"template_id" => $this->templateId,
"page" => $this->page,
"form_id" => $this->formId,
"data" => $this->keywords,
];
if (!empty($this->emphasis_keyword)) {
$params['emphasis_keyword'] = $this->emphasis_keyword;
}
$header = ['content-type' => 'application/json'];
return WxClient::post($url, $params, NULL, $header)
->append('postBody', $params);
} }
} }