Compare commits
155 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b92ea7df9f | |||
| 72a7c58bc4 | |||
| e1959752e1 | |||
| 361ad4538f | |||
| 90e05b1892 | |||
| b66d1de244 | |||
| 191624b7e6 | |||
| 88917d55c3 | |||
| 7273fe1ce5 | |||
| ad76b45b45 | |||
| 66db166fc6 | |||
| 5d227c619c | |||
| 4efbdf8631 | |||
| 6ccc1a8c1f | |||
| c412df7252 | |||
| 0569d1d752 | |||
| f5b14a4565 | |||
| 10f3d5b99f | |||
| 18d898a438 | |||
| 61a226bc0f | |||
| 820132e5bc | |||
| b06f893e57 | |||
| 457d0ebd4e | |||
| 50773cefad | |||
| 74960d1df1 | |||
| e8ff51e8be | |||
| cf19d0ea15 | |||
| 58c276eef9 | |||
| 649817e9ef | |||
| fedc0bde24 | |||
| 523ee91797 | |||
| 589b92c152 | |||
| 74847b2a2d | |||
| bdafc011a6 | |||
| 80abb3a20a | |||
| 092e355850 | |||
| 80833aacb8 | |||
| 075dcd3700 | |||
| 6d141c60cf | |||
| 512e62a442 | |||
| ed87844b7d | |||
| 781d754d59 | |||
| 16c728e697 | |||
| eec78ff4a8 | |||
| 9f0d07b224 | |||
| 6d4d6cdfb4 | |||
| 18536c575f | |||
| 49b671afa4 | |||
| b6f7c0b840 | |||
| 59ac8f97bb | |||
| ee82e3b592 | |||
| ae3c9ea077 | |||
| 4b088296b3 | |||
| 80ead6d2bb | |||
| b6668b815d | |||
| f9a37089f7 | |||
| 3ca162fa85 | |||
| ce0255b2c5 | |||
| aec9c53e5a | |||
| 2898481696 | |||
| 8d9c41b9e2 | |||
| 6fd5e530fb | |||
| 41393ef343 | |||
| 1e507f0e6c | |||
| 062f712862 | |||
| 07803dd31a | |||
| 8e1d54413a | |||
| b92fdeee39 | |||
| f174d9450e | |||
| f6b89da482 | |||
| 87dd5dee46 | |||
| 2c808dd27c | |||
| 3f26f94ff4 | |||
| f6b28c0084 | |||
| 4e2d39293e | |||
| da349413c2 | |||
| 943a233724 | |||
| 02cfc264f2 | |||
| f7d401b852 | |||
| 837fc72a10 | |||
| 016885ba16 | |||
| 411bd5f017 | |||
| 63d0639b85 | |||
| 6e488f87b1 | |||
| 8f8161a780 | |||
| 13a56f3555 | |||
| 8319fac4b7 | |||
| b556ebffa2 | |||
| 7e3a959096 | |||
| 650b4d9f19 | |||
| 59e72de410 | |||
| 6377d7e1ee | |||
| e9d9b305eb | |||
| 02997c3476 | |||
| 17da6239f3 | |||
| 30b5ea5d3d | |||
| 0c04c6fc05 | |||
| 48da0a22bf | |||
| 7def314478 | |||
| 98723bcd4f | |||
| 32faedd793 | |||
| 774b16e372 | |||
| dc887b81e3 | |||
| c37a3995e9 | |||
| e3956048b2 | |||
| 2ca01ea46f | |||
| b7e92ba514 | |||
| 409eae7dc4 | |||
| c5468c95fc | |||
| b5a8102b72 | |||
| 465e48ffe4 | |||
| 03df0bfa4e | |||
| 4d24fd7dde | |||
| 401ef6aa18 | |||
| 178a558b6d | |||
| 95849babc7 | |||
| 2f2950e944 | |||
| 2b9bced512 | |||
| 421c6ecf31 | |||
| c0ab8adfcb | |||
| 0d202fce14 | |||
| bf18dd5951 | |||
| 2d322ab0e9 | |||
| 6c94ecc353 | |||
| bba7544942 | |||
| 562f06cc49 | |||
| f8724a8c36 | |||
| f1c06d30e1 | |||
| 5a27218ea7 | |||
| 93579952b9 | |||
| b378d20826 | |||
| 14bea139e9 | |||
| a8840aaae8 | |||
| 65422f37fb | |||
| eaaec85239 | |||
| 83742695c1 | |||
| efdae639b3 | |||
| d980695e0e | |||
| aae5f9cea3 | |||
| acd2583a20 | |||
| 109db2e3c4 | |||
| 7dbb749cce | |||
| b8f240d910 | |||
| aeb7a8c4b5 | |||
| e8c1f12fc8 | |||
| 28a2b0b4f8 | |||
| 7a8c869f6a | |||
| 2e83dbc44f | |||
| 7e58adf4ff | |||
| df40f74560 | |||
| 7329a24577 | |||
| 557c92a343 | |||
| 2969eac00e | |||
| f218e12de3 | |||
| b6544dbb8e |
+4
-5
@@ -12,7 +12,6 @@ class Container
|
|||||||
/** @var Container */
|
/** @var Container */
|
||||||
private static $_instance;
|
private static $_instance;
|
||||||
|
|
||||||
/** @var \common\Config */
|
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
private $container = [];
|
private $container = [];
|
||||||
@@ -38,8 +37,8 @@ class Container
|
|||||||
|
|
||||||
if (static::$_instance->exists($class)) {
|
if (static::$_instance->exists($class)) {
|
||||||
$newInstance = static::$_instance->get($class);
|
$newInstance = static::$_instance->get($class);
|
||||||
if (method_exists($newInstance, 'initConfig')) {
|
if (method_exists($newInstance, 'setConfig')) {
|
||||||
$newInstance->initConfig($config);
|
$newInstance->setConfig($config);
|
||||||
}
|
}
|
||||||
return $newInstance;
|
return $newInstance;
|
||||||
} else {
|
} else {
|
||||||
@@ -71,8 +70,8 @@ class Container
|
|||||||
throw new Exception('Class Con\'t instance.');
|
throw new Exception('Class Con\'t instance.');
|
||||||
}
|
}
|
||||||
$newInstance = $newInstance->newInstance();
|
$newInstance = $newInstance->newInstance();
|
||||||
if (method_exists($newInstance, 'initConfig')) {
|
if (method_exists($newInstance, 'setConfig')) {
|
||||||
$newInstance->initConfig($this->config);
|
$newInstance->setConfig($this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->container[$class] = $newInstance;
|
$this->container[$class] = $newInstance;
|
||||||
|
|||||||
+29
-26
@@ -1,28 +1,31 @@
|
|||||||
{
|
{
|
||||||
"name": "wchat/wchat",
|
"name": "wchat/wchat",
|
||||||
"description": "一个参数效验扩展",
|
"description": "一个参数效验扩展",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"validate"
|
"validate"
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "xianglin",
|
"name": "xianglin",
|
||||||
"email": "xianglin@163.com"
|
"email": "xianglin@163.com"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"wchat\\": "wchat"
|
"wchat\\": "./wchat/"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"Container.php"
|
"Container.php"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">= 7.0"
|
"php": ">= 8.0",
|
||||||
},
|
"game-worker/kiri-client": "~v2.6",
|
||||||
"require-dev": {
|
"game-worker/kiri-container": "~v1.9",
|
||||||
"swoole/ide-helper": "@dev"
|
"psr/container": "*"
|
||||||
}
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"swoole/ide-helper": "@dev"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPSTORM_META {
|
||||||
|
|
||||||
|
// Reflect
|
||||||
|
use Kiri\Di\Container;
|
||||||
|
use Kiri\Di\ContainerInterface;
|
||||||
|
use wchat\wx\QqFactory;
|
||||||
|
use wchat\wx\WxFactory;
|
||||||
|
|
||||||
|
override(ContainerInterface::get(0), map('@'));
|
||||||
|
override(Container::get(0), map('@'));
|
||||||
|
override(WxFactory::get(0), map('@'));
|
||||||
|
override(QqFactory::get(0), map('@'));
|
||||||
|
override(Container::make(0), map('@'));
|
||||||
|
override(Container::create(0), map('@'));
|
||||||
|
// override(\Hyperf\Utils\Context::get(0), map('@'));
|
||||||
|
// override(\make(0), map('@'));
|
||||||
|
override(\di(0), map('@'));
|
||||||
|
override(\duplicate(0), map('@'));
|
||||||
|
|
||||||
|
}
|
||||||
+81
-31
@@ -4,22 +4,25 @@
|
|||||||
namespace wchat\base;
|
namespace wchat\base;
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\Miniprogarampage;
|
use Kiri\Client;
|
||||||
|
use wchat\common\Multiprogramming;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Subject
|
* Class Subject
|
||||||
* @package wchat\base
|
* @package wchat\base
|
||||||
*/
|
*/
|
||||||
abstract class Subject extends Miniprogarampage
|
abstract class Subject extends Multiprogramming
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private $keywords = [];
|
private array $keywords = [];
|
||||||
private $templateId = '';
|
private string $templateId = '';
|
||||||
private $openId = '';
|
private string $openId = '';
|
||||||
private $page = 'pages/index/index';
|
private string $page = 'pages/index/index';
|
||||||
private $emphasis_keyword = '';
|
private string $emphasis_keyword = '';
|
||||||
|
private string $oac_appid = '';
|
||||||
|
private string $use_robot = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $keywords
|
* @param array $keywords
|
||||||
@@ -30,33 +33,33 @@ abstract class Subject extends Miniprogarampage
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $templateId
|
* @param $templateId
|
||||||
*/
|
*/
|
||||||
public function setTemplateId(string $templateId)
|
public function setTemplateId($templateId)
|
||||||
{
|
{
|
||||||
$this->templateId = $templateId;
|
$this->templateId = $templateId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $openId
|
* @param $openId
|
||||||
*/
|
*/
|
||||||
public function setOpenId(string $openId)
|
public function setOpenId($openId)
|
||||||
{
|
{
|
||||||
$this->openId = $openId;
|
$this->openId = $openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $page
|
* @param $page
|
||||||
*/
|
*/
|
||||||
public function setPage(string $page)
|
public function setPage($page)
|
||||||
{
|
{
|
||||||
$this->page = $page;
|
$this->page = $page;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $emphasis_keyword
|
* @param $emphasis_keyword
|
||||||
*/
|
*/
|
||||||
public function setEmphasisKeyword(string $emphasis_keyword)
|
public function setEmphasisKeyword($emphasis_keyword)
|
||||||
{
|
{
|
||||||
$this->emphasis_keyword = $emphasis_keyword;
|
$this->emphasis_keyword = $emphasis_keyword;
|
||||||
}
|
}
|
||||||
@@ -64,9 +67,9 @@ abstract class Subject extends Miniprogarampage
|
|||||||
/**
|
/**
|
||||||
* @param $index
|
* @param $index
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param $color
|
* @param string $color
|
||||||
*/
|
*/
|
||||||
public function replaceKeyword($index, $context, $color = '')
|
public function replaceKeyword($index, $context, string $color = '')
|
||||||
{
|
{
|
||||||
if (empty($color)) {
|
if (empty($color)) {
|
||||||
$color = '#000';
|
$color = '#000';
|
||||||
@@ -93,37 +96,84 @@ abstract class Subject extends Miniprogarampage
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function getUrl(): string;
|
abstract public function getUrl();
|
||||||
|
|
||||||
|
abstract public function getHost();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getOacAppid(): string
|
||||||
|
{
|
||||||
|
return $this->oac_appid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $oac_appid
|
||||||
|
*/
|
||||||
|
public function setOacAppid(string $oac_appid): void
|
||||||
|
{
|
||||||
|
$this->oac_appid = $oac_appid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUseRobot(): string
|
||||||
|
{
|
||||||
|
return $this->use_robot;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $use_robot
|
||||||
|
*/
|
||||||
|
public function setUseRobot(string $use_robot): void
|
||||||
|
{
|
||||||
|
$this->use_robot = $use_robot;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function sendTemplate()
|
public function sendTemplate(): Result
|
||||||
{
|
{
|
||||||
$url = $this->getUrl() . '?access_token=' . $this->config->getAccessToken();
|
$access_token = $this->config->getAccessToken();
|
||||||
|
if (empty($access_token)) {
|
||||||
|
throw new \Exception('request access_token con\'t null.');
|
||||||
|
}
|
||||||
$params = [
|
$params = [
|
||||||
"touser" => $this->openId,
|
"touser" => $this->openId,
|
||||||
"template_id" => $this->templateId,
|
"template_id" => $this->templateId,
|
||||||
"page" => $this->page,
|
"page" => $this->page,
|
||||||
"data" => $this->keywords,
|
"data" => $this->keywords,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!empty($this->emphasis_keyword)) {
|
if (!empty($this->emphasis_keyword)) {
|
||||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||||
}
|
}
|
||||||
|
if (!empty($this->oac_appid)) {
|
||||||
|
$params['oac_appid'] = $this->oac_appid;
|
||||||
|
}
|
||||||
|
if (!empty($this->use_robot)) {
|
||||||
|
$params['use_robot'] = $this->use_robot;
|
||||||
|
}
|
||||||
|
$this->reset($result);
|
||||||
|
|
||||||
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
|
$client = new Client($this->getHost(), 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
|
||||||
|
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
$this->request->setIsSSL(true);
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
$this->request->addHeader('Content-Type', 'application/json');
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
$this->request->setErrorField('errcode');
|
$body = json_decode($client->getBody(), true);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
$result = $this->request->post($url, $params);
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
$result->append('postBody', $params);
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
return $this->reset($result);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+41
-34
@@ -2,22 +2,23 @@
|
|||||||
|
|
||||||
namespace wchat\base;
|
namespace wchat\base;
|
||||||
|
|
||||||
use wchat\common\Miniprogarampage;
|
use Kiri\Client;
|
||||||
|
use wchat\common\Multiprogramming;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Template
|
* Class Template
|
||||||
*/
|
*/
|
||||||
abstract class Template extends Miniprogarampage
|
abstract class Template extends Multiprogramming
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private $keywords = [];
|
private array $keywords = [];
|
||||||
private $templateId = '';
|
private string $templateId = '';
|
||||||
private $formId = '';
|
private string $formId = '';
|
||||||
private $openId = '';
|
private string $openId = '';
|
||||||
private $page = 'pages/index/index';
|
private string $page = 'pages/index/index';
|
||||||
private $emphasis_keyword = '';
|
private string $emphasis_keyword = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $keywords
|
* @param array $keywords
|
||||||
@@ -28,41 +29,41 @@ abstract class Template extends Miniprogarampage
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $templateId
|
* @param $templateId
|
||||||
*/
|
*/
|
||||||
public function setTemplateId(string $templateId)
|
public function setTemplateId($templateId)
|
||||||
{
|
{
|
||||||
$this->templateId = $templateId;
|
$this->templateId = $templateId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $formId
|
* @param $formId
|
||||||
*/
|
*/
|
||||||
public function setFormId(string $formId)
|
public function setFormId($formId)
|
||||||
{
|
{
|
||||||
$this->formId = $formId;
|
$this->formId = $formId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $openId
|
* @param $openId
|
||||||
*/
|
*/
|
||||||
public function setOpenId(string $openId)
|
public function setOpenId($openId)
|
||||||
{
|
{
|
||||||
$this->openId = $openId;
|
$this->openId = $openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $page
|
* @param $page
|
||||||
*/
|
*/
|
||||||
public function setPage(string $page)
|
public function setPage($page)
|
||||||
{
|
{
|
||||||
$this->page = $page;
|
$this->page = $page;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $emphasis_keyword
|
* @param $emphasis_keyword
|
||||||
*/
|
*/
|
||||||
public function setEmphasisKeyword(string $emphasis_keyword)
|
public function setEmphasisKeyword($emphasis_keyword)
|
||||||
{
|
{
|
||||||
$this->emphasis_keyword = $emphasis_keyword;
|
$this->emphasis_keyword = $emphasis_keyword;
|
||||||
}
|
}
|
||||||
@@ -72,7 +73,7 @@ abstract class Template extends Miniprogarampage
|
|||||||
* @param $context
|
* @param $context
|
||||||
* @param $color
|
* @param $color
|
||||||
*/
|
*/
|
||||||
public function replaceKeyword($index, $context, $color = '')
|
public function replaceKeyword($index, $context, string $color = '')
|
||||||
{
|
{
|
||||||
if (empty($color)) {
|
if (empty($color)) {
|
||||||
$color = '#000';
|
$color = '#000';
|
||||||
@@ -99,7 +100,9 @@ abstract class Template extends Miniprogarampage
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function getUrl(): string;
|
abstract public function getUrl();
|
||||||
|
|
||||||
|
abstract public function getHost();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Result
|
* @return Result
|
||||||
@@ -107,14 +110,13 @@ abstract class Template extends Miniprogarampage
|
|||||||
*
|
*
|
||||||
* 奴隶交易通知
|
* 奴隶交易通知
|
||||||
*/
|
*/
|
||||||
public function sendTemplate()
|
public function sendTemplate(): Result
|
||||||
{
|
{
|
||||||
$access_token = $this->config->getAccessToken();
|
$access_token = $this->config->getAccessToken();
|
||||||
if (empty($access_token)) {
|
if (empty($access_token)) {
|
||||||
throw new \Exception('request access_token con\'t null.');
|
throw new \Exception('request access_token con\'t null.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = $this->getUrl() . '?access_token=' . $access_token;
|
|
||||||
$params = [
|
$params = [
|
||||||
"touser" => $this->openId,
|
"touser" => $this->openId,
|
||||||
"template_id" => $this->templateId,
|
"template_id" => $this->templateId,
|
||||||
@@ -126,30 +128,35 @@ abstract class Template extends Miniprogarampage
|
|||||||
if (!empty($this->emphasis_keyword)) {
|
if (!empty($this->emphasis_keyword)) {
|
||||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||||
}
|
}
|
||||||
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
|
|
||||||
$this->request->setIsSSL(true);
|
|
||||||
$this->request->addHeader('Content-Type', 'application/json');
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
$result = $this->request->post($url, $params);
|
|
||||||
$result->append('postBody', $params);
|
|
||||||
|
|
||||||
return $this->reset($result);
|
$this->reset($result);
|
||||||
|
|
||||||
|
$client = new Client($this->getHost(), 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json; charset=utf-8']);
|
||||||
|
$client->post($this->getUrl() . '?access_token=' . $access_token, $params);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
}
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $result
|
* @param $result
|
||||||
* @return mixed
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function reset($result)
|
private function reset($result): void
|
||||||
{
|
{
|
||||||
$this->openId = '';
|
$this->openId = '';
|
||||||
$this->keywords = [];
|
$this->keywords = [];
|
||||||
$this->formId = '';
|
$this->formId = '';
|
||||||
$this->templateId = '';
|
$this->templateId = '';
|
||||||
$this->page = '';
|
$this->page = '';
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+379
-346
@@ -12,7 +12,7 @@ class Config
|
|||||||
*
|
*
|
||||||
* 小程序ID
|
* 小程序ID
|
||||||
*/
|
*/
|
||||||
private $appid = '';
|
private string $appid = '';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,100 +20,425 @@ class Config
|
|||||||
*
|
*
|
||||||
* 商户号ID
|
* 商户号ID
|
||||||
*/
|
*/
|
||||||
private $mch_id = '';
|
private string $mch_id = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 设备号
|
* 设备号
|
||||||
*/
|
*/
|
||||||
private $device_info = 'WEB';
|
private string $device_info = 'WEB';
|
||||||
|
|
||||||
|
private string $token;
|
||||||
|
private string $encodingAesKey;
|
||||||
|
|
||||||
|
private string $schema = 'WECHATPAY2-SHA256-RSA2048';
|
||||||
|
|
||||||
|
private string $serial_no = '33F00000E4248E2EB3C61FC8384E1E155ECBB1B2';
|
||||||
|
|
||||||
private $token;
|
|
||||||
private $encodingAesKey;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 随机字符串
|
* 随机字符串
|
||||||
*/
|
*/
|
||||||
private $nonce_str = '';
|
private string $nonce_str = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 商品简单描述
|
* 商品简单描述
|
||||||
*/
|
*/
|
||||||
private $body = '好友默契Pk充值!';
|
private string $body = '好友默契Pk充值!';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 商户订单号
|
* 商户订单号
|
||||||
*/
|
*/
|
||||||
private $out_trade_no = "";
|
private string $out_trade_no = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int|float
|
||||||
*
|
*
|
||||||
* 金额
|
* 金额
|
||||||
*/
|
*/
|
||||||
private $total_fee = 0;
|
private float|int $total_fee = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 终端IP
|
* 终端IP
|
||||||
*/
|
*/
|
||||||
private $spbill_create_ip = "";
|
private string $spbill_create_ip = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 异步回调地址
|
* 异步回调地址
|
||||||
*/
|
*/
|
||||||
private $notify_url = "";
|
private string $notify_url = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 交易类型
|
* 交易类型
|
||||||
*/
|
*/
|
||||||
private $trade_type = 'JSAPI';
|
private string $trade_type = 'JSAPI';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 签名方式
|
* 签名方式
|
||||||
*/
|
*/
|
||||||
private $sign_type = 'MD5';
|
private string $sign_type = 'MD5';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
* 商户接口地址
|
* 商户接口地址
|
||||||
*/
|
*/
|
||||||
private $mch_host = 'https://api.mch.weixin.qq.com';
|
private string $mch_host = 'https://api.mch.weixin.qq.com';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $appsecret = '';
|
private string $appsecret = '';
|
||||||
|
|
||||||
private $remote_addr = '127.0.0.1';
|
private string $remote_addr = '127.0.0.1';
|
||||||
|
|
||||||
private $ssl_cert = '';
|
private string $ssl_cert = '';
|
||||||
private $ssl_key = '';
|
private string $ssl_key = '';
|
||||||
private $ssl_ca = '';
|
private string $ssl_ca = '';
|
||||||
private $port = '';
|
private int $port = 443;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $key = '';
|
private string $key = '';
|
||||||
private $access_token = '';
|
private string $access_token = '';
|
||||||
private $agent = '';
|
private string $agent = '';
|
||||||
private $usrSwoole = false;
|
private bool $usrSwoole = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSchema(): string
|
||||||
|
{
|
||||||
|
return $this->schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $schema
|
||||||
|
*/
|
||||||
|
public function setSchema(string $schema): void
|
||||||
|
{
|
||||||
|
$this->schema = $schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSerialNo(): string
|
||||||
|
{
|
||||||
|
return $this->serial_no;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $serial_no
|
||||||
|
*/
|
||||||
|
public function setSerialNo(string $serial_no): void
|
||||||
|
{
|
||||||
|
$this->serial_no = $serial_no;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAppid(): string
|
||||||
|
{
|
||||||
|
return $this->appid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $appid
|
||||||
|
*/
|
||||||
|
public function setAppid(string $appid): void
|
||||||
|
{
|
||||||
|
$this->appid = $appid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMchId(): string
|
||||||
|
{
|
||||||
|
return $this->mch_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $mch_id
|
||||||
|
*/
|
||||||
|
public function setMchId(string $mch_id): void
|
||||||
|
{
|
||||||
|
$this->mch_id = $mch_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getDeviceInfo(): string
|
||||||
|
{
|
||||||
|
return $this->device_info;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $device_info
|
||||||
|
*/
|
||||||
|
public function setDeviceInfo(string $device_info): void
|
||||||
|
{
|
||||||
|
$this->device_info = $device_info;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getToken(): string
|
||||||
|
{
|
||||||
|
return $this->token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $token
|
||||||
|
*/
|
||||||
|
public function setToken(string $token): void
|
||||||
|
{
|
||||||
|
$this->token = $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getEncodingAesKey(): string
|
||||||
|
{
|
||||||
|
return $this->encodingAesKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $encodingAesKey
|
||||||
|
*/
|
||||||
|
public function setEncodingAesKey(string $encodingAesKey): void
|
||||||
|
{
|
||||||
|
$this->encodingAesKey = $encodingAesKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNonceStr(): string
|
||||||
|
{
|
||||||
|
return $this->nonce_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $nonce_str
|
||||||
|
*/
|
||||||
|
public function setNonceStr(string $nonce_str): void
|
||||||
|
{
|
||||||
|
$this->nonce_str = $nonce_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBody(): string
|
||||||
|
{
|
||||||
|
return $this->body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $body
|
||||||
|
*/
|
||||||
|
public function setBody(string $body): void
|
||||||
|
{
|
||||||
|
$this->body = $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getOutTradeNo(): string
|
||||||
|
{
|
||||||
|
return $this->out_trade_no;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $out_trade_no
|
||||||
|
*/
|
||||||
|
public function setOutTradeNo(string $out_trade_no): void
|
||||||
|
{
|
||||||
|
$this->out_trade_no = $out_trade_no;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getTotalFee(): int|string
|
||||||
|
{
|
||||||
|
return $this->total_fee;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $total_fee
|
||||||
|
*/
|
||||||
|
public function setTotalFee(int|string $total_fee): void
|
||||||
|
{
|
||||||
|
$this->total_fee = $total_fee;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSpbillCreateIp(): string
|
||||||
|
{
|
||||||
|
return $this->spbill_create_ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $spbill_create_ip
|
||||||
|
*/
|
||||||
|
public function setSpbillCreateIp(string $spbill_create_ip): void
|
||||||
|
{
|
||||||
|
$this->spbill_create_ip = $spbill_create_ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNotifyUrl(): string
|
||||||
|
{
|
||||||
|
return $this->notify_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $notify_url
|
||||||
|
*/
|
||||||
|
public function setNotifyUrl(string $notify_url): void
|
||||||
|
{
|
||||||
|
$this->notify_url = $notify_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTradeType(): string
|
||||||
|
{
|
||||||
|
return $this->trade_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $trade_type
|
||||||
|
*/
|
||||||
|
public function setTradeType(string $trade_type): void
|
||||||
|
{
|
||||||
|
$this->trade_type = $trade_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSignType(): string
|
||||||
|
{
|
||||||
|
return $this->sign_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sign_type
|
||||||
|
*/
|
||||||
|
public function setSignType(string $sign_type): void
|
||||||
|
{
|
||||||
|
$this->sign_type = $sign_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMchHost(): string
|
||||||
|
{
|
||||||
|
return $this->mch_host;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $mch_host
|
||||||
|
*/
|
||||||
|
public function setMchHost(string $mch_host): void
|
||||||
|
{
|
||||||
|
$this->mch_host = $mch_host;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAppsecret(): string
|
||||||
|
{
|
||||||
|
return $this->appsecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $appsecret
|
||||||
|
*/
|
||||||
|
public function setAppsecret(string $appsecret): void
|
||||||
|
{
|
||||||
|
$this->appsecret = $appsecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getRemoteAddr(): string
|
||||||
|
{
|
||||||
|
return $this->remote_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $remote_addr
|
||||||
|
*/
|
||||||
|
public function setRemoteAddr(string $remote_addr): void
|
||||||
|
{
|
||||||
|
$this->remote_addr = $remote_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslCert(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_cert;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_cert
|
||||||
|
*/
|
||||||
|
public function setSslCert(string $ssl_cert): void
|
||||||
|
{
|
||||||
|
$this->ssl_cert = $ssl_cert;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslKey(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_key
|
||||||
|
*/
|
||||||
|
public function setSslKey(string $ssl_key): void
|
||||||
|
{
|
||||||
|
$this->ssl_key = $ssl_key;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
@@ -125,12 +450,10 @@ class Config
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $ssl_ca
|
* @param string $ssl_ca
|
||||||
* @return Config
|
|
||||||
*/
|
*/
|
||||||
public function setSslCa(string $ssl_ca): Config
|
public function setSslCa(string $ssl_ca): void
|
||||||
{
|
{
|
||||||
$this->ssl_ca = $ssl_ca;
|
$this->ssl_ca = $ssl_ca;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -143,84 +466,26 @@ class Config
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $port
|
* @param string $port
|
||||||
* @return Config
|
|
||||||
*/
|
*/
|
||||||
public function setPort(string $port): Config
|
public function setPort(string $port): void
|
||||||
{
|
{
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isUsrSwoole(): bool
|
|
||||||
{
|
|
||||||
return $this->usrSwoole;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $usrSwoole
|
|
||||||
* @return Config
|
|
||||||
*/
|
|
||||||
public function setUsrSwoole(bool $usrSwoole): Config
|
|
||||||
{
|
|
||||||
$this->usrSwoole = $usrSwoole;
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAgent(): string
|
public function getKey(): string
|
||||||
{
|
{
|
||||||
return $this->agent;
|
return $this->key;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $agent
|
* @param string $key
|
||||||
* @return Config
|
|
||||||
*/
|
*/
|
||||||
public function setAgent(string $agent): Config
|
public function setKey(string $key): void
|
||||||
{
|
{
|
||||||
$this->agent = $agent;
|
$this->key = $key;
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getToken()
|
|
||||||
{
|
|
||||||
return $this->token;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $token
|
|
||||||
* @return Config
|
|
||||||
*/
|
|
||||||
public function setToken($token)
|
|
||||||
{
|
|
||||||
$this->token = $token;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getEncodingAesKey()
|
|
||||||
{
|
|
||||||
return $this->encodingAesKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $encodingAesKey
|
|
||||||
* @return Config
|
|
||||||
*/
|
|
||||||
public function setEncodingAesKey($encodingAesKey)
|
|
||||||
{
|
|
||||||
$this->encodingAesKey = $encodingAesKey;
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -234,289 +499,57 @@ class Config
|
|||||||
/**
|
/**
|
||||||
* @param string $access_token
|
* @param string $access_token
|
||||||
*/
|
*/
|
||||||
public function setAccessToken(string $access_token)
|
public function setAccessToken(string $access_token): void
|
||||||
{
|
{
|
||||||
$this->access_token = $access_token;
|
$this->access_token = $access_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $remote_addr
|
|
||||||
*/
|
|
||||||
public function setRemoteAddr(string $remote_addr)
|
|
||||||
{
|
|
||||||
$this->remote_addr = $remote_addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $appid
|
|
||||||
*/
|
|
||||||
public function setAppid(string $appid)
|
|
||||||
{
|
|
||||||
$this->appid = $appid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $mch_id
|
|
||||||
*/
|
|
||||||
public function setMchId(string $mch_id)
|
|
||||||
{
|
|
||||||
$this->mch_id = $mch_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $device_info
|
|
||||||
*/
|
|
||||||
public function setDeviceInfo(string $device_info)
|
|
||||||
{
|
|
||||||
$this->device_info = $device_info;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $nonce_str
|
|
||||||
*/
|
|
||||||
public function setNonceStr(string $nonce_str)
|
|
||||||
{
|
|
||||||
$this->nonce_str = $nonce_str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $body
|
|
||||||
*/
|
|
||||||
public function setBody(string $body)
|
|
||||||
{
|
|
||||||
$this->body = $body;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $out_trade_no
|
|
||||||
*/
|
|
||||||
public function setOutTradeNo(string $out_trade_no)
|
|
||||||
{
|
|
||||||
$this->out_trade_no = $out_trade_no;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $total_fee
|
|
||||||
*/
|
|
||||||
public function setTotalFee(int $total_fee)
|
|
||||||
{
|
|
||||||
$this->total_fee = $total_fee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $spbill_create_ip
|
|
||||||
*/
|
|
||||||
public function setSpbillCreateIp(string $spbill_create_ip)
|
|
||||||
{
|
|
||||||
$this->spbill_create_ip = $spbill_create_ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $notify_url
|
|
||||||
*/
|
|
||||||
public function setNotifyUrl(string $notify_url)
|
|
||||||
{
|
|
||||||
$this->notify_url = $notify_url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $trade_type
|
|
||||||
*/
|
|
||||||
public function setTradeType(string $trade_type)
|
|
||||||
{
|
|
||||||
$this->trade_type = $trade_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sign_type
|
|
||||||
*/
|
|
||||||
public function setSignType(string $sign_type)
|
|
||||||
{
|
|
||||||
$this->sign_type = $sign_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $mch_host
|
|
||||||
*/
|
|
||||||
public function setMchHost(string $mch_host)
|
|
||||||
{
|
|
||||||
$this->mch_host = $mch_host;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $appsecret
|
|
||||||
*/
|
|
||||||
public function setAppsecret(string $appsecret)
|
|
||||||
{
|
|
||||||
$this->appsecret = $appsecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_cert
|
|
||||||
*/
|
|
||||||
public function setSslCert(string $ssl_cert)
|
|
||||||
{
|
|
||||||
$this->ssl_cert = $ssl_cert;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_key
|
|
||||||
*/
|
|
||||||
public function setSslKey(string $ssl_key)
|
|
||||||
{
|
|
||||||
$this->ssl_key = $ssl_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
*/
|
|
||||||
public function setKey(string $key)
|
|
||||||
{
|
|
||||||
$this->key = $key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAppid(): string
|
public function getAgent(): string
|
||||||
{
|
{
|
||||||
return $this->appid;
|
return $this->agent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param string $agent
|
||||||
*/
|
*/
|
||||||
public function getMchId(): string
|
public function setAgent(string $agent): void
|
||||||
{
|
{
|
||||||
return $this->mch_id;
|
$this->agent = $agent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getDeviceInfo(): string
|
public function isUsrSwoole(): bool
|
||||||
{
|
{
|
||||||
return $this->device_info;
|
return $this->usrSwoole;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param bool $usrSwoole
|
||||||
*/
|
*/
|
||||||
public function getNonceStr(): string
|
public function setUsrSwoole(bool $usrSwoole): void
|
||||||
{
|
{
|
||||||
return $this->nonce_str;
|
$this->usrSwoole = $usrSwoole;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getBody(): string
|
|
||||||
{
|
|
||||||
return $this->body;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getOutTradeNo(): string
|
|
||||||
{
|
|
||||||
return $this->out_trade_no;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getTotalFee(): int
|
|
||||||
{
|
|
||||||
return $this->total_fee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSpbillCreateIp(): string
|
|
||||||
{
|
|
||||||
return $this->spbill_create_ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getNotifyUrl(): string
|
|
||||||
{
|
|
||||||
return $this->notify_url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getTradeType(): string
|
|
||||||
{
|
|
||||||
return $this->trade_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSignType(): string
|
|
||||||
{
|
|
||||||
return $this->sign_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getMchHost(): string
|
|
||||||
{
|
|
||||||
return $this->mch_host;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAppsecret(): string
|
|
||||||
{
|
|
||||||
return $this->appsecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getRemoteAddr(): string
|
|
||||||
{
|
|
||||||
return $this->remote_addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSslCert(): string
|
|
||||||
{
|
|
||||||
return $this->ssl_cert;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSslKey(): string
|
|
||||||
{
|
|
||||||
return $this->ssl_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getKey(): string
|
|
||||||
{
|
|
||||||
return $this->key;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param array $configs
|
||||||
|
*/
|
||||||
|
public function __construct(array $configs)
|
||||||
|
{
|
||||||
|
$this->setConfigs($configs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* @param array $configs
|
* @param array $configs
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setConfigs(array $configs)
|
protected function setConfigs(array $configs): static
|
||||||
{
|
{
|
||||||
if (empty($configs)) {
|
if (empty($configs)) {
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
+51
-33
@@ -7,56 +7,80 @@ namespace wchat\common;
|
|||||||
class Decode
|
class Decode
|
||||||
{
|
{
|
||||||
|
|
||||||
private $sessionKey;
|
private string $sessionKey;
|
||||||
private $iv;
|
private string $iv;
|
||||||
private $encryptedData;
|
private string $encryptedData;
|
||||||
private $appId;
|
private string $appId;
|
||||||
|
|
||||||
|
|
||||||
private $OK = 0;
|
private int $OK = 0;
|
||||||
private $IllegalAesKey = -41001;
|
private int $IllegalAesKey = -41001;
|
||||||
private $IllegalIv = -41002;
|
private int $IllegalIv = -41002;
|
||||||
private $IllegalBuffer = -41003;
|
private int $IllegalBuffer = -41003;
|
||||||
private $DecodeBase64Error = -41004;
|
private int $DecodeBase64Error = -41004;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $sessionKey
|
* @return string
|
||||||
* @return Decode
|
|
||||||
*/
|
*/
|
||||||
public function setSessionKey($sessionKey)
|
public function getSessionKey(): string
|
||||||
|
{
|
||||||
|
return $this->sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sessionKey
|
||||||
|
*/
|
||||||
|
public function setSessionKey(string $sessionKey): void
|
||||||
{
|
{
|
||||||
$this->sessionKey = $sessionKey;
|
$this->sessionKey = $sessionKey;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $iv
|
* @return string
|
||||||
* @return Decode
|
|
||||||
*/
|
*/
|
||||||
public function setIv($iv)
|
public function getIv(): string
|
||||||
|
{
|
||||||
|
return $this->iv;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $iv
|
||||||
|
*/
|
||||||
|
public function setIv(string $iv): void
|
||||||
{
|
{
|
||||||
$this->iv = $iv;
|
$this->iv = $iv;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $encryptedData
|
* @return string
|
||||||
* @return Decode
|
|
||||||
*/
|
*/
|
||||||
public function setEncryptedData($encryptedData)
|
public function getEncryptedData(): string
|
||||||
|
{
|
||||||
|
return $this->encryptedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $encryptedData
|
||||||
|
*/
|
||||||
|
public function setEncryptedData(string $encryptedData): void
|
||||||
{
|
{
|
||||||
$this->encryptedData = $encryptedData;
|
$this->encryptedData = $encryptedData;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $appId
|
* @return string
|
||||||
* @return Decode
|
|
||||||
*/
|
*/
|
||||||
public function setAppId($appId)
|
public function getAppId(): string
|
||||||
|
{
|
||||||
|
return $this->appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $appId
|
||||||
|
*/
|
||||||
|
public function setAppId(string $appId): void
|
||||||
{
|
{
|
||||||
$this->appId = $appId;
|
$this->appId = $appId;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -65,7 +89,7 @@ class Decode
|
|||||||
* @return array|mixed
|
* @return array|mixed
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function decode($asArray)
|
public function decode($asArray): object|array
|
||||||
{
|
{
|
||||||
if (strlen($this->sessionKey) != 24) {
|
if (strlen($this->sessionKey) != 24) {
|
||||||
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
||||||
@@ -87,13 +111,7 @@ class Decode
|
|||||||
if ($dataObj->watermark->appid != $this->appId) {
|
if ($dataObj->watermark->appid != $this->appId) {
|
||||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||||
}
|
}
|
||||||
|
return $asArray ? get_object_vars($dataObj) : $dataObj;
|
||||||
if ($asArray) {
|
|
||||||
return get_object_vars($dataObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $dataObj;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-54
@@ -4,17 +4,21 @@
|
|||||||
namespace wchat\common;
|
namespace wchat\common;
|
||||||
|
|
||||||
|
|
||||||
class Help extends Miniprogarampage
|
class Help extends Multiprogramming
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function toXml(array $data)
|
public static function toXml(array $data): string
|
||||||
{
|
{
|
||||||
$xml = "<xml>";
|
$xml = "<xml>";
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
|
if (is_array($val)) {
|
||||||
|
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (is_numeric($val)) {
|
if (is_numeric($val)) {
|
||||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||||
} else {
|
} else {
|
||||||
@@ -27,65 +31,69 @@ class Help extends Miniprogarampage
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $xml
|
* @param array $array
|
||||||
* @return mixed
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function toArray($xml)
|
private static function xmlChild(array $array): string
|
||||||
{
|
{
|
||||||
if (is_array($xml)) {
|
$string = '';
|
||||||
return $xml;
|
foreach ($array as $key => $value) {
|
||||||
}
|
if (is_array($value)) {
|
||||||
$matchQoute = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
|
$string .= static::xmlChild($value);
|
||||||
if (!preg_match($matchQoute, $xml)) {
|
continue;
|
||||||
return self::jsonToArray($xml);
|
}
|
||||||
}
|
|
||||||
try {
|
if (is_numeric($value)) {
|
||||||
$data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
$string .= "<" . $key . ">" . $value . "</" . $key . ">";
|
||||||
if ($data !== false) {
|
} else {
|
||||||
$data = json_decode(json_encode($data), TRUE);
|
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
|
||||||
} else {
|
|
||||||
$data = $xml;
|
|
||||||
}
|
}
|
||||||
} catch (\Exception $exception) {
|
|
||||||
$data = $xml;
|
|
||||||
} finally {
|
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function jsonToArray($xml)
|
|
||||||
{
|
|
||||||
$_xml = json_decode($xml, true);
|
|
||||||
if (is_null($_xml)) {
|
|
||||||
return $xml;
|
|
||||||
}
|
|
||||||
return $_xml;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $xml
|
* @param $xml
|
||||||
* @return mixed
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
public static function xmlToArray($xml)
|
public static function toArray($xml): ?array
|
||||||
{
|
{
|
||||||
if (is_array($xml)) {
|
if (is_array($xml)) return $xml;
|
||||||
return $xml;
|
if (str_starts_with('<', $xml) && str_ends_with('>', $xml)) {
|
||||||
|
return self::xmlToArray($xml);
|
||||||
|
} else {
|
||||||
|
return self::jsonToArray($xml);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $xml
|
||||||
|
* @return array|null
|
||||||
|
*/
|
||||||
|
public static function jsonToArray(string $xml): ?array
|
||||||
|
{
|
||||||
|
return json_decode($xml, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $xml
|
||||||
|
* @return array|null
|
||||||
|
*/
|
||||||
|
public static function xmlToArray(string $xml): ?array
|
||||||
|
{
|
||||||
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
|
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
|
||||||
return json_decode(json_encode($data), TRUE);
|
return json_decode(json_encode($data), TRUE);
|
||||||
}
|
}
|
||||||
if (!is_null($json = json_decode($xml, TRUE))) {
|
return null;
|
||||||
return $json;
|
|
||||||
}
|
|
||||||
return $xml;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $json
|
* @param mixed $json
|
||||||
* @return false|mixed|string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function toJson($json)
|
public static function toJson(mixed $json): string
|
||||||
{
|
{
|
||||||
if (is_object($json)) {
|
if (is_object($json)) {
|
||||||
$json = get_object_vars($json);
|
$json = get_object_vars($json);
|
||||||
@@ -111,7 +119,7 @@ class Help extends Miniprogarampage
|
|||||||
*
|
*
|
||||||
* 随机字符串
|
* 随机字符串
|
||||||
*/
|
*/
|
||||||
public static function random($length = 20)
|
public static function random(int $length = 20): string
|
||||||
{
|
{
|
||||||
$res = [];
|
$res = [];
|
||||||
$str = 'abcdefghijklmnopqrstuvwxyz';
|
$str = 'abcdefghijklmnopqrstuvwxyz';
|
||||||
@@ -121,7 +129,7 @@ class Help extends Miniprogarampage
|
|||||||
if (empty($rand)) {
|
if (empty($rand)) {
|
||||||
$rand = substr($str, strlen($str) - 3, 1);
|
$rand = substr($str, strlen($str) - 3, 1);
|
||||||
}
|
}
|
||||||
array_push($res, $rand);
|
$res[] = $rand;
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode($res);
|
return implode($res);
|
||||||
@@ -133,22 +141,20 @@ class Help extends Miniprogarampage
|
|||||||
* @param $type
|
* @param $type
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function sign(array $array, $key, $type)
|
public static function sign(array $array, $key, $type): string
|
||||||
{
|
{
|
||||||
ksort($array, SORT_ASC);
|
ksort($array);
|
||||||
$string = [];
|
$buff = "";
|
||||||
foreach ($array as $hashKey => $val) {
|
foreach ($array as $k => $v) {
|
||||||
if (empty($val)) {
|
if ($k != "sign" && $v != "" && !is_array($v)) {
|
||||||
continue;
|
$buff .= $k . "=" . $v . "&";
|
||||||
}
|
}
|
||||||
$string[] = $hashKey . '=' . $val;
|
|
||||||
}
|
}
|
||||||
$string[] = 'key=' . $key;
|
$string = trim($buff, "&") . '&key=' . $key;
|
||||||
$string = implode('&', $string);
|
if (strtoupper($type) == 'MD5') {
|
||||||
if ($type == 'MD5') {
|
|
||||||
return strtoupper(md5($string));
|
return strtoupper(md5($string));
|
||||||
} else {
|
} else {
|
||||||
return hash('sha256', $string);
|
return hash_hmac('sha256', $string, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,950 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace wchat\common;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Swoole\Coroutine;
|
|
||||||
use Swoole\Coroutine\Http\Client as SClient;
|
|
||||||
use Swoole\Coroutine\System;
|
|
||||||
use Swoole\Coroutine\Client;
|
|
||||||
|
|
||||||
class HttpClient
|
|
||||||
{
|
|
||||||
|
|
||||||
private $host = '';
|
|
||||||
|
|
||||||
private $header = [];
|
|
||||||
|
|
||||||
private $callback = null;
|
|
||||||
private $method = 'get';
|
|
||||||
|
|
||||||
private $url = '';
|
|
||||||
private $isSSL = false;
|
|
||||||
private $agent = '';
|
|
||||||
private $isFileStream = false;
|
|
||||||
private $errorCodeField = '';
|
|
||||||
private $errorMsgField = '';
|
|
||||||
private $use_swoole = false;
|
|
||||||
|
|
||||||
private $ssl_cert_file = '';
|
|
||||||
private $ssl_key_file = '';
|
|
||||||
private $ca = '';
|
|
||||||
private $port = '';
|
|
||||||
|
|
||||||
/** @var string $_message 错误信息 */
|
|
||||||
private $_message = '';
|
|
||||||
private $_data = '';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getCa(): string
|
|
||||||
{
|
|
||||||
return $this->ca;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ca
|
|
||||||
*/
|
|
||||||
public function setCa(string $ca): void
|
|
||||||
{
|
|
||||||
$this->ca = $ca;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getPort(): string
|
|
||||||
{
|
|
||||||
return $this->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $port
|
|
||||||
*/
|
|
||||||
public function setPort(string $port): void
|
|
||||||
{
|
|
||||||
$this->port = $port;
|
|
||||||
}
|
|
||||||
|
|
||||||
const POST = 'post';
|
|
||||||
const GET = 'get';
|
|
||||||
const PUT = 'put';
|
|
||||||
const DELETE = 'delete';
|
|
||||||
const OPTIONS = 'option';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* HttpClient constructor.
|
|
||||||
*/
|
|
||||||
private function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
*/
|
|
||||||
public function setData($data)
|
|
||||||
{
|
|
||||||
$this->_data = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSslCertFile(): string
|
|
||||||
{
|
|
||||||
return $this->ssl_cert_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_cert_file
|
|
||||||
*/
|
|
||||||
public function setSslCertFile(string $ssl_cert_file)
|
|
||||||
{
|
|
||||||
$this->ssl_cert_file = $ssl_cert_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSslKeyFile(): string
|
|
||||||
{
|
|
||||||
return $this->ssl_key_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $ssl_key_file
|
|
||||||
*/
|
|
||||||
public function setSslKeyFile(string $ssl_key_file)
|
|
||||||
{
|
|
||||||
$this->ssl_key_file = $ssl_key_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
public static function NewRequest()
|
|
||||||
{
|
|
||||||
static $client = null;
|
|
||||||
if (!($client instanceof HttpClient)) {
|
|
||||||
$client = new HttpClient();
|
|
||||||
}
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setErrorField(string $name)
|
|
||||||
{
|
|
||||||
$this->errorCodeField = $name;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $bool
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setUseSwoole($bool)
|
|
||||||
{
|
|
||||||
$this->use_swoole = $bool;
|
|
||||||
if ($this->use_swoole) {
|
|
||||||
function_exists('setCli') && setCli(true);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setErrorMsgField(string $name)
|
|
||||||
{
|
|
||||||
$this->errorMsgField = $name;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $host
|
|
||||||
*/
|
|
||||||
public function setHost(string $host)
|
|
||||||
{
|
|
||||||
$this->host = $this->replaceHost($host);
|
|
||||||
$match_quest = '/^[a-zA-Z\-]+(\.[a-zA-Z\-])+/';
|
|
||||||
if (preg_match($match_quest, $this->host)) {
|
|
||||||
$this->addHeader('Host', $this->host);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
* @param array $data
|
|
||||||
* @return Result
|
|
||||||
*/
|
|
||||||
public function grpc($path, array $data)
|
|
||||||
{
|
|
||||||
$client = new Client(SWOOLE_TCP);
|
|
||||||
if (empty($this->host) || empty($this->port)) {
|
|
||||||
return new Result(['code' => 500, 'message' => 'Host and port is null']);
|
|
||||||
}
|
|
||||||
if (!$client->connect($this->host, $this->port)) {
|
|
||||||
return new Result(['code' => 500, 'message' => $client->errMsg]);
|
|
||||||
}
|
|
||||||
$path = $this->port . '/' . ltrim($path, '/');
|
|
||||||
|
|
||||||
$params['body'] = $data;
|
|
||||||
$params['header']['request_uri'] = $path;
|
|
||||||
$params['header']['request_method'] = 'grpc';
|
|
||||||
|
|
||||||
if ($client->send($params)) {
|
|
||||||
$recv = $client->recv();
|
|
||||||
$param = ['code' => 0, 'message' => Help::toArray($recv)];
|
|
||||||
} else {
|
|
||||||
$param = ['code' => 500, 'message' => $client->errMsg];
|
|
||||||
}
|
|
||||||
$client->close();
|
|
||||||
return new Result($param);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @param $value
|
|
||||||
*/
|
|
||||||
public function setHeader($key, $value)
|
|
||||||
{
|
|
||||||
$this->header[$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @param $value
|
|
||||||
*/
|
|
||||||
public function addHeader($key, $value)
|
|
||||||
{
|
|
||||||
$this->header[$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param null $callback
|
|
||||||
*/
|
|
||||||
public function setCallback($callback)
|
|
||||||
{
|
|
||||||
$this->callback = $callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $method
|
|
||||||
*/
|
|
||||||
public function setMethod(string $method)
|
|
||||||
{
|
|
||||||
$this->method = $method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $url
|
|
||||||
*/
|
|
||||||
public function setUrl(string $url)
|
|
||||||
{
|
|
||||||
$this->url = $url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setAgent(string $agent)
|
|
||||||
{
|
|
||||||
$this->agent = $agent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $isSSL
|
|
||||||
*/
|
|
||||||
public function setIsSSL(bool $isSSL)
|
|
||||||
{
|
|
||||||
$this->isSSL = $isSSL;
|
|
||||||
if ($this->isSSL) {
|
|
||||||
$this->port = 443;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function getIsSSL()
|
|
||||||
{
|
|
||||||
return $this->isSSL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $isFIle
|
|
||||||
* 设置返回类型
|
|
||||||
*/
|
|
||||||
public function asFileStream($isFIle = true)
|
|
||||||
{
|
|
||||||
$this->isFileStream = $isFIle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function request($url, $data = [])
|
|
||||||
{
|
|
||||||
$data = $this->paramEncode($data);
|
|
||||||
if ($this->use_swoole) {
|
|
||||||
return $this->coroutine($this->matchHost($url), $data);
|
|
||||||
} else {
|
|
||||||
return $this->useCurl($url, $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isCli()
|
|
||||||
{
|
|
||||||
return function_exists('getIsCli') && getIsCli();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $string
|
|
||||||
* @return bool|string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function matchHost($string = '')
|
|
||||||
{
|
|
||||||
if (empty($string)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->isHttp($string)) {
|
|
||||||
$string = str_replace('http://', '', $string);
|
|
||||||
$hostAndUrls = explode('/', $string);
|
|
||||||
|
|
||||||
$this->host = array_shift($hostAndUrls);
|
|
||||||
$string = implode('/', $hostAndUrls);
|
|
||||||
} else if ($this->isHttps($string)) {
|
|
||||||
$string = str_replace('https://', '', $string);
|
|
||||||
$this->setIsSSL(true);
|
|
||||||
|
|
||||||
$hostAndUrls = explode('/', $string);
|
|
||||||
|
|
||||||
$this->host = array_shift($hostAndUrls);
|
|
||||||
$string = implode('/', $hostAndUrls);
|
|
||||||
} else if (empty($this->host)) {
|
|
||||||
$hostAndUrls = explode('/', $string);
|
|
||||||
$this->host = array_shift($hostAndUrls);
|
|
||||||
|
|
||||||
$string = implode('/', $hostAndUrls);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strpos($this->host, ':') !== false) {
|
|
||||||
[$this->host, $this->port] = explode(':', $this->host);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) {
|
|
||||||
$this->host = System::gethostbyname($this->host);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->checkIsIp($this->host) && !$this->isDomainName($this->host)) {
|
|
||||||
throw new Exception('Client Host error.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return bool|mixed
|
|
||||||
*/
|
|
||||||
private function isDomainName($name)
|
|
||||||
{
|
|
||||||
if (!preg_match('/^[a-zA-Z\-0-9]+(\.[a-zA-Z\-0-9]+)+[^\/]?/', $name, $out)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $out[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param $data
|
|
||||||
* @return array|Result|mixed
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
private function useCurl($url, $data)
|
|
||||||
{
|
|
||||||
if ($this->isHttp($url) || $this->isHttps($url)) {
|
|
||||||
return $this->curl($url, $data);
|
|
||||||
}
|
|
||||||
$url = $this->matchHost(ltrim($url, '/'));
|
|
||||||
if (!empty($this->port)) {
|
|
||||||
$this->host .= ':' . $this->port;
|
|
||||||
}
|
|
||||||
if ($this->isSSL) {
|
|
||||||
return $this->curl('https://' . $this->host . '/' . $url, $data);
|
|
||||||
} else {
|
|
||||||
return $this->curl('http://' . $this->host . '/' . $url, $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $host
|
|
||||||
* @return string|string[]
|
|
||||||
*/
|
|
||||||
private function replaceHost($host)
|
|
||||||
{
|
|
||||||
if ($this->isHttp($host)) {
|
|
||||||
return str_replace('http://', '', $host);
|
|
||||||
}
|
|
||||||
if ($this->isHttps($host)) {
|
|
||||||
return str_replace('https://', '', $host);
|
|
||||||
}
|
|
||||||
return $host;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @return false|int
|
|
||||||
*/
|
|
||||||
private function checkIsIp($url)
|
|
||||||
{
|
|
||||||
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isHttp($url)
|
|
||||||
{
|
|
||||||
return strpos($url, 'http://') === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isHttps($url)
|
|
||||||
{
|
|
||||||
return strpos($url, 'https://') === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws Exception
|
|
||||||
* 使用swoole协程方式请求
|
|
||||||
*/
|
|
||||||
private function coroutine($url, $data = [])
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$client = $this->generate_client($this->host, $url, $data);
|
|
||||||
if ($client->statusCode < 0) {
|
|
||||||
throw new Exception($client->errMsg);
|
|
||||||
}
|
|
||||||
unset($this->_data);
|
|
||||||
|
|
||||||
$body = $this->resolve($client->getHeaders(), $client->body);
|
|
||||||
if ($client->getStatusCode() != 200) {
|
|
||||||
if (is_string($body)) {
|
|
||||||
$message = 'Request error code ' . $client->getStatusCode();
|
|
||||||
} else {
|
|
||||||
$message = $this->searchMessageByData($body);
|
|
||||||
}
|
|
||||||
$response['code'] = $client->getStatusCode();
|
|
||||||
$response['message'] = $message;
|
|
||||||
$response['data'] = $body;
|
|
||||||
} else {
|
|
||||||
$response = $this->structure($body, $data, $client->getHeaders());
|
|
||||||
}
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$response['code'] = 500;
|
|
||||||
$response['message'] = $exception->getMessage();
|
|
||||||
$response['data'] = array_slice($exception->getTrace(), 0, 6);
|
|
||||||
$response['header'] = [];
|
|
||||||
}
|
|
||||||
if (!($response instanceof Result)) {
|
|
||||||
$response = new Result($response);
|
|
||||||
}
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function getHostPort()
|
|
||||||
{
|
|
||||||
if (!empty($this->port)) {
|
|
||||||
return $this->port;
|
|
||||||
}
|
|
||||||
$port = 80;
|
|
||||||
if ($this->isSSL) $port = 443;
|
|
||||||
return $port;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $host
|
|
||||||
* @param $url
|
|
||||||
* @param $data
|
|
||||||
* @return SClient
|
|
||||||
*/
|
|
||||||
private function generate_client($host, $url, $data = [])
|
|
||||||
{
|
|
||||||
$client = new SClient($host, $this->getHostPort(), $this->isSSL);
|
|
||||||
if (strpos($url, '/') !== 0) {
|
|
||||||
$url = '/' . $url;
|
|
||||||
}
|
|
||||||
|
|
||||||
$client->set($this->settings());
|
|
||||||
if (!empty($this->agent)) {
|
|
||||||
$this->header['User-Agent'] = $this->agent;
|
|
||||||
}
|
|
||||||
if (!empty($this->header)) {
|
|
||||||
$client->setHeaders($this->header);
|
|
||||||
}
|
|
||||||
$client->setMethod(strtoupper($this->method));
|
|
||||||
if (strtolower($this->method) == self::GET && !empty($data)) {
|
|
||||||
$url .= '?' . $data;
|
|
||||||
} else {
|
|
||||||
$this->_data = $this->mergeParams($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($this->_data)) {
|
|
||||||
$client->setData($this->_data);
|
|
||||||
}
|
|
||||||
$client->execute($url);
|
|
||||||
$client->close();
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $newData
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function mergeParams($newData)
|
|
||||||
{
|
|
||||||
if (empty($this->_data)) {
|
|
||||||
return $this->toRequest($newData);
|
|
||||||
} else if (empty($newData)) {
|
|
||||||
return $this->toRequest($this->_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
$newData = Help::toArray($newData);
|
|
||||||
$array = Help::toArray($this->_data);
|
|
||||||
|
|
||||||
$params = array_merge($array, $newData);
|
|
||||||
|
|
||||||
return $this->toRequest($params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @return false|mixed|string
|
|
||||||
*/
|
|
||||||
private function toRequest($data)
|
|
||||||
{
|
|
||||||
if (is_string($data)) {
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
$contentType = 'application/x-www-form-urlencoded';
|
|
||||||
if (isset($this->header['Content-Type'])) {
|
|
||||||
$contentType = $this->header['Content-Type'];
|
|
||||||
} else if (isset($this->header['content-type'])) {
|
|
||||||
$contentType = $this->header['content-type'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strpos($contentType, 'json') !== false) {
|
|
||||||
return Help::toJson($data);
|
|
||||||
} else if (strpos($contentType, 'xml') !== false) {
|
|
||||||
return Help::toXml($data);
|
|
||||||
} else {
|
|
||||||
return http_build_query($data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function settings()
|
|
||||||
{
|
|
||||||
$sslCert = $this->getSslCertFile();
|
|
||||||
$sslKey = $this->getSslKeyFile();
|
|
||||||
$sslCa = $this->getCa();
|
|
||||||
|
|
||||||
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$params['ssl_host_name'] = $this->host;
|
|
||||||
$params['ssl_cert_file'] = $this->getSslCertFile();
|
|
||||||
$params['ssl_key_file'] = $this->getSslKeyFile();
|
|
||||||
$params['ssl_verify_peer'] = true;
|
|
||||||
$params['ssl_cafile'] = $sslCa;
|
|
||||||
|
|
||||||
return $params;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
*/
|
|
||||||
private function curl($url, $data = [])
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$output = $this->curlParse($url, $this->mergeParams($data));
|
|
||||||
if ($output === FALSE) {
|
|
||||||
return new Result(['code' => 500, 'message' => $output]);
|
|
||||||
}
|
|
||||||
[$header, $body, $status] = $this->explode($output);
|
|
||||||
if ($status != 200 && $status != 201) {
|
|
||||||
$data = new Result(['code' => $status, 'message' => $body, 'header' => $header]);
|
|
||||||
} else {
|
|
||||||
$data = $this->structure($body, $data, $header);
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$response['code'] = 500;
|
|
||||||
$response['message'] = $exception->getMessage();
|
|
||||||
$response['data'] = array_slice($exception->getTrace(), 0, 6);
|
|
||||||
$response['header'] = [];
|
|
||||||
return new Result($response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param $data
|
|
||||||
* @return bool|string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function curlParse($url, $data)
|
|
||||||
{
|
|
||||||
$ch = curl_init();
|
|
||||||
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
|
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
|
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
||||||
|
|
||||||
if ($headers = $this->parseHeaderMat()) {
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($this->agent)) {
|
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
|
|
||||||
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
|
|
||||||
curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile());
|
|
||||||
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
|
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
|
|
||||||
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
|
|
||||||
|
|
||||||
if ($this->method == self::POST) {
|
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->method != self::GET) {
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
|
|
||||||
$output = curl_exec($ch);
|
|
||||||
if ($output === false) {
|
|
||||||
throw new Exception(curl_error($ch));
|
|
||||||
}
|
|
||||||
curl_close($ch);
|
|
||||||
return $output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $output
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function explode($output)
|
|
||||||
{
|
|
||||||
[$header, $body] = explode("\r\n\r\n", $output, 2);
|
|
||||||
if ($header == 'HTTP/1.1 100 Continue') {
|
|
||||||
[$header, $body] = explode("\r\n\r\n", $body, 2);
|
|
||||||
} else if (strpos($body, "\r\n\r\n") !== false) {
|
|
||||||
[$header, $body] = explode("\r\n\r\n", $body, 2);
|
|
||||||
}
|
|
||||||
$header = explode("\r\n", $header);
|
|
||||||
|
|
||||||
unset($output);
|
|
||||||
|
|
||||||
$status = (int)explode(' ', trim($header[0]))[1];
|
|
||||||
$header = $this->headerFormat($header);
|
|
||||||
|
|
||||||
return [$header, $this->resolve($header, $body), $status];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param $data
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function createRequestUrl($url, $data)
|
|
||||||
{
|
|
||||||
if ($this->isGet()) {
|
|
||||||
return $url . '?' . $data;
|
|
||||||
}
|
|
||||||
return $url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @param $body
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function resolve($data, $body)
|
|
||||||
{
|
|
||||||
if (is_array($body)) {
|
|
||||||
return $body;
|
|
||||||
}
|
|
||||||
$type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html';
|
|
||||||
if (strpos($type, 'text/html') !== false) {
|
|
||||||
return $body;
|
|
||||||
} else if (strpos($type, 'json') !== false) {
|
|
||||||
return json_decode($body, true);
|
|
||||||
} else if (strpos($type, 'xml') !== false) {
|
|
||||||
return Help::xmlToArray($body);
|
|
||||||
} else if (strpos($type, 'plain') !== false) {
|
|
||||||
return Help::toArray($body);
|
|
||||||
}
|
|
||||||
return $body;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $headers
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function headerFormat($headers)
|
|
||||||
{
|
|
||||||
$_tmp = [];
|
|
||||||
foreach ($headers as $key => $val) {
|
|
||||||
$trim = explode(': ', trim($val));
|
|
||||||
|
|
||||||
$_tmp[strtolower($trim[0])] = $trim[1] ?? '';
|
|
||||||
}
|
|
||||||
return $_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $body
|
|
||||||
* @param $_data
|
|
||||||
* @param $header
|
|
||||||
* @param $statusCode
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* 构建返回体
|
|
||||||
*/
|
|
||||||
private function structure($body, $_data, $header = [], $statusCode = 200)
|
|
||||||
{
|
|
||||||
$this->setIsSSL(false);
|
|
||||||
$this->setHeaders([]);
|
|
||||||
|
|
||||||
if ($this->callback !== NULL) {
|
|
||||||
$result = call_user_func($this->callback, $body, $_data, $header);
|
|
||||||
$this->setCallback(null);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
if (is_string($body)) {
|
|
||||||
$result['code'] = 0;
|
|
||||||
$result['message'] = '';
|
|
||||||
} else {
|
|
||||||
$result['code'] = $body[$this->errorCodeField] ?? 0;
|
|
||||||
$result['message'] = $this->searchMessageByData($body);
|
|
||||||
}
|
|
||||||
$result['data'] = $body;
|
|
||||||
$result['header'] = $header;
|
|
||||||
$result['httpStatus'] = $statusCode;
|
|
||||||
|
|
||||||
|
|
||||||
return new Result($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $body
|
|
||||||
* @return array|mixed|string
|
|
||||||
*/
|
|
||||||
private function searchMessageByData($body)
|
|
||||||
{
|
|
||||||
$parent = [];
|
|
||||||
if (empty($this->errorMsgField)) {
|
|
||||||
return 'system success.';
|
|
||||||
}
|
|
||||||
$explode = explode('.', $this->errorMsgField);
|
|
||||||
if (!isset($body[$explode[0]])) {
|
|
||||||
return 'system success.';
|
|
||||||
}
|
|
||||||
foreach ($explode as $item) {
|
|
||||||
if (empty($item)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (empty($parent)) {
|
|
||||||
$parent = $body[$item];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (is_string($parent) || !isset($parent[$item])) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$parent = $parent[$item];
|
|
||||||
}
|
|
||||||
return !empty($parent) ? $parent : 'system success.';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* check isPost Request
|
|
||||||
*/
|
|
||||||
public function isPost()
|
|
||||||
{
|
|
||||||
return strtolower($this->method) === self::POST;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* check isGet Request
|
|
||||||
*/
|
|
||||||
public function isGet()
|
|
||||||
{
|
|
||||||
return strtolower($this->method) === self::GET;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $arr
|
|
||||||
*
|
|
||||||
* @return array|string
|
|
||||||
* 将请求参数进行编码
|
|
||||||
*/
|
|
||||||
private function paramEncode($arr)
|
|
||||||
{
|
|
||||||
if (!is_array($arr)) {
|
|
||||||
return $arr;
|
|
||||||
}
|
|
||||||
$_tmp = [];
|
|
||||||
foreach ($arr as $Key => $val) {
|
|
||||||
$_tmp[$Key] = $val;
|
|
||||||
}
|
|
||||||
if ($this->isGet()) {
|
|
||||||
return http_build_query($_tmp);
|
|
||||||
}
|
|
||||||
return $_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function post($url, $data = [])
|
|
||||||
{
|
|
||||||
$this->setMethod(self::POST);
|
|
||||||
return $this->request($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function put($url, $data = [])
|
|
||||||
{
|
|
||||||
$this->setMethod(self::PUT);
|
|
||||||
return $this->request($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function get($url, $data = [])
|
|
||||||
{
|
|
||||||
$this->setMethod(self::GET);
|
|
||||||
return $this->request($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function option($url, $data = [])
|
|
||||||
{
|
|
||||||
$this->setMethod(self::OPTIONS);
|
|
||||||
return $this->request($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function delete($url, $data = [])
|
|
||||||
{
|
|
||||||
$this->setMethod(self::DELETE);
|
|
||||||
return $this->request($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function send($url, $data = [])
|
|
||||||
{
|
|
||||||
return $this->request($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function parseHeaderMat()
|
|
||||||
{
|
|
||||||
if ($this->use_swoole) {
|
|
||||||
return $this->header;
|
|
||||||
}
|
|
||||||
$headers = [];
|
|
||||||
foreach ($this->header as $key => $val) {
|
|
||||||
$header = $key . ':' . $val;
|
|
||||||
if (in_array($header, $headers)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$headers[] = $header;
|
|
||||||
}
|
|
||||||
$this->header = [];
|
|
||||||
return $headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $headers
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function setHeaders(array $headers)
|
|
||||||
{
|
|
||||||
if (empty($headers)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
foreach ($headers as $key => $val) {
|
|
||||||
$this->header[$key] = $val;
|
|
||||||
}
|
|
||||||
return $this->header;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,221 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/3/26 0026
|
|
||||||
* Time: 10:23
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace wchat\common;
|
|
||||||
|
|
||||||
use wchat\common\Config;
|
|
||||||
|
|
||||||
abstract class Miniprogarampage implements Progaram
|
|
||||||
{
|
|
||||||
|
|
||||||
/** @var Config */
|
|
||||||
protected $config;
|
|
||||||
|
|
||||||
/** @var Miniprogarampage $instance */
|
|
||||||
protected static $instance = null;
|
|
||||||
|
|
||||||
/** @var HttpClient */
|
|
||||||
protected $request = null;
|
|
||||||
|
|
||||||
protected $errorCode = 0;
|
|
||||||
protected $errorMsg = '';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $message
|
|
||||||
* @param int $code
|
|
||||||
* @return Result
|
|
||||||
*/
|
|
||||||
protected function sendError($message, $code = 500)
|
|
||||||
{
|
|
||||||
return new Result(['code' => $code, 'message' => $message]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $code
|
|
||||||
*/
|
|
||||||
public function setErrorCode($code)
|
|
||||||
{
|
|
||||||
$this->errorCode = $code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $message
|
|
||||||
*/
|
|
||||||
public function setErrorMessage($message)
|
|
||||||
{
|
|
||||||
$this->errorMsg = $message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getErrorCode()
|
|
||||||
{
|
|
||||||
return $this->errorCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getErrorMessage()
|
|
||||||
{
|
|
||||||
return $this->errorMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Miniprogarampage constructor.
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function getInstance(Config $config)
|
|
||||||
{
|
|
||||||
if (static::$instance === null) {
|
|
||||||
static::$instance = new static();
|
|
||||||
}
|
|
||||||
static::$instance->config = $config;
|
|
||||||
static::$instance->request = HttpClient::NewRequest();
|
|
||||||
static::$instance->request->setAgent($config->getAgent());
|
|
||||||
static::$instance->request->setUseSwoole($config->isUsrSwoole());
|
|
||||||
|
|
||||||
$request = static::$instance->request;
|
|
||||||
if ($request->getIsSSL()) {
|
|
||||||
$request->addHeader('ssl_cert_file', $config->getSslCert());
|
|
||||||
$request->addHeader('ssl_key_file', $config->getSslKey());
|
|
||||||
}
|
|
||||||
return static::$instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function initConfig($config)
|
|
||||||
{
|
|
||||||
$this->config = $config;
|
|
||||||
$this->request = HttpClient::NewRequest();
|
|
||||||
$this->request->setIsSSL(true);
|
|
||||||
$this->request->setData(null);
|
|
||||||
$this->request->setUseSwoole($config->isUsrSwoole());
|
|
||||||
if (!empty($config->getAgent())) {
|
|
||||||
$this->request->setAgent($config->getAgent());
|
|
||||||
}
|
|
||||||
if (!empty($config->getSslCert())) {
|
|
||||||
$this->request->setSslCertFile($config->getSslCert());
|
|
||||||
}
|
|
||||||
if (!empty($config->getSslKey())) {
|
|
||||||
$this->request->setSslKeyFile($config->getSslKey());
|
|
||||||
}
|
|
||||||
if (!empty($config->getSslCa())) {
|
|
||||||
$this->request->setCa($config->getSslCa());
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \wchat\common\Config
|
|
||||||
*/
|
|
||||||
public function getConfig()
|
|
||||||
{
|
|
||||||
return $this->config;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool|mixed|string
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
protected function getAccessToken()
|
|
||||||
{
|
|
||||||
$access = $this->config->getAccessToken();
|
|
||||||
if (!empty($access)) {
|
|
||||||
return $access;
|
|
||||||
}
|
|
||||||
$this->request->setMethod(HttpClient::GET);
|
|
||||||
$data = $this->request->get('/cgi-bin/token', [
|
|
||||||
'grant_type' => 'client_credential',
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'secret' => $this->config->getAppsecret(),
|
|
||||||
]);
|
|
||||||
if (!$data->isResultsOK()) {
|
|
||||||
throw new \Exception($data->getMessage());
|
|
||||||
}
|
|
||||||
$access = $data->getData('access_token');
|
|
||||||
$this->config->setAccessToken($access);
|
|
||||||
return $access;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @param $body
|
|
||||||
* @return mixed
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
protected function buildResult($data, $body = NULL)
|
|
||||||
{
|
|
||||||
$data = $this->checkSign($data);
|
|
||||||
if (!$data) {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = '签名错误.';
|
|
||||||
} else {
|
|
||||||
if (isset($data['return_code'])) {
|
|
||||||
if ($data['return_code'] == 'FAIL') {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = $data['return_msg'];
|
|
||||||
} else {
|
|
||||||
$return['code'] = 0;
|
|
||||||
$return['data'] = $data;
|
|
||||||
$return['data']['postBody'] = $body;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($data['errcode'] == 'FAIL') {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = $data['errmsg'];
|
|
||||||
} else {
|
|
||||||
$return['code'] = 0;
|
|
||||||
$return['data'] = $data;
|
|
||||||
$return['data']['postBody'] = $body;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $result
|
|
||||||
* @return mixed
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
protected function checkSign($result)
|
|
||||||
{
|
|
||||||
$data = Help::toArray($result);
|
|
||||||
|
|
||||||
if (!isset($data['sign'])) {
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
$sign = $data['sign'];
|
|
||||||
|
|
||||||
unset($data['sign']);
|
|
||||||
|
|
||||||
$key = $this->config->getKey();
|
|
||||||
$sign_type = $this->config->getSignType();
|
|
||||||
|
|
||||||
$_sign = Help::sign($data, $key, $sign_type);
|
|
||||||
if ($sign != $_sign) {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/3/26 0026
|
||||||
|
* Time: 10:23
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace wchat\common;
|
||||||
|
|
||||||
|
use wchat\common\Config;
|
||||||
|
|
||||||
|
abstract class Multiprogramming implements Progaram
|
||||||
|
{
|
||||||
|
|
||||||
|
/** @var Config */
|
||||||
|
protected Config $config;
|
||||||
|
|
||||||
|
protected static ?Multiprogramming $instance = null;
|
||||||
|
|
||||||
|
protected int $errorCode = 0;
|
||||||
|
protected string $errorMsg = '';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $message
|
||||||
|
* @param int $code
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
protected function sendError($message, int $code = 500): Result
|
||||||
|
{
|
||||||
|
return new Result(code: $code, message: $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $code
|
||||||
|
*/
|
||||||
|
public function setErrorCode($code)
|
||||||
|
{
|
||||||
|
$this->errorCode = $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $message
|
||||||
|
*/
|
||||||
|
public function setErrorMessage($message)
|
||||||
|
{
|
||||||
|
$this->errorMsg = $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getErrorCode(): int
|
||||||
|
{
|
||||||
|
return $this->errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getErrorMessage(): string
|
||||||
|
{
|
||||||
|
return $this->errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \wchat\common\Config $config
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setConfig(Config $config): void
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \wchat\common\Config
|
||||||
|
*/
|
||||||
|
public function getConfig(): \wchat\common\Config
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $result
|
||||||
|
* @return array|bool
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
protected function checkSign($result): array|bool
|
||||||
|
{
|
||||||
|
$data = Help::toArray($result);
|
||||||
|
|
||||||
|
if (!isset($data['sign'])) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sign = $data['sign'];
|
||||||
|
|
||||||
|
unset($data['sign']);
|
||||||
|
|
||||||
|
$key = $this->config->getKey();
|
||||||
|
$sign_type = $this->config->getSignType();
|
||||||
|
|
||||||
|
$_sign = Help::sign($data, $key, $sign_type);
|
||||||
|
if ($sign != $_sign) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+148
-143
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
namespace wchat\common;
|
namespace wchat\common;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
use Kiri\Client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Result
|
* Class Result
|
||||||
*
|
*
|
||||||
@@ -14,163 +18,164 @@ namespace wchat\common;
|
|||||||
*/
|
*/
|
||||||
class Result
|
class Result
|
||||||
{
|
{
|
||||||
public $code;
|
|
||||||
public $message;
|
|
||||||
public $count = 0;
|
|
||||||
public $data;
|
|
||||||
public $header;
|
|
||||||
private $startTime;
|
|
||||||
private $requestTime;
|
|
||||||
private $runTime;
|
|
||||||
|
|
||||||
public function __construct(array $data)
|
private ?int $startTime;
|
||||||
{
|
private ?int $requestTime;
|
||||||
foreach ($data as $key => $val) {
|
private ?int $runTime;
|
||||||
$this->$key = $val;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->header = $this->reloadResponse($this->header);
|
/**
|
||||||
}
|
* @param int $code
|
||||||
|
* @param string $message
|
||||||
|
* @param int $count
|
||||||
|
* @param mixed $data
|
||||||
|
* @param array $header
|
||||||
|
*/
|
||||||
|
public function __construct(public int $code, public string $message = '', public int $count = 0, public mixed $data = [], public array $header = [],)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $header
|
* @param string $data
|
||||||
*
|
* @return static
|
||||||
* @return array
|
*/
|
||||||
*/
|
public static function wxInit(string $data): static
|
||||||
private function reloadResponse($header)
|
{
|
||||||
{
|
$json = json_decode($data, true);
|
||||||
$data = [];
|
if (isset($json['errcode']) && $json['errcode'] != 0) {
|
||||||
if (is_array($header)) {
|
return new Result(code: $json['errcode'], message: $json['errmsg']);
|
||||||
return $header;
|
} else {
|
||||||
}
|
return new Result(code: 0, data: $json);
|
||||||
$load = explode("\n", $header);
|
}
|
||||||
if (!empty($load) && is_array($load)) {
|
}
|
||||||
foreach ($load as $key => $val) {
|
|
||||||
if (empty($val)) continue;
|
|
||||||
$ex = explode(': ', $val);
|
|
||||||
if (!empty($ex[0]) && !empty($ex[1])) {
|
|
||||||
$data[trim($ex[0])] = trim($ex[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __get($name)
|
|
||||||
{
|
|
||||||
return $this->$name;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function __set($name, $value)
|
/**
|
||||||
{
|
* @param Client $client
|
||||||
$this->$name = $value;
|
* @return static
|
||||||
|
*/
|
||||||
|
public static function init(Client $client): static
|
||||||
|
{
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @param $name
|
||||||
*/
|
* @return mixed
|
||||||
public function getTime()
|
*/
|
||||||
{
|
public function __get($name)
|
||||||
return [
|
{
|
||||||
'startTime' => $this->startTime,
|
return $this->$name;
|
||||||
'requestTime' => $this->requestTime,
|
}
|
||||||
'runTime' => $this->runTime,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @param $data
|
|
||||||
* @return $this
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function setAttr($key, $data)
|
|
||||||
{
|
|
||||||
if (!property_exists($this, $key)) {
|
|
||||||
throw new \Exception('未查找到相应对象属性');
|
|
||||||
}
|
|
||||||
$this->$key = $data;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @param $name
|
||||||
*/
|
* @param $value
|
||||||
public function isResultsOK()
|
* @return $this
|
||||||
{
|
*/
|
||||||
if (!is_numeric($this->code)) {
|
public function __set($name, $value)
|
||||||
return false;
|
{
|
||||||
}
|
$this->$name = $value;
|
||||||
return $this->code == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* @param array $headers
|
}
|
||||||
* 批量设置返回头
|
|
||||||
*/
|
|
||||||
public function setHeaders(array $headers)
|
|
||||||
{
|
|
||||||
foreach ($headers as $key => $val) {
|
|
||||||
$this->setHeader($key, $val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @return array
|
||||||
* @param $val
|
*/
|
||||||
* 设置返回头
|
#[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])]
|
||||||
*/
|
public function getTime(): array
|
||||||
public function setHeader($key, $val)
|
{
|
||||||
{
|
return [
|
||||||
header($key . ':' . $val);
|
'startTime' => $this->startTime,
|
||||||
}
|
'requestTime' => $this->requestTime,
|
||||||
|
'runTime' => $this->runTime,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param $key
|
||||||
* @return mixed
|
* @param $data
|
||||||
*/
|
* @return $this
|
||||||
public function getData($name = '')
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
if (!$this->isResultsOK()) {
|
public function setAttr($key, $data): static
|
||||||
return $this->data;
|
{
|
||||||
}
|
if (!property_exists($this, $key)) {
|
||||||
if (!empty($name) && isset($this->data[$name])) {
|
throw new Exception('未查找到相应对象属性');
|
||||||
return $this->data[$name];
|
}
|
||||||
}
|
$this->$key = $data;
|
||||||
return $this->data;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @return bool
|
||||||
* @param $data
|
*/
|
||||||
* @return $this
|
public function isResultsOK(): bool
|
||||||
*/
|
{
|
||||||
public function append($key, $data)
|
if (!is_numeric($this->code)) {
|
||||||
{
|
return false;
|
||||||
if (!is_array($this->data)) {
|
}
|
||||||
$this->data = ['origin' => $this->data];
|
return $this->code == 0;
|
||||||
}
|
}
|
||||||
$this->data[$key] = $data;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getMessage()
|
|
||||||
{
|
|
||||||
return $this->message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @param string $name
|
||||||
*/
|
* @return mixed
|
||||||
public function getCode()
|
*/
|
||||||
{
|
public function getData(string $name = ''): mixed
|
||||||
return $this->code;
|
{
|
||||||
}
|
if (!$this->isResultsOK()) {
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
if (!is_array($this->data)) {
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
if ($name != '') {
|
||||||
|
return $this->data[$name] ?? null;
|
||||||
|
} else {
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $key
|
||||||
|
* @param $data
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function append($key, $data): static
|
||||||
|
{
|
||||||
|
if (!is_array($this->data)) {
|
||||||
|
$this->data = ['origin' => $this->data];
|
||||||
|
}
|
||||||
|
$this->data[$key] = $data;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMessage(): string
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getCode(): int
|
||||||
|
{
|
||||||
|
return $this->code;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
|
|
||||||
class AccessToken extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\common\Miniprogarampage;
|
|
||||||
|
|
||||||
abstract class AfficialAccount extends Miniprogarampage
|
|
||||||
{
|
|
||||||
protected static $instance;
|
|
||||||
|
|
||||||
private $url = 'https://api.weixin.qq.com/cgi-bin/token?';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function generateAccess_token()
|
|
||||||
{
|
|
||||||
if (!empty($this->config->getToken())) {
|
|
||||||
return $this->config->getToken();
|
|
||||||
}
|
|
||||||
$requestParam['grant_type'] = 'client_credential';
|
|
||||||
$requestParam['appid'] = $this->config->getAppid();
|
|
||||||
$requestParam['secret'] = $this->config->getAppsecret();
|
|
||||||
|
|
||||||
$result = HttpClient::NewRequest()->get($this->url, $requestParam);
|
|
||||||
if (!$result->isResultsOK()) {
|
|
||||||
throw new \Exception($result->getMessage(), $result->getCode());
|
|
||||||
}
|
|
||||||
return $result->getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\officialaccount\dcaler\SnsInfo;
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\officialaccount\dcaler\Authorization as DAuth;
|
|
||||||
|
|
||||||
class Authorization extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
private $url = 'https://api.weixin.qq.com/cgi-bin/user/info';
|
|
||||||
|
|
||||||
private $turn = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token=';
|
|
||||||
|
|
||||||
private $oauth2 = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
|
||||||
private $snsUserInfo = 'https://api.weixin.qq.com/sns/userinfo';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $code
|
|
||||||
* @return array|mixed|\wchat\Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function authorization_user_info($code)
|
|
||||||
{
|
|
||||||
$param = $this->GetAccessTokenByCode($code);
|
|
||||||
if (!$param->isResultsOK()) {
|
|
||||||
throw new \Exception($param->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
$requestParam['access_token'] = $param->getData('access_token');
|
|
||||||
$requestParam['openid'] = $param->getData('openid');
|
|
||||||
$requestParam['lang'] = 'zh_CN';
|
|
||||||
|
|
||||||
$client = HttpClient::NewRequest();
|
|
||||||
$result = $client->get($this->snsUserInfo, $requestParam);
|
|
||||||
if ($result->isResultsOK()) {
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
$userInfo = SnsInfo::instance($result->getData());
|
|
||||||
$result->append('instance', $userInfo);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $code
|
|
||||||
* @return array|mixed|\wchat\Result
|
|
||||||
*/
|
|
||||||
private function GetAccessTokenByCode($code)
|
|
||||||
{
|
|
||||||
$requestParam['appid'] = $this->config->getAppid();
|
|
||||||
$requestParam['secret'] = $this->config->getAppsecret();
|
|
||||||
$requestParam['code'] = $code;
|
|
||||||
$requestParam['grant_type'] = 'authorization_code';
|
|
||||||
|
|
||||||
$client = HttpClient::NewRequest();
|
|
||||||
return $client->get($this->oauth2, $requestParam);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $openid
|
|
||||||
* @return DAuth
|
|
||||||
* @throws \Exception
|
|
||||||
* 获取用户信息
|
|
||||||
*/
|
|
||||||
public function To_grant_authorization($openid)
|
|
||||||
{
|
|
||||||
$requestParam['access_token'] = $this->config->getAccessToken();
|
|
||||||
$requestParam['openid'] = $openid;
|
|
||||||
$requestParam['lang'] = 'zh_CN';
|
|
||||||
|
|
||||||
$authorization = $this->request->get($this->url, $requestParam);
|
|
||||||
if (!$authorization) {
|
|
||||||
throw new \Exception($authorization->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return DAuth::instance($authorization->getData());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $long_url
|
|
||||||
* @return string
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function Turn_to_short_chain($long_url)
|
|
||||||
{
|
|
||||||
$token = $this->config->getAccessToken();
|
|
||||||
$requestParam['action'] = 'long2short';
|
|
||||||
$requestParam['long_url'] = $long_url;
|
|
||||||
|
|
||||||
$result = $this->request->post($this->turn . $token, $requestParam);
|
|
||||||
if (!$result->isResultsOK()) {
|
|
||||||
throw new \Exception($result->getMessage());
|
|
||||||
}
|
|
||||||
return $result->getData('short_url');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,178 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\officialaccount\dcaler\Prpcrypt;
|
|
||||||
use wchat\officialaccount\dcaler\XMLParse;
|
|
||||||
|
|
||||||
class NewsManager extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
const OK = 0;
|
|
||||||
const ValidateSignatureError = -40001;
|
|
||||||
const ParseXmlError = -40002;
|
|
||||||
const ComputeSignatureError = -40003;
|
|
||||||
const IllegalAesKey = -40004;
|
|
||||||
const ValidateAppidError = -40005;
|
|
||||||
const EncryptAESError = -40006;
|
|
||||||
const DecryptAESError = -40007;
|
|
||||||
const IllegalBuffer = -40008;
|
|
||||||
const EncodeBase64Error = -40009;
|
|
||||||
const DecodeBase64Error = -40010;
|
|
||||||
const GenReturnXmlError = -40011;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $code
|
|
||||||
* @return mixed|string
|
|
||||||
*/
|
|
||||||
public function toError($code)
|
|
||||||
{
|
|
||||||
$messages = [
|
|
||||||
self::ValidateSignatureError => '签名验证错误',
|
|
||||||
self::ParseXmlError => 'xml解析失败',
|
|
||||||
self::ComputeSignatureError => 'sha加密生成签名失败',
|
|
||||||
self::IllegalAesKey => 'encodingAesKey 非法',
|
|
||||||
self::ValidateAppidError => 'appid 校验错误',
|
|
||||||
self::EncryptAESError => 'aes 加密失败',
|
|
||||||
self::DecryptAESError => 'aes 解密失败',
|
|
||||||
self::IllegalBuffer => '解密后得到的buffer非法',
|
|
||||||
self::EncodeBase64Error => 'base64加密失败',
|
|
||||||
self::DecodeBase64Error => 'base64解密失败',
|
|
||||||
self::GenReturnXmlError => '生成xml失败',
|
|
||||||
];
|
|
||||||
return $messages[$code] ?? 'OK';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将公众平台回复用户的消息加密打包.
|
|
||||||
* <ol>
|
|
||||||
* <li>对要发送的消息进行AES-CBC加密</li>
|
|
||||||
* <li>生成安全签名</li>
|
|
||||||
* <li>将消息密文和安全签名打包成xml格式</li>
|
|
||||||
* </ol>
|
|
||||||
*
|
|
||||||
* @param $replyMsg string 公众平台待回复用户的消息,xml格式的字符串
|
|
||||||
* @param $timeStamp string 时间戳,可以自己生成,也可以用URL参数的timestamp
|
|
||||||
* @param $nonce string 随机串,可以自己生成,也可以用URL参数的nonce
|
|
||||||
* @param &$encryptMsg string 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串,
|
|
||||||
* 当return返回0时有效
|
|
||||||
*
|
|
||||||
* @return int 成功0,失败返回对应的错误码
|
|
||||||
*/
|
|
||||||
public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg)
|
|
||||||
{
|
|
||||||
$pc = new Prpcrypt($this->config->getEncodingAesKey());
|
|
||||||
|
|
||||||
//加密
|
|
||||||
$array = $pc->encrypt($replyMsg, $this->config->getAppid());
|
|
||||||
$ret = $array[0];
|
|
||||||
if ($ret != 0) {
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($timeStamp == null) {
|
|
||||||
$timeStamp = time();
|
|
||||||
}
|
|
||||||
$encrypt = $array[1];
|
|
||||||
|
|
||||||
//生成安全签名
|
|
||||||
$array = $this->getSHA1($this->config->getToken(), $timeStamp, $nonce, $encrypt);
|
|
||||||
$ret = $array[0];
|
|
||||||
if ($ret != 0) {
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
$signature = $array[1];
|
|
||||||
|
|
||||||
//生成发送的xml
|
|
||||||
$xmlparse = new XMLParse;
|
|
||||||
$encryptMsg = $xmlparse->generate($encrypt, $signature, $timeStamp, $nonce);
|
|
||||||
return self::OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检验消息的真实性,并且获取解密后的明文.
|
|
||||||
* <ol>
|
|
||||||
* <li>利用收到的密文生成安全签名,进行签名验证</li>
|
|
||||||
* <li>若验证通过,则提取xml中的加密消息</li>
|
|
||||||
* <li>对消息进行解密</li>
|
|
||||||
* </ol>
|
|
||||||
*
|
|
||||||
* @param $msgSignature string 签名串,对应URL参数的msg_signature
|
|
||||||
* @param $timestamp string 时间戳 对应URL参数的timestamp
|
|
||||||
* @param $nonce string 随机串,对应URL参数的nonce
|
|
||||||
* @param $postData string 密文,对应POST请求的数据
|
|
||||||
* @param &$msg string 解密后的原文,当return返回0时有效
|
|
||||||
*
|
|
||||||
* @return int 成功0,失败返回对应的错误码
|
|
||||||
*/
|
|
||||||
public function decryptMsg($msgSignature, $timestamp, $nonce, $postData, &$msg)
|
|
||||||
{
|
|
||||||
if (strlen($this->config->getEncodingAesKey()) != 43) {
|
|
||||||
return self::IllegalAesKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
$pc = new Prpcrypt($this->config->getEncodingAesKey());
|
|
||||||
|
|
||||||
//提取密文
|
|
||||||
$xmlparse = new XMLParse();
|
|
||||||
$array = $xmlparse->extract($postData);
|
|
||||||
$ret = $array[0];
|
|
||||||
|
|
||||||
if ($ret != 0) {
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($timestamp == null) {
|
|
||||||
$timestamp = time();
|
|
||||||
}
|
|
||||||
|
|
||||||
$encrypt = $array[1];
|
|
||||||
$touser_name = $array[2];
|
|
||||||
|
|
||||||
//验证安全签名
|
|
||||||
$array = $this->getSHA1($this->config->getToken(), $timestamp, $nonce, $encrypt);
|
|
||||||
$ret = $array[0];
|
|
||||||
|
|
||||||
if ($ret != 0) {
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
$signature = $array[1];
|
|
||||||
if ($signature != $msgSignature) {
|
|
||||||
return self::ValidateSignatureError;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = $pc->decrypt($encrypt, $this->config->getAppid());
|
|
||||||
if ($result[0] != 0) {
|
|
||||||
return $result[0];
|
|
||||||
}
|
|
||||||
$msg = $result[1];
|
|
||||||
|
|
||||||
return self::OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $token
|
|
||||||
* @param $timestamp
|
|
||||||
* @param $nonce
|
|
||||||
* @param $encrypt_msg
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function getSHA1($token, $timestamp, $nonce, $encrypt_msg)
|
|
||||||
{
|
|
||||||
//排序
|
|
||||||
try {
|
|
||||||
$array = array($encrypt_msg, $token, $timestamp, $nonce);
|
|
||||||
sort($array, SORT_STRING);
|
|
||||||
$str = implode($array);
|
|
||||||
return array(self::OK, sha1($str));
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
//print $e . "\n";
|
|
||||||
return array(self::ComputeSignatureError, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\common\Result;
|
|
||||||
|
|
||||||
class QrCode extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
private $tick_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
|
||||||
private $temporary_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
|
|
||||||
private $permanent_qr_code_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $scene_id
|
|
||||||
* @return mixed
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function temporaryQrCode($scene_id)
|
|
||||||
{
|
|
||||||
$requestParam['expire_seconds'] = 24 * 3600 * 30;
|
|
||||||
$requestParam['action_name'] = 'QR_SCENE';
|
|
||||||
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
|
||||||
|
|
||||||
$tick = $this->get_tick($requestParam);
|
|
||||||
|
|
||||||
$client = HttpClient::NewRequest();
|
|
||||||
return $client->get($this->temporary_url, ['ticket' => $tick]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $scene_id
|
|
||||||
* @return array|mixed|\common\Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function permanent_qr_code($scene_id)
|
|
||||||
{
|
|
||||||
$requestParam['action_name'] = 'QR_LIMIT_SCENE';
|
|
||||||
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
|
||||||
|
|
||||||
$tick = $this->get_tick($requestParam);
|
|
||||||
|
|
||||||
$client = HttpClient::NewRequest();
|
|
||||||
return $client->get($this->permanent_qr_code_url, ['ticket' => $tick]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $requestParam
|
|
||||||
* @return mixed
|
|
||||||
* @throws \Exception
|
|
||||||
* tick
|
|
||||||
*/
|
|
||||||
private function get_tick($requestParam)
|
|
||||||
{
|
|
||||||
$request = HttpClient::NewRequest();
|
|
||||||
$request->setCallback(function ($body) {
|
|
||||||
if (is_array($body)) {
|
|
||||||
$param = ['code' => $body['errcode'], 'message' => $body['errmsg'], 'data' => $body];
|
|
||||||
} else {
|
|
||||||
$param = ['code' => 0, 'data' => json_decode($body, true)];
|
|
||||||
}
|
|
||||||
return new Result($param);
|
|
||||||
});
|
|
||||||
$data = $request->post($this->tick_url . $this->config->getAccessToken(), json_encode($requestParam));
|
|
||||||
if (!$data->isResultsOK()) {
|
|
||||||
throw new \Exception($data->getMessage());
|
|
||||||
}
|
|
||||||
return $data->getData('ticket');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\common\Result;
|
|
||||||
|
|
||||||
class SourceMaterial extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
private $uploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/upload?';
|
|
||||||
private $getUploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/get';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $file
|
|
||||||
* @return bool|mixed|Result
|
|
||||||
*/
|
|
||||||
public function ImageUpload($file)
|
|
||||||
{
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
return $this->sendError('文件不存在.', 404);
|
|
||||||
}
|
|
||||||
return $this->upload($file, 'image');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $file
|
|
||||||
* @return bool|mixed|Result
|
|
||||||
*/
|
|
||||||
public function VoiceUpload($file)
|
|
||||||
{
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
return $this->sendError('文件不存在.', 404);
|
|
||||||
}
|
|
||||||
return $this->upload($file, 'voice');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $file
|
|
||||||
* @return bool|mixed|Result
|
|
||||||
*/
|
|
||||||
public function VideoUpload($file)
|
|
||||||
{
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
return $this->sendError('文件不存在.', 404);
|
|
||||||
}
|
|
||||||
return $this->upload($file, 'video');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $file
|
|
||||||
* @return bool|mixed|Result
|
|
||||||
*/
|
|
||||||
public function ThumbUpload($file)
|
|
||||||
{
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
return $this->sendError('文件不存在.', 404);
|
|
||||||
}
|
|
||||||
return $this->upload($file, 'thumb');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $media_id
|
|
||||||
* @return array|mixed|Result
|
|
||||||
*/
|
|
||||||
public function MediaGet($media_id)
|
|
||||||
{
|
|
||||||
$client = HttpClient::NewRequest();
|
|
||||||
|
|
||||||
$accessToken = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
return $client->get($this->getUploadUrl, ['access_token' => $accessToken, 'media_id' => $media_id]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $file
|
|
||||||
* @param $type
|
|
||||||
* @return bool|mixed
|
|
||||||
*/
|
|
||||||
private function upload($file, $type)
|
|
||||||
{
|
|
||||||
$uploadInfo['media'] = new \CURLFile($file);
|
|
||||||
$uploadInfo['form-data[filename]'] = $uploadInfo['media']->getFilename();
|
|
||||||
$uploadInfo['form-data[Content-Type]'] = $uploadInfo['media']->getMimeType();
|
|
||||||
|
|
||||||
$accessToken = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
$url = $this->uploadUrl . 'access_token=' . $accessToken . '&type=' . $type;
|
|
||||||
$result = $this->request->post($url, $uploadInfo);
|
|
||||||
if (!$result->isResultsOK()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $result->getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
|
|
||||||
class Subscribe extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
private $openid = '';
|
|
||||||
private $template_id = '';
|
|
||||||
private $url = '';
|
|
||||||
private $miniprogram = [];
|
|
||||||
private $scene = '';
|
|
||||||
private $title = '';
|
|
||||||
private $keywords = [];
|
|
||||||
|
|
||||||
private $pushUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $openid
|
|
||||||
* @return Subscribe
|
|
||||||
*/
|
|
||||||
public function setOpenid(string $openid): Subscribe
|
|
||||||
{
|
|
||||||
$this->openid = $openid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $template_id
|
|
||||||
* @return Subscribe
|
|
||||||
*/
|
|
||||||
public function setTemplateId(string $template_id): Subscribe
|
|
||||||
{
|
|
||||||
$this->template_id = $template_id;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $url
|
|
||||||
* @return Subscribe
|
|
||||||
*/
|
|
||||||
public function setUrl(string $url): Subscribe
|
|
||||||
{
|
|
||||||
$this->url = $url;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $appid
|
|
||||||
* @param string $path
|
|
||||||
* @return Subscribe
|
|
||||||
*/
|
|
||||||
public function setMiniprogram(string $appid, string $path): Subscribe
|
|
||||||
{
|
|
||||||
$this->miniprogram['appid'] = $appid;
|
|
||||||
$this->miniprogram['pagepath'] = $path;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $scene
|
|
||||||
* @return Subscribe
|
|
||||||
*/
|
|
||||||
public function setScene(string $scene): Subscribe
|
|
||||||
{
|
|
||||||
$this->scene = $scene;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $title
|
|
||||||
* @return Subscribe
|
|
||||||
*/
|
|
||||||
public function setTitle(string $title): Subscribe
|
|
||||||
{
|
|
||||||
$this->title = $title;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $keyword
|
|
||||||
* @param $content
|
|
||||||
* @param null $color
|
|
||||||
*/
|
|
||||||
public function addContent($keyword, $content, $color = null)
|
|
||||||
{
|
|
||||||
$param['value'] = $content;
|
|
||||||
if (!empty($color)) {
|
|
||||||
$param['color'] = $color;
|
|
||||||
}
|
|
||||||
$this->keywords[$keyword] = $param;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array|mixed|\wchat\common\Result
|
|
||||||
*/
|
|
||||||
public function sendSubscribeMessage()
|
|
||||||
{
|
|
||||||
$requestParam['touser'] = $this->openid;
|
|
||||||
$requestParam['template_id'] = $this->template_id;
|
|
||||||
$requestParam['url'] = $this->url;
|
|
||||||
$requestParam['miniprogram'] = $this->miniprogram;
|
|
||||||
$requestParam['scene'] = $this->scene;
|
|
||||||
$requestParam['title'] = $this->title;
|
|
||||||
$requestParam['data'] = $this->keywords;
|
|
||||||
|
|
||||||
$requestUrl = $this->pushUrl . $this->config->getAccessToken();
|
|
||||||
|
|
||||||
$client = HttpClient::NewRequest();
|
|
||||||
return $client->post($requestUrl, $requestParam);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\Result;
|
|
||||||
|
|
||||||
class Tag extends AfficialAccount
|
|
||||||
{
|
|
||||||
|
|
||||||
private $generate_url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=';
|
|
||||||
private $tag_lists = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token=';
|
|
||||||
private $tag_edit = 'https://api.weixin.qq.com/cgi-bin/tags/update?access_token=';
|
|
||||||
private $tag_delete = 'https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=';
|
|
||||||
private $tag_fans_list = 'https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=';
|
|
||||||
private $user_batch_add_tag = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=';
|
|
||||||
private $user_batch_remove_tag = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=';
|
|
||||||
private $user_tag = 'https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $tagName
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function generate($tagName)
|
|
||||||
{
|
|
||||||
$params['tag'] = ['name' => $tagName];
|
|
||||||
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
|
|
||||||
$this->request->setData(json_encode($params, JSON_UNESCAPED_UNICODE));
|
|
||||||
return $this->request->post($this->generate_url . $config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @param $tagName
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function tag_edit($id, $tagName)
|
|
||||||
{
|
|
||||||
$url['tag[id]'] = $id;
|
|
||||||
$url['tag[name]'] = $tagName;
|
|
||||||
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
|
|
||||||
return $this->request->post($this->tag_edit . $config, $url);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function tag_delete($id)
|
|
||||||
{
|
|
||||||
$url['tag[id]'] = $id;
|
|
||||||
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
return $this->request->post($this->tag_delete . $config, $url);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @param string[] $openid_list
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function user_batch_add_tag($id, array $openid_list)
|
|
||||||
{
|
|
||||||
$url['tagid'] = $id;
|
|
||||||
$url['openid_list'] = $openid_list;
|
|
||||||
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
$this->request->setData(json_encode($url, JSON_UNESCAPED_UNICODE));
|
|
||||||
return $this->request->post($this->user_batch_add_tag . $config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @param string[] $openid_list
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function user_batch_remove_tag($id, array $openid_list)
|
|
||||||
{
|
|
||||||
$url['tagid'] = $id;
|
|
||||||
$url['openid_list'] = $openid_list;
|
|
||||||
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
return $this->request->post($this->user_batch_remove_tag . $config, $url);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function tagList()
|
|
||||||
{
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
return $this->request->get($this->tag_lists . $config, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function tag_fans_list()
|
|
||||||
{
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
return $this->request->get($this->tag_fans_list . $config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $openid
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function user_tag($openid)
|
|
||||||
{
|
|
||||||
$config = $this->config->getAccessToken();
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
return $this->request->post($this->user_tag . $config, [
|
|
||||||
'openid' => $openid
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\Config;
|
|
||||||
|
|
||||||
class WxSDK
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function authorization(Config $config)
|
|
||||||
{
|
|
||||||
return Authorization::getInstance($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function qrCode(Config $config)
|
|
||||||
{
|
|
||||||
return QrCode::getInstance($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function news(Config $config)
|
|
||||||
{
|
|
||||||
return NewsManager::getInstance($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,360 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace wchat\officialaccount\dcaler;
|
|
||||||
|
|
||||||
|
|
||||||
class Authorization
|
|
||||||
{
|
|
||||||
private $subscribe;
|
|
||||||
private $openid;
|
|
||||||
private $nickname;
|
|
||||||
private $sex;
|
|
||||||
private $language;
|
|
||||||
private $city;
|
|
||||||
private $province;
|
|
||||||
private $country;
|
|
||||||
private $headimgurl;
|
|
||||||
private $subscribe_time;
|
|
||||||
private $unionid;
|
|
||||||
private $remark;
|
|
||||||
private $groupid;
|
|
||||||
private $tagid_list;
|
|
||||||
private $subscribe_scene;
|
|
||||||
private $qr_scene;
|
|
||||||
private $qr_scene_str;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $object
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public static function instance($object)
|
|
||||||
{
|
|
||||||
$class = new Authorization();
|
|
||||||
$class->setSubscribe($object['subscribe']);
|
|
||||||
$class->setOpenid($object['openid']);
|
|
||||||
$class->setNickname($object['nickname']);
|
|
||||||
$class->setSex($object['sex']);
|
|
||||||
$class->setLanguage($object['language']);
|
|
||||||
$class->setCity($object['city']);
|
|
||||||
$class->setProvince($object['province']);
|
|
||||||
$class->setCountry($object['country']);
|
|
||||||
$class->setHeadimgurl($object['headimgurl']);
|
|
||||||
$class->setSubscribeTime($object['subscribe_time']);
|
|
||||||
$class->setUnionid($object['unionid']);
|
|
||||||
$class->setRemark($object['remark']);
|
|
||||||
$class->setGroupid($object['groupid']);
|
|
||||||
$class->setTagidList($object['tagid_list']);
|
|
||||||
$class->setSubscribeScene($object['subscribe_scene']);
|
|
||||||
$class->setQrScene($object['qr_scene']);
|
|
||||||
$class->setQrSceneStr($object['qr_scene_str']);
|
|
||||||
return $class;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSubscribe()
|
|
||||||
{
|
|
||||||
return $this->subscribe;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $subscribe
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setSubscribe($subscribe)
|
|
||||||
{
|
|
||||||
$this->subscribe = $subscribe;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getOpenid()
|
|
||||||
{
|
|
||||||
return $this->openid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $openid
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setOpenid($openid)
|
|
||||||
{
|
|
||||||
$this->openid = $openid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getNickname()
|
|
||||||
{
|
|
||||||
return $this->nickname;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $nickname
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setNickname($nickname)
|
|
||||||
{
|
|
||||||
$this->nickname = $nickname;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSex()
|
|
||||||
{
|
|
||||||
return $this->sex;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $sex
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setSex($sex)
|
|
||||||
{
|
|
||||||
$this->sex = $sex;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getLanguage()
|
|
||||||
{
|
|
||||||
return $this->language;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $language
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setLanguage($language)
|
|
||||||
{
|
|
||||||
$this->language = $language;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCity()
|
|
||||||
{
|
|
||||||
return $this->city;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $city
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setCity($city)
|
|
||||||
{
|
|
||||||
$this->city = $city;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getProvince()
|
|
||||||
{
|
|
||||||
return $this->province;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $province
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setProvince($province)
|
|
||||||
{
|
|
||||||
$this->province = $province;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCountry()
|
|
||||||
{
|
|
||||||
return $this->country;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $country
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setCountry($country)
|
|
||||||
{
|
|
||||||
$this->country = $country;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getHeadimgurl()
|
|
||||||
{
|
|
||||||
return $this->headimgurl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $headimgurl
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setHeadimgurl($headimgurl)
|
|
||||||
{
|
|
||||||
$this->headimgurl = $headimgurl;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSubscribeTime()
|
|
||||||
{
|
|
||||||
return $this->subscribe_time;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $subscribe_time
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setSubscribeTime($subscribe_time)
|
|
||||||
{
|
|
||||||
$this->subscribe_time = $subscribe_time;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getUnionid()
|
|
||||||
{
|
|
||||||
return $this->unionid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $unionid
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setUnionid($unionid)
|
|
||||||
{
|
|
||||||
$this->unionid = $unionid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getRemark()
|
|
||||||
{
|
|
||||||
return $this->remark;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $remark
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setRemark($remark)
|
|
||||||
{
|
|
||||||
$this->remark = $remark;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getGroupid()
|
|
||||||
{
|
|
||||||
return $this->groupid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $groupid
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setGroupid($groupid)
|
|
||||||
{
|
|
||||||
$this->groupid = $groupid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getTagidList()
|
|
||||||
{
|
|
||||||
return $this->tagid_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $tagid_list
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setTagidList($tagid_list)
|
|
||||||
{
|
|
||||||
$this->tagid_list = $tagid_list;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSubscribeScene()
|
|
||||||
{
|
|
||||||
return $this->subscribe_scene;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $subscribe_scene
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setSubscribeScene($subscribe_scene)
|
|
||||||
{
|
|
||||||
$this->subscribe_scene = $subscribe_scene;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getQrScene()
|
|
||||||
{
|
|
||||||
return $this->qr_scene;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $qr_scene
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setQrScene($qr_scene)
|
|
||||||
{
|
|
||||||
$this->qr_scene = $qr_scene;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getQrSceneStr()
|
|
||||||
{
|
|
||||||
return $this->qr_scene_str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $qr_scene_str
|
|
||||||
* @return Authorization
|
|
||||||
*/
|
|
||||||
public function setQrSceneStr($qr_scene_str)
|
|
||||||
{
|
|
||||||
$this->qr_scene_str = $qr_scene_str;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount\dcaler;
|
|
||||||
|
|
||||||
|
|
||||||
class PKCS7Encoder
|
|
||||||
{
|
|
||||||
public static $block_size = 32;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $text
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
function encode($text)
|
|
||||||
{
|
|
||||||
$block_size = PKCS7Encoder::$block_size;
|
|
||||||
$text_length = strlen($text);
|
|
||||||
//计算需要填充的位数
|
|
||||||
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
|
|
||||||
if ($amount_to_pad == 0) {
|
|
||||||
$amount_to_pad = PKCS7Encoder::$block_size;
|
|
||||||
}
|
|
||||||
//获得补位所用的字符
|
|
||||||
$pad_chr = chr($amount_to_pad);
|
|
||||||
$tmp = "";
|
|
||||||
for ($index = 0; $index < $amount_to_pad; $index++) {
|
|
||||||
$tmp .= $pad_chr;
|
|
||||||
}
|
|
||||||
return $text . $tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $text
|
|
||||||
* @return false|string
|
|
||||||
*/
|
|
||||||
function decode($text)
|
|
||||||
{
|
|
||||||
|
|
||||||
$pad = ord(substr($text, -1));
|
|
||||||
if ($pad < 1 || $pad > 32) {
|
|
||||||
$pad = 0;
|
|
||||||
}
|
|
||||||
return substr($text, 0, (strlen($text) - $pad));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount\dcaler;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\officialaccount\NewsManager;
|
|
||||||
|
|
||||||
class Prpcrypt
|
|
||||||
{
|
|
||||||
public $key;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prpcrypt constructor.
|
|
||||||
* @param $k
|
|
||||||
*/
|
|
||||||
public function __construct($k)
|
|
||||||
{
|
|
||||||
$this->key = base64_decode($k . "=");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $text
|
|
||||||
* @param $appid
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function encrypt($text, $appid)
|
|
||||||
{
|
|
||||||
|
|
||||||
try {
|
|
||||||
//获得16位随机字符串,填充到明文之前
|
|
||||||
$random = $this->getRandomStr();
|
|
||||||
$text = $random . pack("N", strlen($text)) . $text . $appid;
|
|
||||||
// 网络字节序
|
|
||||||
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
|
||||||
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
||||||
$iv = substr($this->key, 0, 16);
|
|
||||||
//使用自定义的填充方式对明文进行补位填充
|
|
||||||
$pkc_encoder = new PKCS7Encoder;
|
|
||||||
$text = $pkc_encoder->encode($text);
|
|
||||||
mcrypt_generic_init($module, $this->key, $iv);
|
|
||||||
//加密
|
|
||||||
$encrypted = mcrypt_generic($module, $text);
|
|
||||||
mcrypt_generic_deinit($module);
|
|
||||||
mcrypt_module_close($module);
|
|
||||||
|
|
||||||
//print(base64_encode($encrypted));
|
|
||||||
//使用BASE64对加密后的字符串进行编码
|
|
||||||
return array(NewsManager::OK, base64_encode($encrypted));
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
//print $e;
|
|
||||||
return array(NewsManager::EncryptAESError, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $encrypted
|
|
||||||
* @param $appid
|
|
||||||
* @return array|string
|
|
||||||
*/
|
|
||||||
public function decrypt($encrypted, $appid)
|
|
||||||
{
|
|
||||||
|
|
||||||
try {
|
|
||||||
//使用BASE64对需要解密的字符串进行解码
|
|
||||||
$ciphertext_dec = base64_decode($encrypted);
|
|
||||||
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
||||||
$iv = substr($this->key, 0, 16);
|
|
||||||
mcrypt_generic_init($module, $this->key, $iv);
|
|
||||||
|
|
||||||
//解密
|
|
||||||
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
|
||||||
mcrypt_generic_deinit($module);
|
|
||||||
mcrypt_module_close($module);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return array(NewsManager::DecryptAESError, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
//去除补位字符
|
|
||||||
$pkc_encoder = new PKCS7Encoder;
|
|
||||||
$result = $pkc_encoder->decode($decrypted);
|
|
||||||
//去除16位随机字符串,网络字节序和AppId
|
|
||||||
if (strlen($result) < 16)
|
|
||||||
return "";
|
|
||||||
$content = substr($result, 16, strlen($result));
|
|
||||||
$len_list = unpack("N", substr($content, 0, 4));
|
|
||||||
$xml_len = $len_list[1];
|
|
||||||
$xml_content = substr($content, 4, $xml_len);
|
|
||||||
$from_appid = substr($content, $xml_len + 4);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
//print $e;
|
|
||||||
return array(NewsManager::IllegalBuffer, null);
|
|
||||||
}
|
|
||||||
if ($from_appid != $appid)
|
|
||||||
return array(NewsManager::ValidateAppidError, null);
|
|
||||||
return array(0, $xml_content);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 随机生成16位字符串
|
|
||||||
* @return string 生成的字符串
|
|
||||||
*/
|
|
||||||
function getRandomStr()
|
|
||||||
{
|
|
||||||
|
|
||||||
$str = "";
|
|
||||||
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
|
|
||||||
$max = strlen($str_pol) - 1;
|
|
||||||
for ($i = 0; $i < 16; $i++) {
|
|
||||||
$str .= $str_pol[mt_rand(0, $max)];
|
|
||||||
}
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,204 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount\dcaler;
|
|
||||||
|
|
||||||
|
|
||||||
class SnsInfo
|
|
||||||
{
|
|
||||||
|
|
||||||
private $openid;
|
|
||||||
private $nickname;
|
|
||||||
private $sex;
|
|
||||||
private $province;
|
|
||||||
private $city;
|
|
||||||
private $country;
|
|
||||||
private $headimgurl;
|
|
||||||
private $privilege;
|
|
||||||
private $unionid;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getOpenid()
|
|
||||||
{
|
|
||||||
return $this->openid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $openid
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setOpenid($openid)
|
|
||||||
{
|
|
||||||
$this->openid = $openid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getNickname()
|
|
||||||
{
|
|
||||||
return $this->nickname;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $nickname
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setNickname($nickname)
|
|
||||||
{
|
|
||||||
$this->nickname = $nickname;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSex()
|
|
||||||
{
|
|
||||||
return $this->sex;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $sex
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setSex($sex)
|
|
||||||
{
|
|
||||||
$this->sex = $sex;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getProvince()
|
|
||||||
{
|
|
||||||
return $this->province;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $province
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setProvince($province)
|
|
||||||
{
|
|
||||||
$this->province = $province;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCity()
|
|
||||||
{
|
|
||||||
return $this->city;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $city
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setCity($city)
|
|
||||||
{
|
|
||||||
$this->city = $city;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCountry()
|
|
||||||
{
|
|
||||||
return $this->country;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $country
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setCountry($country)
|
|
||||||
{
|
|
||||||
$this->country = $country;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getHeadimgurl()
|
|
||||||
{
|
|
||||||
return $this->headimgurl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $headimgurl
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setHeadimgurl($headimgurl)
|
|
||||||
{
|
|
||||||
$this->headimgurl = $headimgurl;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getPrivilege()
|
|
||||||
{
|
|
||||||
return $this->privilege;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $privilege
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setPrivilege($privilege)
|
|
||||||
{
|
|
||||||
$this->privilege = $privilege;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getUnionid()
|
|
||||||
{
|
|
||||||
return $this->unionid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $unionid
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public function setUnionid($unionid)
|
|
||||||
{
|
|
||||||
$this->unionid = $unionid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $object
|
|
||||||
* @return SnsInfo
|
|
||||||
*/
|
|
||||||
public static function instance($object)
|
|
||||||
{
|
|
||||||
$class = new SnsInfo();
|
|
||||||
$class->setOpenid($object['openid']);
|
|
||||||
$class->setNickname($object['nickname']);
|
|
||||||
$class->setSex($object['sex']);
|
|
||||||
$class->setCity($object['city']);
|
|
||||||
$class->setProvince($object['province']);
|
|
||||||
$class->setCountry($object['country']);
|
|
||||||
$class->setHeadimgurl($object['headimgurl']);
|
|
||||||
$class->setUnionid($object['unionid']);
|
|
||||||
$class->setPrivilege($object['privilege']);
|
|
||||||
return $class;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\officialaccount\dcaler;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\officialaccount\NewsManager;
|
|
||||||
|
|
||||||
class XMLParse
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* 提取出xml数据包中的加密消息
|
|
||||||
* @param string $xmltext 待提取的xml字符串
|
|
||||||
* @return array 提取出的加密消息字符串
|
|
||||||
*/
|
|
||||||
public function extract($xmltext)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$xml = new \DOMDocument();
|
|
||||||
$xml->loadXML($xmltext);
|
|
||||||
$array_e = $xml->getElementsByTagName('Encrypt');
|
|
||||||
$array_a = $xml->getElementsByTagName('ToUserName');
|
|
||||||
$encrypt = $array_e->item(0)->nodeValue;
|
|
||||||
$tousername = $array_a->item(0)->nodeValue;
|
|
||||||
return [0, $encrypt, $tousername];
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return [NewsManager::ParseXmlError, null, null];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $encrypt
|
|
||||||
* @param $signature
|
|
||||||
* @param $timestamp
|
|
||||||
* @param $nonce
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function generate($encrypt, $signature, $timestamp, $nonce)
|
|
||||||
{
|
|
||||||
$format = "<xml>
|
|
||||||
<Encrypt><![CDATA[%s]]></Encrypt>
|
|
||||||
<MsgSignature><![CDATA[%s]]></MsgSignature>
|
|
||||||
<TimeStamp>%s</TimeStamp>
|
|
||||||
<Nonce><![CDATA[%s]]></Nonce>
|
|
||||||
</xml>";
|
|
||||||
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+9
-37
@@ -8,57 +8,29 @@
|
|||||||
|
|
||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
use wchat\common\Decode;
|
use Kiri\Client;
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
class Account extends SmallProgram
|
class Account extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $code
|
* @param string $code
|
||||||
* @return Result
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function login($code)
|
public function login(string $code): Result
|
||||||
{
|
{
|
||||||
$param['appid'] = $this->config->getAppid();
|
$param['appid'] = $this->config->getAppid();
|
||||||
$param['secret'] = $this->config->getAppsecret();
|
$param['secret'] = $this->config->getAppsecret();
|
||||||
$param['js_code'] = $code;
|
$param['js_code'] = $code;
|
||||||
$param['grant_type'] = 'authorization_code';
|
$param['grant_type'] = 'authorization_code';
|
||||||
|
|
||||||
if (empty($code)) {
|
$client = new Client('api.q.qq.com', 443, true);
|
||||||
return new Result(['code' => 404, 'message' => '临时登录凭证不能为空.']);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
}
|
$client->get('sns/jscode2session', $param);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
$this->request->setHost('api.q.qq.com');
|
return Result::init($client);
|
||||||
$this->request->setMethod(HttpClient::GET);
|
|
||||||
$this->request->setIsSSL(true);
|
|
||||||
$this->request->setUseSwoole($this->config->isUsrSwoole());
|
|
||||||
|
|
||||||
return $this->request->get('sns/jscode2session', $param);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $encryptedData
|
|
||||||
* @param $iv
|
|
||||||
* @param $sessionKey
|
|
||||||
* @param $asArray
|
|
||||||
* @return object|array
|
|
||||||
* @throws
|
|
||||||
*
|
|
||||||
* * <li>-41001: encodingAesKey 非法</li>
|
|
||||||
* <li>-41003: aes 解密失败</li>
|
|
||||||
* <li>-41004: 解密后得到的buffer非法</li>
|
|
||||||
* <li>-41005: base64加密失败</li>
|
|
||||||
* <li>-41016: base64解密失败</li>
|
|
||||||
*/
|
|
||||||
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
|
||||||
{
|
|
||||||
$decode = new Decode();
|
|
||||||
$decode->setAppId($this->config->getAppid());
|
|
||||||
$decode->setIv($iv);
|
|
||||||
$decode->setEncryptedData($encryptedData);
|
|
||||||
$decode->setSessionKey($sessionKey);
|
|
||||||
return $decode->decode($asArray);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\qq;
|
|
||||||
|
|
||||||
|
|
||||||
class GamePrePay extends SmallProgram
|
|
||||||
{
|
|
||||||
|
|
||||||
private $url = 'https://api.q.qq.com/api/json/openApiPay/GamePrePay?access_token=';
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+37
-542
@@ -3,82 +3,72 @@
|
|||||||
|
|
||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use wchat\common\Help;
|
use wchat\common\Help;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Notify
|
||||||
|
* @package wchat\qq
|
||||||
|
*/
|
||||||
class Notify extends SmallProgram
|
class Notify extends SmallProgram
|
||||||
{
|
{
|
||||||
public $appid = '';
|
public mixed $appid = null;
|
||||||
public $mch_id = '';
|
public mixed $mch_id = null;
|
||||||
public $device_info = '';
|
public mixed $nonce_str = null;
|
||||||
public $nonce_str = '';
|
public mixed $sign = null;
|
||||||
public $sign = '';
|
public mixed $device_info = null;
|
||||||
public $sign_type = '';
|
public mixed $trade_type = null;
|
||||||
public $result_code = '';
|
public mixed $trade_state = null;
|
||||||
public $err_code = '';
|
public mixed $bank_type = null;
|
||||||
public $err_code_des = '';
|
public mixed $fee_type = null;
|
||||||
public $openid = '';
|
public mixed $total_fee = null;
|
||||||
public $is_subscribe = '';
|
public mixed $cash_fee = null;
|
||||||
public $trade_type = '';
|
public mixed $coupon_fee = null;
|
||||||
public $bank_type = '';
|
public mixed $transaction_id = null;
|
||||||
public $total_fee = '';
|
public mixed $out_trade_no = null;
|
||||||
public $settlement_total_fee = '';
|
public mixed $attach = null;
|
||||||
public $fee_type = '';
|
public mixed $time_end = null;
|
||||||
public $cash_fee = '';
|
public mixed $openid = null;
|
||||||
public $cash_fee_type = '';
|
|
||||||
public $coupon_fee = '';
|
|
||||||
public $coupon_count = '';
|
|
||||||
public $coupon_type_n = '';
|
|
||||||
public $coupon_id_n = '';
|
|
||||||
public $coupon_fee_n = '';
|
|
||||||
public $transaction_id = '';
|
|
||||||
public $out_trade_no = '';
|
|
||||||
public $attach = '';
|
|
||||||
public $time_end = '';
|
|
||||||
public $return_code = '';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* 判断是否完成支付
|
* 判断是否完成支付
|
||||||
*/
|
*/
|
||||||
public function isSuccess()
|
public function isSuccess(): bool
|
||||||
{
|
{
|
||||||
if ($this->getReturnCode() != "SUCCESS") {
|
return $this->trade_state === 'SUCCESS';
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->getResultCode() != 'SUCCESS') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return $this
|
* @return $this
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setPayNotifyData(array $params)
|
public function setPayNotifyData(array $params): static
|
||||||
{
|
{
|
||||||
if (!$this->validation($params)) {
|
if (!$this->validation($params)) {
|
||||||
$this->setResultCode('FAIL');
|
throw new Exception('签名错误!');
|
||||||
$this->setErrCodeDes('签名错误');
|
|
||||||
unset($params['result_code'], $params['err_code_des']);
|
|
||||||
}
|
}
|
||||||
foreach ($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
if (!property_exists($this, $key)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->$key = $val;
|
$this->$key = $val;
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function __set(string $name, $value): void
|
||||||
|
{
|
||||||
|
if (property_exists($this, $name)) {
|
||||||
|
$this->{$name} = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validation(array $params)
|
public function validation(array $params): bool
|
||||||
{
|
{
|
||||||
$sign = $params['sign'];
|
$sign = $params['sign'];
|
||||||
unset($params['sign']);
|
unset($params['sign']);
|
||||||
@@ -93,507 +83,12 @@ class Notify extends SmallProgram
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return null
|
||||||
*/
|
*/
|
||||||
public function getAppid()
|
public function getAppid()
|
||||||
{
|
{
|
||||||
return $this->appid;
|
return $this->appid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $appid
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setAppid($appid)
|
|
||||||
{
|
|
||||||
$this->appid = $appid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getMchId()
|
|
||||||
{
|
|
||||||
return $this->mch_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $mch_id
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setMchId($mch_id)
|
|
||||||
{
|
|
||||||
$this->mch_id = $mch_id;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getDeviceInfo()
|
|
||||||
{
|
|
||||||
return $this->device_info;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $device_info
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setDeviceInfo($device_info)
|
|
||||||
{
|
|
||||||
$this->device_info = $device_info;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getNonceStr()
|
|
||||||
{
|
|
||||||
return $this->nonce_str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $nonce_str
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setNonceStr($nonce_str)
|
|
||||||
{
|
|
||||||
$this->nonce_str = $nonce_str;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSign()
|
|
||||||
{
|
|
||||||
return $this->sign;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $sign
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setSign($sign)
|
|
||||||
{
|
|
||||||
$this->sign = $sign;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSignType()
|
|
||||||
{
|
|
||||||
return $this->sign_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $sign_type
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setSignType($sign_type)
|
|
||||||
{
|
|
||||||
$this->sign_type = $sign_type;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getResultCode()
|
|
||||||
{
|
|
||||||
return $this->result_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $result_code
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setResultCode($result_code)
|
|
||||||
{
|
|
||||||
$this->result_code = $result_code;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getErrCode()
|
|
||||||
{
|
|
||||||
return $this->err_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $err_code
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setErrCode($err_code)
|
|
||||||
{
|
|
||||||
$this->err_code = $err_code;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getErrCodeDes()
|
|
||||||
{
|
|
||||||
return $this->err_code_des;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $err_code_des
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setErrCodeDes($err_code_des)
|
|
||||||
{
|
|
||||||
$this->err_code_des = $err_code_des;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getOpenid()
|
|
||||||
{
|
|
||||||
return $this->openid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $openid
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setOpenid($openid)
|
|
||||||
{
|
|
||||||
$this->openid = $openid;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getIsSubscribe()
|
|
||||||
{
|
|
||||||
return $this->is_subscribe;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $is_subscribe
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setIsSubscribe($is_subscribe)
|
|
||||||
{
|
|
||||||
$this->is_subscribe = $is_subscribe;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getTradeType()
|
|
||||||
{
|
|
||||||
return $this->trade_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $trade_type
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setTradeType($trade_type)
|
|
||||||
{
|
|
||||||
$this->trade_type = $trade_type;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getBankType()
|
|
||||||
{
|
|
||||||
return $this->bank_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $bank_type
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setBankType($bank_type)
|
|
||||||
{
|
|
||||||
$this->bank_type = $bank_type;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getTotalFee()
|
|
||||||
{
|
|
||||||
return $this->total_fee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $total_fee
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setTotalFee($total_fee)
|
|
||||||
{
|
|
||||||
$this->total_fee = $total_fee;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSettlementTotalFee()
|
|
||||||
{
|
|
||||||
return $this->settlement_total_fee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $settlement_total_fee
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setSettlementTotalFee($settlement_total_fee)
|
|
||||||
{
|
|
||||||
$this->settlement_total_fee = $settlement_total_fee;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFeeType()
|
|
||||||
{
|
|
||||||
return $this->fee_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $fee_type
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setFeeType($fee_type)
|
|
||||||
{
|
|
||||||
$this->fee_type = $fee_type;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCashFee()
|
|
||||||
{
|
|
||||||
return $this->cash_fee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $cash_fee
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCashFee($cash_fee)
|
|
||||||
{
|
|
||||||
$this->cash_fee = $cash_fee;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCashFeeType()
|
|
||||||
{
|
|
||||||
return $this->cash_fee_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $cash_fee_type
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCashFeeType($cash_fee_type)
|
|
||||||
{
|
|
||||||
$this->cash_fee_type = $cash_fee_type;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCouponFee()
|
|
||||||
{
|
|
||||||
return $this->coupon_fee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $coupon_fee
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCouponFee($coupon_fee)
|
|
||||||
{
|
|
||||||
$this->coupon_fee = $coupon_fee;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCouponCount()
|
|
||||||
{
|
|
||||||
return $this->coupon_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $coupon_count
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCouponCount($coupon_count)
|
|
||||||
{
|
|
||||||
$this->coupon_count = $coupon_count;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCouponTypeN()
|
|
||||||
{
|
|
||||||
return $this->coupon_type_n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $coupon_type_n
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCouponTypeN($coupon_type_n)
|
|
||||||
{
|
|
||||||
$this->coupon_type_n = $coupon_type_n;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCouponIdN()
|
|
||||||
{
|
|
||||||
return $this->coupon_id_n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $coupon_id_n
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCouponIdN($coupon_id_n)
|
|
||||||
{
|
|
||||||
$this->coupon_id_n = $coupon_id_n;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getCouponFeeN()
|
|
||||||
{
|
|
||||||
return $this->coupon_fee_n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $coupon_fee_n
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setCouponFeeN($coupon_fee_n)
|
|
||||||
{
|
|
||||||
$this->coupon_fee_n = $coupon_fee_n;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getTransactionId()
|
|
||||||
{
|
|
||||||
return $this->transaction_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $transaction_id
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setTransactionId($transaction_id)
|
|
||||||
{
|
|
||||||
$this->transaction_id = $transaction_id;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getOutTradeNo()
|
|
||||||
{
|
|
||||||
return $this->out_trade_no;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $out_trade_no
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setOutTradeNo($out_trade_no)
|
|
||||||
{
|
|
||||||
$this->out_trade_no = $out_trade_no;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getAttach()
|
|
||||||
{
|
|
||||||
return $this->attach;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $attach
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setAttach($attach)
|
|
||||||
{
|
|
||||||
$this->attach = $attach;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getTimeEnd()
|
|
||||||
{
|
|
||||||
return $this->time_end;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $time_end
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setTimeEnd($time_end)
|
|
||||||
{
|
|
||||||
$this->time_end = $time_end;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getReturnCode()
|
|
||||||
{
|
|
||||||
return $this->return_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $return_code
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function setReturnCode($return_code)
|
|
||||||
{
|
|
||||||
$this->return_code = $return_code;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
use Kiri\Di\Container;
|
||||||
|
use ReflectionException;
|
||||||
|
use wchat\common\Config;
|
||||||
|
|
||||||
|
class QqFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $class
|
||||||
|
* @param Config $config
|
||||||
|
* @return mixed
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public static function get($class, Config $config): mixed
|
||||||
|
{
|
||||||
|
$container = Container::instance();
|
||||||
|
$object = $container->get($class);
|
||||||
|
$object->setConfig($config);
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat\qq;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\common\Result;
|
|
||||||
|
|
||||||
class QrCode extends SmallProgram
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
|
||||||
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
|
|
||||||
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
|
||||||
|
|
||||||
private $savePath = __DIR__ . '/../../Users/';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
*/
|
|
||||||
public function setSavePath($path)
|
|
||||||
{
|
|
||||||
$this->savePath = $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
* @param $width
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function createwxaqrcode($path, $width)
|
|
||||||
{
|
|
||||||
$url = $this->wxaqr . $this->getAccessToken();
|
|
||||||
|
|
||||||
$sendBody['path'] = $path;
|
|
||||||
$sendBody['width'] = $width;
|
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
|
||||||
|
|
||||||
return $this->request->post($url, $sendBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
* @param $width
|
|
||||||
* @param bool $is_hyaline
|
|
||||||
* @param bool $auto_color
|
|
||||||
* @param string $line_color
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function getwxacode($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
|
||||||
{
|
|
||||||
$sendBody['path'] = $path;
|
|
||||||
$sendBody['width'] = $width;
|
|
||||||
$sendBody['auto_color'] = $auto_color;
|
|
||||||
$sendBody['is_hyaline'] = $is_hyaline;
|
|
||||||
if ($auto_color) {
|
|
||||||
$sendBody['line_color'] = $line_color;
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = $this->getwxacode . $this->getAccessToken();
|
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
|
||||||
return $this->request->post($url, $sendBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
* @param $width
|
|
||||||
* @param bool $is_hyaline
|
|
||||||
* @param bool $auto_color
|
|
||||||
* @param string $line_color
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function getwxacodeunlimit($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
|
||||||
{
|
|
||||||
$sendBody['path'] = $path;
|
|
||||||
$sendBody['width'] = $width;
|
|
||||||
$sendBody['auto_color'] = $auto_color;
|
|
||||||
$sendBody['is_hyaline'] = $is_hyaline;
|
|
||||||
if ($auto_color) {
|
|
||||||
$sendBody['line_color'] = $line_color;
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = $this->getwxacodeunlimit . $this->getAccessToken();
|
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
|
||||||
return $this->request->post($url, $sendBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $body
|
|
||||||
* @return string
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function saveByPath($body)
|
|
||||||
{
|
|
||||||
if (!is_null($json = json_decode($body))) {
|
|
||||||
throw new \Exception($json['errmsg'], $json['errcode']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$push = md5_file($body) . '.png';
|
|
||||||
file_put_contents($this->savePath . $push, $this->savePath);
|
|
||||||
return $this->savePath . $push;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+85
-86
@@ -9,108 +9,107 @@
|
|||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use wchat\common\HttpClient;
|
use Kiri\Client;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
use wchat\common\Help;
|
use wchat\common\Help;
|
||||||
|
|
||||||
class Recharge extends SmallProgram
|
class Recharge extends SmallProgram
|
||||||
{
|
{
|
||||||
private $money = 0;
|
private float $money = 0;
|
||||||
|
|
||||||
private $orderNo;
|
private string $orderNo;
|
||||||
|
|
||||||
private $data = [];
|
private array $data = [];
|
||||||
|
|
||||||
private $spill_create_ip = '';
|
private string $spill_create_ip = '';
|
||||||
|
|
||||||
private $uniformer = 'https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi';
|
private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setSpillCreateIp(string $value)
|
public function setSpillCreateIp($value)
|
||||||
{
|
{
|
||||||
$this->spill_create_ip = $value;
|
$this->spill_create_ip = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $money
|
* @param int $money
|
||||||
* @param string $orderNo
|
* @param string $orderNo
|
||||||
* @param string $openId
|
* @param string $openId
|
||||||
* @return array|mixed|Result
|
* @return Result
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function recharge(int $money, string $orderNo, $openId = '')
|
public function recharge(int $money, string $orderNo, string $openId = ''): Result
|
||||||
{
|
{
|
||||||
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;
|
||||||
|
|
||||||
$this->request->setCallback(function ($result, $body) {
|
$client = new Client('qpay.qq.com', 443, true);
|
||||||
$data = Help::toArray($result);
|
$client->withHeader(['Content-Type' => 'application/xml']);
|
||||||
if (isset($data['sign'])) {
|
$client->withBody($this->builder());
|
||||||
$sign = $data['sign'];
|
$client->post($this->uniformer);
|
||||||
unset($data['sign']);
|
$client->close();
|
||||||
}
|
|
||||||
if (!isset($sign)) {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = '返回数据签名验证失败';
|
|
||||||
} else {
|
|
||||||
$return['code'] = 0;
|
|
||||||
$return['data'] = $data;
|
|
||||||
$return['data']['postBody'] = $body;
|
|
||||||
if ($data['return_code'] == 'FAIL') {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = $data['return_msg'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Result($return);
|
|
||||||
});
|
|
||||||
return $this->send($this->uniformer, $this->builder());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
* @return string
|
return new Result(code: 505, message: 'network error.');
|
||||||
*/
|
}
|
||||||
protected function builder()
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'mch_id' => $this->config->getMchId(),
|
|
||||||
'nonce_str' => Help::random(32),
|
|
||||||
'body' => $this->config->getBody(),
|
|
||||||
'out_trade_no' => $this->orderNo,
|
|
||||||
'total_fee' => $this->money,
|
|
||||||
'spbill_create_ip' => $this->spill_create_ip,
|
|
||||||
'notify_url' => $this->config->getNotifyUrl(),
|
|
||||||
'trade_type' => $this->config->getTradeType(),
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->data = array_merge($data, $this->data);
|
$data = Help::toArray($client->getBody());
|
||||||
|
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 503, message: $data['return_msg']);
|
||||||
|
}
|
||||||
|
if ($data['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 504, message: $data['err_code_des']);
|
||||||
|
}
|
||||||
|
|
||||||
$key = $this->config->getKey();
|
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||||
$sign_type = $this->config->getSignType();
|
}
|
||||||
|
|
||||||
$this->data['sign_type'] = $this->config->getSignType();
|
|
||||||
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
|
|
||||||
|
|
||||||
return Help::toXml($this->data);
|
/**
|
||||||
}
|
* @param string $prepay_id
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function reception(string $prepay_id): string
|
||||||
|
{
|
||||||
|
return Help::sign([
|
||||||
|
'signType' => $this->config->getSignType(),
|
||||||
|
'package' => 'prepay_id=' . $prepay_id,
|
||||||
|
'nonceStr' => Help::random(32),
|
||||||
|
'timestamp' => time()
|
||||||
|
], $this->getConfig()->getKey(), $this->getConfig()->getSignType());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $url
|
* @return string
|
||||||
* @param $data
|
*/
|
||||||
* @return array|mixed|Result
|
protected function builder(): string
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
$data = [
|
||||||
private function send($url, $data)
|
'appid' => $this->config->getAppid(),
|
||||||
{
|
'mch_id' => $this->config->getMchId(),
|
||||||
$this->request->setIsSSL(true);
|
'nonce_str' => Help::random(32),
|
||||||
$this->request->setMethod(HttpClient::POST);
|
'body' => $this->config->getBody(),
|
||||||
$this->request->addHeader('Content-Type', 'text/xml');
|
'out_trade_no' => $this->orderNo,
|
||||||
return $this->request->send($url, $data);
|
'total_fee' => $this->money,
|
||||||
}
|
'spbill_create_ip' => $this->spill_create_ip,
|
||||||
|
'notify_url' => $this->config->getNotifyUrl(),
|
||||||
|
'trade_type' => $this->config->getTradeType(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->data = array_merge($data, $this->data);
|
||||||
|
|
||||||
|
$key = $this->config->getKey();
|
||||||
|
$sign_type = $this->config->getSignType();
|
||||||
|
|
||||||
|
$this->data['sign_type'] = $this->config->getSignType();
|
||||||
|
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
|
||||||
|
|
||||||
|
return Help::toXml($this->data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+140
-158
@@ -3,73 +3,37 @@
|
|||||||
|
|
||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Help;
|
use wchat\common\Help;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
use qq\result\RedHatResult;
|
|
||||||
|
|
||||||
class Redhat extends SmallProgram
|
class Redhat extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $charset = 'UTF8';
|
private string $charset = 'UTF8';
|
||||||
private $nonce_str = '';
|
private string $nonce_str = '';
|
||||||
private $sign = '';
|
private string $sign = '';
|
||||||
private $mch_billno = '';
|
private string $mch_billno = '';
|
||||||
private $mch_id = '';
|
private string $mch_id = '';
|
||||||
private $mch_name = '';
|
private string $mch_name = '';
|
||||||
private $qqappid = '';
|
private string $qqappid = '';
|
||||||
private $re_openid = '';
|
private string $re_openid = '';
|
||||||
private $total_amount = '';
|
private string $total_amount = '';
|
||||||
private $total_num = '';
|
private string $total_num = '';
|
||||||
private $wishing = '';
|
private string $wishing = '';
|
||||||
private $act_name = '';
|
private string $act_name = '';
|
||||||
private $icon_id = '';
|
private string $icon_id = '';
|
||||||
private $banner_id = '';
|
private string $banner_id = '';
|
||||||
private $notify_url = '';
|
private string $notify_url = '';
|
||||||
private $not_send_msg = '';
|
private string $not_send_msg = '';
|
||||||
private $min_value = '';
|
private string $min_value = '';
|
||||||
private $max_value = '';
|
private string $max_value = '';
|
||||||
private $key = '';
|
private string $key = '';
|
||||||
private $signType = '';
|
private string $signType = '';
|
||||||
|
|
||||||
|
|
||||||
private $sendUrl = 'https://api.qpay.qq.com/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
private string $sendUrl = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||||
private $searchUrl = 'https://qpay.qq.com/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
|
private string $searchUrl = '/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getKey(): string
|
|
||||||
{
|
|
||||||
return $this->key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @return Redhat
|
|
||||||
*/
|
|
||||||
public function setKey(string $key): Redhat
|
|
||||||
{
|
|
||||||
$this->key = $key;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSignType(): string
|
|
||||||
{
|
|
||||||
return $this->signType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $signType
|
|
||||||
* @return Redhat
|
|
||||||
*/
|
|
||||||
public function setSignType(string $signType): Redhat
|
|
||||||
{
|
|
||||||
$this->signType = $signType;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
@@ -81,12 +45,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $charset
|
* @param string $charset
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setCharset(string $charset): Redhat
|
public function setCharset(string $charset): void
|
||||||
{
|
{
|
||||||
$this->charset = $charset;
|
$this->charset = $charset;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,12 +61,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $nonce_str
|
* @param string $nonce_str
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setNonceStr(string $nonce_str): Redhat
|
public function setNonceStr(string $nonce_str): void
|
||||||
{
|
{
|
||||||
$this->nonce_str = $nonce_str;
|
$this->nonce_str = $nonce_str;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,12 +77,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sign
|
* @param string $sign
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setSign(string $sign): Redhat
|
public function setSign(string $sign): void
|
||||||
{
|
{
|
||||||
$this->sign = $sign;
|
$this->sign = $sign;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -130,20 +88,15 @@ class Redhat extends SmallProgram
|
|||||||
*/
|
*/
|
||||||
public function getMchBillno(): string
|
public function getMchBillno(): string
|
||||||
{
|
{
|
||||||
if (empty($this->mch_billno)) {
|
|
||||||
return '1559179381' . date('Ymd') . mt_rand(11, 99) . date('His') . mt_rand(11, 99);
|
|
||||||
}
|
|
||||||
return $this->mch_billno;
|
return $this->mch_billno;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $mch_billno
|
* @param string $mch_billno
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setMchBillno(string $mch_billno): Redhat
|
public function setMchBillno(string $mch_billno): void
|
||||||
{
|
{
|
||||||
$this->mch_billno = $mch_billno;
|
$this->mch_billno = $mch_billno;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,12 +109,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $mch_id
|
* @param string $mch_id
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setMchId(string $mch_id): Redhat
|
public function setMchId(string $mch_id): void
|
||||||
{
|
{
|
||||||
$this->mch_id = $mch_id;
|
$this->mch_id = $mch_id;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -174,12 +125,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $mch_name
|
* @param string $mch_name
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setMchName(string $mch_name): Redhat
|
public function setMchName(string $mch_name): void
|
||||||
{
|
{
|
||||||
$this->mch_name = $mch_name;
|
$this->mch_name = $mch_name;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,12 +141,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $qqappid
|
* @param string $qqappid
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setQqappid(string $qqappid): Redhat
|
public function setQqappid(string $qqappid): void
|
||||||
{
|
{
|
||||||
$this->qqappid = $qqappid;
|
$this->qqappid = $qqappid;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -210,12 +157,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $re_openid
|
* @param string $re_openid
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setReOpenid(string $re_openid): Redhat
|
public function setReOpenid(string $re_openid): void
|
||||||
{
|
{
|
||||||
$this->re_openid = $re_openid;
|
$this->re_openid = $re_openid;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -228,12 +173,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $total_amount
|
* @param string $total_amount
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setTotalAmount(string $total_amount): Redhat
|
public function setTotalAmount(string $total_amount): void
|
||||||
{
|
{
|
||||||
$this->total_amount = $total_amount;
|
$this->total_amount = $total_amount;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,12 +189,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $total_num
|
* @param string $total_num
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setTotalNum(string $total_num): Redhat
|
public function setTotalNum(string $total_num): void
|
||||||
{
|
{
|
||||||
$this->total_num = $total_num;
|
$this->total_num = $total_num;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -264,12 +205,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $wishing
|
* @param string $wishing
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setWishing(string $wishing): Redhat
|
public function setWishing(string $wishing): void
|
||||||
{
|
{
|
||||||
$this->wishing = $wishing;
|
$this->wishing = $wishing;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -282,12 +221,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $act_name
|
* @param string $act_name
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setActName(string $act_name): Redhat
|
public function setActName(string $act_name): void
|
||||||
{
|
{
|
||||||
$this->act_name = $act_name;
|
$this->act_name = $act_name;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -300,12 +237,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $icon_id
|
* @param string $icon_id
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setIconId(string $icon_id): Redhat
|
public function setIconId(string $icon_id): void
|
||||||
{
|
{
|
||||||
$this->icon_id = $icon_id;
|
$this->icon_id = $icon_id;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -318,12 +253,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $banner_id
|
* @param string $banner_id
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setBannerId(string $banner_id): Redhat
|
public function setBannerId(string $banner_id): void
|
||||||
{
|
{
|
||||||
$this->banner_id = $banner_id;
|
$this->banner_id = $banner_id;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -336,12 +269,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $notify_url
|
* @param string $notify_url
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setNotifyUrl(string $notify_url): Redhat
|
public function setNotifyUrl(string $notify_url): void
|
||||||
{
|
{
|
||||||
$this->notify_url = $notify_url;
|
$this->notify_url = $notify_url;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -354,12 +285,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $not_send_msg
|
* @param string $not_send_msg
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setNotSendMsg(string $not_send_msg): Redhat
|
public function setNotSendMsg(string $not_send_msg): void
|
||||||
{
|
{
|
||||||
$this->not_send_msg = $not_send_msg;
|
$this->not_send_msg = $not_send_msg;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -372,12 +301,10 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $min_value
|
* @param string $min_value
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setMinValue(string $min_value): Redhat
|
public function setMinValue(string $min_value): void
|
||||||
{
|
{
|
||||||
$this->min_value = $min_value;
|
$this->min_value = $min_value;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -390,12 +317,74 @@ class Redhat extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $max_value
|
* @param string $max_value
|
||||||
* @return Redhat
|
|
||||||
*/
|
*/
|
||||||
public function setMaxValue(string $max_value): Redhat
|
public function setMaxValue(string $max_value): void
|
||||||
{
|
{
|
||||||
$this->max_value = $max_value;
|
$this->max_value = $max_value;
|
||||||
return $this;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getKey(): string
|
||||||
|
{
|
||||||
|
return $this->key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function setKey(string $key): void
|
||||||
|
{
|
||||||
|
$this->key = $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSignType(): string
|
||||||
|
{
|
||||||
|
return $this->signType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $signType
|
||||||
|
*/
|
||||||
|
public function setSignType(string $signType): void
|
||||||
|
{
|
||||||
|
$this->signType = $signType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSendUrl(): string
|
||||||
|
{
|
||||||
|
return $this->sendUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sendUrl
|
||||||
|
*/
|
||||||
|
public function setSendUrl(string $sendUrl): void
|
||||||
|
{
|
||||||
|
$this->sendUrl = $sendUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSearchUrl(): string
|
||||||
|
{
|
||||||
|
return $this->searchUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $searchUrl
|
||||||
|
*/
|
||||||
|
public function setSearchUrl(string $searchUrl): void
|
||||||
|
{
|
||||||
|
$this->searchUrl = $searchUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -403,8 +392,9 @@ class Redhat extends SmallProgram
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* 构建请求参数
|
* 构建请求参数
|
||||||
*/
|
*/
|
||||||
private function generate()
|
private function generate(): array
|
||||||
{
|
{
|
||||||
|
$requestParam = [];
|
||||||
$requestParam['charset'] = $this->getCharset();
|
$requestParam['charset'] = $this->getCharset();
|
||||||
$requestParam['nonce_str'] = $this->getNonceStr();
|
$requestParam['nonce_str'] = $this->getNonceStr();
|
||||||
$requestParam['mch_billno'] = $this->getMchBillno();
|
$requestParam['mch_billno'] = $this->getMchBillno();
|
||||||
@@ -431,48 +421,41 @@ class Redhat extends SmallProgram
|
|||||||
* @param $requestParam
|
* @param $requestParam
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function builderSign($requestParam)
|
private function builderSign($requestParam): string
|
||||||
{
|
{
|
||||||
return Help::sign($requestParam, $this->getKey(), $this->getSignType());
|
return Help::sign($requestParam, $this->getKey(), $this->getSignType());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return Result
|
||||||
* @throws
|
|
||||||
* 发红包
|
|
||||||
*/
|
*/
|
||||||
public function redEnvelopes()
|
public function redEnvelopes(): Result
|
||||||
{
|
{
|
||||||
$callback = function ($response) {
|
$client = new Client('api.qpay.qq.com', 443, true);
|
||||||
$json = json_decode($response, true);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
$client->post($this->sendUrl, $this->generate());
|
||||||
$message = $json['return_msg'] ?? $json['retmsg'];
|
$client->close();
|
||||||
$param = ['code' => 500, 'message' => $message];
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
} else if ($json['retcode'] != 0) {
|
return new Result(code: 505, message: 'network error.');
|
||||||
$param = ['code' => $json['retcode'], 'message' => $json['retmsg']];
|
}
|
||||||
} else {
|
$json = json_decode($client->getBody(), true);
|
||||||
$param = ['code' => 0, 'message' => $json['retmsg'], 'data' => ['listid' => $json['listid']]];
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||||
}
|
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
||||||
return new Result($param);
|
}
|
||||||
};
|
if ($json['retcode'] != 0) {
|
||||||
$this->request->setCallback($callback);
|
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
||||||
$response = $this->request->post($this->sendUrl, $this->generate());
|
} else {
|
||||||
if (!$response->isResultsOK()) {
|
return new Result(code: 0, data: $json);
|
||||||
throw new \Exception($response->getMessage());
|
|
||||||
}
|
}
|
||||||
return $response->getData('listid');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $listid
|
* @param $listid
|
||||||
* @param null $orderNo
|
* @param $orderNo
|
||||||
* @return RedHatResult
|
* @return Result
|
||||||
* @throws \Exception
|
|
||||||
*
|
|
||||||
* 查询红包状态
|
|
||||||
*/
|
*/
|
||||||
public function searchByOrderNo($listid, $orderNo = null)
|
public function searchByOrderNo($listid, $orderNo = null): Result
|
||||||
{
|
{
|
||||||
$requestParam['nonce_str'] = $this->getNonceStr();
|
$requestParam['nonce_str'] = $this->getNonceStr();
|
||||||
$requestParam['mch_id'] = $this->getMchId();
|
$requestParam['mch_id'] = $this->getMchId();
|
||||||
@@ -481,20 +464,19 @@ class Redhat extends SmallProgram
|
|||||||
$requestParam['mch_billno'] = $orderNo;
|
$requestParam['mch_billno'] = $orderNo;
|
||||||
}
|
}
|
||||||
$requestParam['sign'] = $this->builderSign($requestParam);
|
$requestParam['sign'] = $this->builderSign($requestParam);
|
||||||
$this->request->setCallback(function ($response) {
|
$client = new Client('qpay.qq.com', 443, true);
|
||||||
$response = json_decode($response, true);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
if (!isset($response['result']) || $response['result'] != 0) {
|
$client->post($this->searchUrl, $requestParam);
|
||||||
$param = ['code' => 500, 'message' => $response['res_info'] ?? '订单查询失败!', 'data' => $response];
|
$client->close();
|
||||||
} else {
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
$param = ['code' => 0, 'message' => 'system success!', 'data' => $response];
|
return new Result(code: 505, message: 'network error.');
|
||||||
}
|
}
|
||||||
return new Result($param);
|
$json = json_decode($client->getBody(), true);
|
||||||
});
|
if (isset($json['result']) && $json['result'] != 'SUCCESS') {
|
||||||
$response = $this->request->get($this->searchUrl, $requestParam);
|
return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!');
|
||||||
if (!$response->isResultsOK()) {
|
} else {
|
||||||
throw new \Exception($response->getMessage());
|
return new Result(code: 0, data: $json);
|
||||||
}
|
}
|
||||||
return new RedHatResult($response->getData());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-26
@@ -4,6 +4,7 @@
|
|||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,52 +14,68 @@ use wchat\common\Result;
|
|||||||
class SecCheck extends SmallProgram
|
class SecCheck extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $_url = 'https://api.q.qq.com/api/json/security/ImgSecCheck?access_token=';
|
private string $_url = '/api/json/security/ImgSecCheck?access_token=';
|
||||||
|
|
||||||
private $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
|
private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return array|Result|mixed
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function image($path = '')
|
public function image(string $path = ''): Result
|
||||||
{
|
{
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
return $this->sendError('文件不存在', 404);
|
return $this->sendError('文件不存在', 404);
|
||||||
}
|
}
|
||||||
$this->request->setUseSwoole(false);
|
|
||||||
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
$real_path = new \CURLFile($path);
|
||||||
$this->request->setErrorField('errCode');
|
$data = [
|
||||||
$this->request->setErrorMsgField('errMsg');
|
'appid' => $this->config->getAppid(),
|
||||||
return $this->request->post($this->_url . $this->config->getAccessToken(), [
|
'media' => $real_path,
|
||||||
'appid' => $this->config->getAppid(), 'media' => new \CURLFile($path)
|
'form-data[filename]' => $path,
|
||||||
]);
|
'form-data[content-type]' => $real_path->getMimeType()
|
||||||
|
];
|
||||||
|
$client = new Client('api.q.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||||
|
$client->upload($path, $data);
|
||||||
|
$client->close();
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errCode']) && $body['errCode'] != 0) {
|
||||||
|
return new Result(code: $body['errCode'], message: $body['errMsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $content
|
* @param string $content
|
||||||
* @return array|Result|mixed
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function text($content)
|
public function text(string $content): Result
|
||||||
{
|
{
|
||||||
if (empty($content)) {
|
if (empty($content)) {
|
||||||
return $this->sendError('文件不存在', 404);
|
return $this->sendError('文件不存在', 404);
|
||||||
}
|
}
|
||||||
$this->request->setUseSwoole(true);
|
$url = '/' . ltrim($this->_url, '/') . $this->config->getAccessToken();
|
||||||
$this->request->addHeader('Content-Type', 'application/json');
|
|
||||||
$this->request->setErrorField('errCode');
|
|
||||||
$this->request->setErrorMsgField('errMsg');
|
|
||||||
$this->request->setData([
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'content' => $content
|
|
||||||
]);
|
|
||||||
$this->request->setHost('api.q.qq.com');
|
|
||||||
$this->request->setIsSSL(true);
|
|
||||||
|
|
||||||
$url = '/' . ltrim($this->_msgUrl, '/') . $this->config->getAccessToken();
|
$client = new Client('api.q.qq.com', 443, true);
|
||||||
return $this->request->post($url);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->post($url, ['appid' => $this->config->getAppid(), 'content' => $content]);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errCode']) && $body['errCode'] != 0) {
|
||||||
|
return new Result(code: $body['errCode'], message: $body['errMsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,37 +4,9 @@
|
|||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\Miniprogarampage;
|
use wchat\common\Multiprogramming;
|
||||||
|
|
||||||
class SmallProgram extends Miniprogarampage
|
class SmallProgram extends Multiprogramming
|
||||||
{
|
{
|
||||||
|
|
||||||
protected static $instance;
|
|
||||||
|
|
||||||
private $url = 'https://api.q.qq.com/api/getToken';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $get_token
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function generateAccess_token($get_token = false)
|
|
||||||
{
|
|
||||||
if (!empty($this->config->getToken())) {
|
|
||||||
return $this->config->getToken();
|
|
||||||
}
|
|
||||||
$query = [
|
|
||||||
'grant_type' => 'client_credential',
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'secret' => $this->config->getAppsecret()
|
|
||||||
];
|
|
||||||
$param = $this->request->get($this->url, $query);
|
|
||||||
if (!$param->isResultsOK()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if ($get_token) {
|
|
||||||
return $param->getData('access_token');
|
|
||||||
}
|
|
||||||
return $param->getData();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-3
@@ -13,14 +13,21 @@ use wchat\common\Result;
|
|||||||
class Subject extends \wchat\base\Subject
|
class Subject extends \wchat\base\Subject
|
||||||
{
|
{
|
||||||
|
|
||||||
private $sendUrl = 'https://api.q.qq.com/api/json/subscribe/SendSubscriptionMessage';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUrl(): string
|
public function getUrl(): string
|
||||||
{
|
{
|
||||||
return $this->getUrl();
|
return '/api/json/subscribe/SendSubscriptionMessage';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getHost(): string
|
||||||
|
{
|
||||||
|
return 'api.q.qq.com';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/8 0008
|
|
||||||
* Time: 9:49
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace wchat\qq;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\Result;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Template
|
|
||||||
* @package wchat\qq
|
|
||||||
*/
|
|
||||||
class Template extends \wchat\base\Template
|
|
||||||
{
|
|
||||||
|
|
||||||
private $sendUrl = 'https://api.q.qq.com/api/json/template/send';
|
|
||||||
|
|
||||||
public function getUrl(): string
|
|
||||||
{
|
|
||||||
return $this->sendUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+26
-1
@@ -4,10 +4,35 @@
|
|||||||
namespace wchat\qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
class Token extends SmallProgram
|
class Token extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function token(): Result
|
||||||
|
{
|
||||||
|
$query = [
|
||||||
|
'grant_type' => 'client_credential',
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'secret' => $this->config->getAppsecret()
|
||||||
|
];
|
||||||
|
$client = new Client('api.q.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->get('/api/getToken', $query);
|
||||||
|
$client->close();
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-46
@@ -5,6 +5,7 @@ namespace wchat\qq\pay;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Help;
|
use wchat\common\Help;
|
||||||
use wchat\common\HttpClient;
|
use wchat\common\HttpClient;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
@@ -17,9 +18,9 @@ use wchat\wx\SmallProgram;
|
|||||||
class Cash_Bonus extends SmallProgram
|
class Cash_Bonus extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
public $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
public string $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||||
|
|
||||||
private $_errors = [
|
private array $_errors = [
|
||||||
'66228701' => '红包个数超出限制',
|
'66228701' => '红包个数超出限制',
|
||||||
'66228705' => '总金额超出限制',
|
'66228705' => '总金额超出限制',
|
||||||
'66228706' => '总金额不足以按最小金额领取每个红包',
|
'66228706' => '总金额不足以按最小金额领取每个红包',
|
||||||
@@ -33,34 +34,34 @@ class Cash_Bonus extends SmallProgram
|
|||||||
'66229716' => '用户禁用公众号,发AIO消息失败'
|
'66229716' => '用户禁用公众号,发AIO消息失败'
|
||||||
];
|
];
|
||||||
|
|
||||||
private $_requestParams = [];
|
private array $_requestParams = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setMchName(string $value)
|
public function setMchName($value)
|
||||||
{
|
{
|
||||||
$this->_requestParams['mch_name'] = $value;
|
$this->_requestParams['mch_name'] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setActName(string $value)
|
public function setActName($value)
|
||||||
{
|
{
|
||||||
$this->_requestParams['act_name'] = $value;
|
$this->_requestParams['act_name'] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setWishing(string $value)
|
public function setWishing($value)
|
||||||
{
|
{
|
||||||
$this->_requestParams['wishing'] = $value;
|
$this->_requestParams['wishing'] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setIconId($value)
|
public function setIconId($value)
|
||||||
{
|
{
|
||||||
@@ -68,7 +69,7 @@ class Cash_Bonus extends SmallProgram
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setBannerId($value)
|
public function setBannerId($value)
|
||||||
{
|
{
|
||||||
@@ -83,39 +84,30 @@ class Cash_Bonus extends SmallProgram
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function mch_send($mch_billno, $openId, $price)
|
public function mch_send($mch_billno, $openId, $price): Result
|
||||||
{
|
{
|
||||||
$client = HttpClient::NewRequest();
|
$client = new Client('api.qpay.qq.com', 443, true);
|
||||||
$client->setIsSSL(true);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$client->setSslCertFile($this->getConfig()->getSslCert());
|
$client->withSslCertFile($this->getConfig()->getSslCert());
|
||||||
$client->setSslKeyFile($this->getConfig()->getSslKey());
|
$client->withSslKeyFile($this->getConfig()->getSslKey());
|
||||||
$client->setCa($this->getConfig()->getSslCa());
|
$client->withCa($this->getConfig()->getSslCa());
|
||||||
$client->setUseSwoole($this->getConfig()->isUsrSwoole());
|
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||||
$client->setHost('api.qpay.qq.com');
|
$client->close();
|
||||||
$client->setCallback(function ($response, $requestParam) {
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
$json = json_decode($response, true);
|
return new Result(code: 505, message: 'network error.');
|
||||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
}
|
||||||
$array['code'] = 500;
|
$json = json_decode($client->getBody(), true);
|
||||||
$array['message'] = $json['return_msg'] ?? $json['retmsg'];
|
|
||||||
} else if ($json['retcode'] != 0) {
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||||
if (empty($json['retmsg']) && isset($this->_errors[$json['retcode']])) {
|
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
||||||
$json['retmsg'] = $this->_errors[$json['retcode']];
|
} else if ($json['retcode'] != 0) {
|
||||||
}
|
if (empty($json['retmsg']) && isset($this->_errors[$json['retcode']])) {
|
||||||
$array['code'] = $json['retcode'];
|
$json['retmsg'] = $this->_errors[$json['retcode']];
|
||||||
$array['message'] = $json['retmsg'];
|
}
|
||||||
} else {
|
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
||||||
$array['code'] = 0;
|
} else {
|
||||||
$array['message'] = $json['retmsg'];
|
return new Result(code: 0, data: $json);
|
||||||
$array['data'] = ['listid' => $json['listid']];
|
|
||||||
}
|
|
||||||
$array['data']['request'] = $requestParam;
|
|
||||||
return new Result($array);
|
|
||||||
});
|
|
||||||
$response = $client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
|
||||||
if (!$response->isResultsOK()) {
|
|
||||||
throw new Exception($response->getMessage());
|
|
||||||
}
|
}
|
||||||
return $response->getData('listid');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,7 +116,7 @@ class Cash_Bonus extends SmallProgram
|
|||||||
* @param $price
|
* @param $price
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function orderConfig($mch_billno, $openId, $price)
|
private function orderConfig($mch_billno, $openId, $price): array
|
||||||
{
|
{
|
||||||
$requestParam['charset'] = 1;
|
$requestParam['charset'] = 1;
|
||||||
$requestParam['nonce_str'] = Help::random(32);
|
$requestParam['nonce_str'] = Help::random(32);
|
||||||
@@ -138,7 +130,7 @@ class Cash_Bonus extends SmallProgram
|
|||||||
$requestParam['total_num'] = 1;
|
$requestParam['total_num'] = 1;
|
||||||
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
|
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
|
||||||
|
|
||||||
if (!empty($this->_requestParams) && is_array($this->_requestParams)) {
|
if (!empty($this->_requestParams)) {
|
||||||
$requestParam = array_merge($requestParam, $this->_requestParams);
|
$requestParam = array_merge($requestParam, $this->_requestParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +144,7 @@ class Cash_Bonus extends SmallProgram
|
|||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function mchOrderNo()
|
public function mchOrderNo(): string
|
||||||
{
|
{
|
||||||
return implode([
|
return implode([
|
||||||
$this->getConfig()->getMchId(),
|
$this->getConfig()->getMchId(),
|
||||||
@@ -168,7 +160,7 @@ class Cash_Bonus extends SmallProgram
|
|||||||
* @param array $requestParams
|
* @param array $requestParams
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validator(array $requestParams)
|
public function validator(array $requestParams): bool
|
||||||
{
|
{
|
||||||
$notifySign = $requestParams['sign'];
|
$notifySign = $requestParams['sign'];
|
||||||
unset($requestParams['sign']);
|
unset($requestParams['sign']);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace wchat\qq\pay;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Help;
|
use wchat\common\Help;
|
||||||
use wchat\common\HttpClient;
|
use wchat\common\HttpClient;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
@@ -16,7 +17,7 @@ use wchat\qq\SmallProgram;
|
|||||||
*/
|
*/
|
||||||
class Enterprise_payment extends SmallProgram
|
class Enterprise_payment extends SmallProgram
|
||||||
{
|
{
|
||||||
public $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi';
|
public string $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi';
|
||||||
|
|
||||||
private $_errors = [
|
private $_errors = [
|
||||||
'SYSTEMERROR' => '系统错误',
|
'SYSTEMERROR' => '系统错误',
|
||||||
@@ -39,25 +40,25 @@ class Enterprise_payment extends SmallProgram
|
|||||||
private $_requestParams = [];
|
private $_requestParams = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setOpUserId(string $value)
|
public function setOpUserId($value)
|
||||||
{
|
{
|
||||||
$this->_requestParams['op_user_id'] = $value;
|
$this->_requestParams['op_user_id'] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setOpUserPassword(string $value)
|
public function setOpUserPassword($value)
|
||||||
{
|
{
|
||||||
$this->_requestParams['op_user_passwd'] = $value;
|
$this->_requestParams['op_user_passwd'] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setSpbillCreateIp(string $value)
|
public function setSpbillCreateIp($value)
|
||||||
{
|
{
|
||||||
$this->_requestParams['spbill_create_ip'] = $value;
|
$this->_requestParams['spbill_create_ip'] = $value;
|
||||||
}
|
}
|
||||||
@@ -71,34 +72,24 @@ class Enterprise_payment extends SmallProgram
|
|||||||
*/
|
*/
|
||||||
public function mch_send($mch_billno, $openId, $price)
|
public function mch_send($mch_billno, $openId, $price)
|
||||||
{
|
{
|
||||||
$client = HttpClient::NewRequest();
|
$client = new Client('api.qpay.qq.com', 443, true);
|
||||||
$client->setIsSSL(true);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$client->setSslCertFile($this->getConfig()->getSslCert());
|
$client->withSslCertFile($this->getConfig()->getSslCert());
|
||||||
$client->setSslKeyFile($this->getConfig()->getSslKey());
|
$client->withSslKeyFile($this->getConfig()->getSslKey());
|
||||||
$client->setCa($this->getConfig()->getSslCa());
|
$client->withCa($this->getConfig()->getSslCa());
|
||||||
$client->setUseSwoole($this->getConfig()->isUsrSwoole());
|
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||||
$client->setHost('api.qpay.qq.com');
|
$client->close();
|
||||||
$client->setCallback(function ($response, $requestParam) {
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
$json = json_decode($response, true);
|
return new Result(code: 505, message: 'network error.');
|
||||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
}
|
||||||
$array['code'] = 500;
|
$json = json_decode($client->getBody(), true);
|
||||||
$array['message'] = $json['return_msg'] ?? $json['retmsg'];
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||||
} else if ($json['result_code'] != 'SUCCESS') {
|
return new Result(code: 500, data: $json['return_msg'] ?? $json['retmsg']);
|
||||||
$array['code'] = 500;
|
} else if ($json['result_code'] != 'SUCCESS') {
|
||||||
$array['message'] = $this->_errors[$json['err_code']] ?? $json['err_code_desc'];
|
return new Result(code: 500, data: $this->_errors[$json['err_code']] ?? $json['err_code_desc']);
|
||||||
} else {
|
} else {
|
||||||
$array['code'] = 0;
|
return new Result(code: 0, data: $json);
|
||||||
$array['message'] = 'system success';
|
|
||||||
$array['data'] = $json;
|
|
||||||
}
|
|
||||||
$array['data']['request'] = $requestParam;
|
|
||||||
return new Result($array);
|
|
||||||
});
|
|
||||||
$response = $client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
|
||||||
if (!$response->isResultsOK()) {
|
|
||||||
throw new Exception($response->getMessage());
|
|
||||||
}
|
}
|
||||||
return $response->getData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,7 +98,7 @@ class Enterprise_payment extends SmallProgram
|
|||||||
* @param $price
|
* @param $price
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function orderConfig($mch_billno, $openId, $price)
|
private function orderConfig($mch_billno, $openId, $price): array
|
||||||
{
|
{
|
||||||
$requestParam['input_charset'] = 'UTF-8';
|
$requestParam['input_charset'] = 'UTF-8';
|
||||||
$requestParam['nonce_str'] = Help::random(32);
|
$requestParam['nonce_str'] = Help::random(32);
|
||||||
@@ -135,7 +126,7 @@ class Enterprise_payment extends SmallProgram
|
|||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function mchOrderNo()
|
public function mchOrderNo(): string
|
||||||
{
|
{
|
||||||
return implode([
|
return implode([
|
||||||
$this->getConfig()->getMchId(),
|
$this->getConfig()->getMchId(),
|
||||||
@@ -151,7 +142,7 @@ class Enterprise_payment extends SmallProgram
|
|||||||
* @param array $requestParams
|
* @param array $requestParams
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validator(array $requestParams)
|
public function validator(array $requestParams): bool
|
||||||
{
|
{
|
||||||
$notifySign = $requestParams['sign'];
|
$notifySign = $requestParams['sign'];
|
||||||
unset($requestParams['sign']);
|
unset($requestParams['sign']);
|
||||||
|
|||||||
@@ -10,17 +10,17 @@ namespace wchat\qq\result;
|
|||||||
class RedHatResult
|
class RedHatResult
|
||||||
{
|
{
|
||||||
|
|
||||||
public $result;
|
public mixed $result;
|
||||||
public $res_info;
|
public mixed $res_info;
|
||||||
|
|
||||||
public $listid;
|
public mixed $listid;
|
||||||
public $state;
|
public mixed $state;
|
||||||
public $total_num;
|
public mixed $total_num;
|
||||||
public $recv_num;
|
public mixed $recv_num;
|
||||||
public $total_amount;
|
public mixed $total_amount;
|
||||||
public $recv_amount;
|
public mixed $recv_amount;
|
||||||
public $recv_details;
|
public mixed $recv_details;
|
||||||
public $uin;
|
public mixed $uin;
|
||||||
|
|
||||||
|
|
||||||
const RED_HAT_STATUS_PAID = 1;
|
const RED_HAT_STATUS_PAID = 1;
|
||||||
@@ -40,9 +40,9 @@ class RedHatResult
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $array
|
* @param $array
|
||||||
* @return $this
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function init($array)
|
private function init($array): void
|
||||||
{
|
{
|
||||||
foreach ($array as $key => $value) {
|
foreach ($array as $key => $value) {
|
||||||
if (!property_exists($this, $key)) {
|
if (!property_exists($this, $key)) {
|
||||||
@@ -50,7 +50,6 @@ class RedHatResult
|
|||||||
}
|
}
|
||||||
$this->{$key} = $value;
|
$this->{$key} = $value;
|
||||||
}
|
}
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -58,7 +57,7 @@ class RedHatResult
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($field)
|
public function get($field): mixed
|
||||||
{
|
{
|
||||||
return $this->$field;
|
return $this->$field;
|
||||||
}
|
}
|
||||||
@@ -68,7 +67,7 @@ class RedHatResult
|
|||||||
* @return bool
|
* @return bool
|
||||||
* 是否已完成支付
|
* 是否已完成支付
|
||||||
*/
|
*/
|
||||||
public function isPaid()
|
public function isPaid(): bool
|
||||||
{
|
{
|
||||||
return $this->state == self::RED_HAT_STATUS_PAID;
|
return $this->state == self::RED_HAT_STATUS_PAID;
|
||||||
}
|
}
|
||||||
@@ -77,7 +76,7 @@ class RedHatResult
|
|||||||
* @return bool
|
* @return bool
|
||||||
* 是否已抢完
|
* 是否已抢完
|
||||||
*/
|
*/
|
||||||
public function isSnatched()
|
public function isSnatched(): bool
|
||||||
{
|
{
|
||||||
return $this->state == self::RED_HAT_STATUS_SNATCHED;
|
return $this->state == self::RED_HAT_STATUS_SNATCHED;
|
||||||
}
|
}
|
||||||
@@ -86,17 +85,16 @@ class RedHatResult
|
|||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isExpired()
|
public function isExpired(): bool
|
||||||
{
|
{
|
||||||
return $this->state == self::RED_HAT_STATUS_EXPIRED;
|
return $this->state == self::RED_HAT_STATUS_EXPIRED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isRefunded()
|
public function isRefunded(): bool
|
||||||
{
|
{
|
||||||
return $this->state == self::RED_HAT_STATUS_REFUNDED;
|
return $this->state == self::RED_HAT_STATUS_REFUNDED;
|
||||||
}
|
}
|
||||||
|
|||||||
+83
-83
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Decode;
|
use wchat\common\Decode;
|
||||||
use wchat\common\HttpClient;
|
use wchat\common\HttpClient;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
@@ -15,65 +17,59 @@ use wchat\common\Result;
|
|||||||
class Account extends SmallProgram
|
class Account extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
|
||||||
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
|
|
||||||
private $publicInfo = 'https://api.weixin.qq.com/cgi-bin/user/info?';
|
|
||||||
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
|
||||||
|
|
||||||
private $savePath = __DIR__ . '/../../Users/';
|
|
||||||
|
|
||||||
|
|
||||||
private $OK = 0;
|
|
||||||
private $IllegalAesKey = -41001;
|
|
||||||
private $IllegalIv = -41002;
|
|
||||||
private $IllegalBuffer = -41003;
|
|
||||||
private $DecodeBase64Error = -41004;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $path
|
|
||||||
*/
|
|
||||||
public function setSavePath($path)
|
|
||||||
{
|
|
||||||
$this->savePath = $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $code
|
* @param $code
|
||||||
* @return Result
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function login($code)
|
public function login($code): Result
|
||||||
{
|
{
|
||||||
$param['appid'] = $this->config->getAppid();
|
$param['appid'] = $this->config->getAppid();
|
||||||
$param['secret'] = $this->config->getAppsecret();
|
$param['secret'] = $this->config->getAppsecret();
|
||||||
$param['js_code'] = $code;
|
$param['js_code'] = $code;
|
||||||
$param['grant_type'] = 'authorization_code';
|
$param['grant_type'] = 'authorization_code';
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::GET);
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->addHeader('Content-Type', 'text/xml');
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$this->request->setErrorField('errcode');
|
$client->get('sns/jscode2session', $param);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->close();
|
||||||
|
|
||||||
return $this->request->get('sns/jscode2session', $param);
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $openid
|
* @param $openid
|
||||||
* @return array|mixed|Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getPublicUserInfo($openid)
|
public function getPublicUserInfo($openid): Result
|
||||||
{
|
{
|
||||||
$query = [
|
$query = [
|
||||||
'access_token' => $this->getAccessToken(),
|
'access_token' => $this->config->getAccessToken(),
|
||||||
'openid' => $openid,
|
'openid' => $openid,
|
||||||
'lang' => 'zh_CN'
|
'lang' => 'zh_CN'
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::GET);
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setIsSSL(true);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$this->request->setErrorField('errcode');
|
$client->get('cgi-bin/user/info', $query);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->close();
|
||||||
return $this->request->get($this->publicInfo, $query);
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
}
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -81,7 +77,7 @@ class Account extends SmallProgram
|
|||||||
* @param $encryptedData
|
* @param $encryptedData
|
||||||
* @param $iv
|
* @param $iv
|
||||||
* @param $sessionKey
|
* @param $sessionKey
|
||||||
* @param $asArray
|
* @param bool $asArray
|
||||||
* @return object|array
|
* @return object|array
|
||||||
* @throws
|
* @throws
|
||||||
*
|
*
|
||||||
@@ -91,7 +87,7 @@ class Account extends SmallProgram
|
|||||||
* <li>-41005: base64加密失败</li>
|
* <li>-41005: base64加密失败</li>
|
||||||
* <li>-41016: base64解密失败</li>
|
* <li>-41016: base64解密失败</li>
|
||||||
*/
|
*/
|
||||||
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
public function decode($encryptedData, $iv, $sessionKey, bool $asArray = false): object|array
|
||||||
{
|
{
|
||||||
$decode = new Decode();
|
$decode = new Decode();
|
||||||
$decode->setSessionKey($sessionKey);
|
$decode->setSessionKey($sessionKey);
|
||||||
@@ -107,21 +103,26 @@ class Account extends SmallProgram
|
|||||||
* @param $path
|
* @param $path
|
||||||
* @param $width
|
* @param $width
|
||||||
* @return array|mixed|Result
|
* @return array|mixed|Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function createwxaqrcode($path, $width)
|
public function createwxaqrcode($path, $width): mixed
|
||||||
{
|
{
|
||||||
$url = $this->wxaqr . $this->getAccessToken();
|
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||||
|
|
||||||
$sendBody['path'] = $path;
|
$sendBody['path'] = $path;
|
||||||
$sendBody['width'] = $width;
|
$sendBody['width'] = $width;
|
||||||
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setMethod(HttpClient::POST);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||||
$this->request->setErrorField('errcode');
|
$client->close();
|
||||||
$this->request->setErrorMsgField('errmsg');
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
return $this->request->post($url, $sendBody);
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (!is_null($body)) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $client->getBody());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -131,10 +132,9 @@ class Account extends SmallProgram
|
|||||||
* @param bool $is_hyaline
|
* @param bool $is_hyaline
|
||||||
* @param bool $auto_color
|
* @param bool $auto_color
|
||||||
* @param string $line_color
|
* @param string $line_color
|
||||||
* @return array|mixed|Result
|
* @return Result
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function getwxacode($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
public function getwxacode($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||||
{
|
{
|
||||||
$sendBody['path'] = $path;
|
$sendBody['path'] = $path;
|
||||||
$sendBody['width'] = $width;
|
$sendBody['width'] = $width;
|
||||||
@@ -144,13 +144,21 @@ class Account extends SmallProgram
|
|||||||
$sendBody['line_color'] = $line_color;
|
$sendBody['line_color'] = $line_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = $this->getwxacode . $this->getAccessToken();
|
$url = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$this->request->setErrorField('errcode');
|
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->close();
|
||||||
return $this->request->post($url, $sendBody);
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (!is_null($body)) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $client->getBody());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -160,10 +168,9 @@ class Account extends SmallProgram
|
|||||||
* @param bool $is_hyaline
|
* @param bool $is_hyaline
|
||||||
* @param bool $auto_color
|
* @param bool $auto_color
|
||||||
* @param string $line_color
|
* @param string $line_color
|
||||||
* @return array|mixed|Result
|
* @return Result
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function getwxacodeunlimit($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
public function getwxacodeunlimit($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||||
{
|
{
|
||||||
$sendBody['path'] = $path;
|
$sendBody['path'] = $path;
|
||||||
$sendBody['width'] = $width;
|
$sendBody['width'] = $width;
|
||||||
@@ -173,28 +180,21 @@ class Account extends SmallProgram
|
|||||||
$sendBody['line_color'] = $line_color;
|
$sendBody['line_color'] = $line_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = $this->getwxacodeunlimit . $this->getAccessToken();
|
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$this->request->setErrorField('errcode');
|
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->close();
|
||||||
return $this->request->post($url, $sendBody);
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
}
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
/**
|
$body = json_decode($client->getBody(), true);
|
||||||
* @param mixed $body
|
if (!is_null($body)) {
|
||||||
* @return string
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
* @throws \Exception
|
} else {
|
||||||
*/
|
return new Result(code: 0, data: $client->getBody());
|
||||||
public function saveByPath($body)
|
|
||||||
{
|
|
||||||
if (!is_null($json = json_decode($body))) {
|
|
||||||
throw new \Exception($json['errmsg'], $json['errcode']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$push = md5_file($body) . '.png';
|
|
||||||
file_put_contents($this->savePath . $push, $this->savePath);
|
|
||||||
return $this->savePath . $push;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,21 +10,21 @@ namespace wchat\wx;
|
|||||||
*/
|
*/
|
||||||
class ContentAsyncCheck
|
class ContentAsyncCheck
|
||||||
{
|
{
|
||||||
private $_ToUserName = '';
|
private mixed $_ToUserName = '';
|
||||||
private $_FromUserName = '';
|
private mixed $_FromUserName = '';
|
||||||
private $_CreateTime = '';
|
private mixed $_CreateTime = '';
|
||||||
private $_MsgType = '';
|
private mixed $_MsgType = '';
|
||||||
private $_Event = '';
|
private mixed $_Event = '';
|
||||||
private $_isrisky = '';
|
private mixed $_isrisky = '';
|
||||||
private $_extra_info_json = '';
|
private mixed $_extra_info_json = '';
|
||||||
private $_appid = '';
|
private mixed $_appid = '';
|
||||||
private $_trace_id = '';
|
private mixed $_trace_id = '';
|
||||||
private $_status_code = '';
|
private mixed $_status_code = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getToUserName(): string
|
public function getToUserName(): mixed
|
||||||
{
|
{
|
||||||
return $this->_ToUserName;
|
return $this->_ToUserName;
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getFromUserName(): string
|
public function getFromUserName(): mixed
|
||||||
{
|
{
|
||||||
return $this->_FromUserName;
|
return $this->_FromUserName;
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getCreateTime(): string
|
public function getCreateTime(): mixed
|
||||||
{
|
{
|
||||||
return $this->_CreateTime;
|
return $this->_CreateTime;
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getMsgType(): string
|
public function getMsgType(): mixed
|
||||||
{
|
{
|
||||||
return $this->_MsgType;
|
return $this->_MsgType;
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getEvent(): string
|
public function getEvent(): mixed
|
||||||
{
|
{
|
||||||
return $this->_Event;
|
return $this->_Event;
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getIsrisky(): string
|
public function getIsrisky(): mixed
|
||||||
{
|
{
|
||||||
return $this->_isrisky;
|
return $this->_isrisky;
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getExtraInfoJson(): string
|
public function getExtraInfoJson(): mixed
|
||||||
{
|
{
|
||||||
return $this->_extra_info_json;
|
return $this->_extra_info_json;
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAppid(): string
|
public function getAppid(): mixed
|
||||||
{
|
{
|
||||||
return $this->_appid;
|
return $this->_appid;
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTraceId(): string
|
public function getTraceId(): mixed
|
||||||
{
|
{
|
||||||
return $this->_trace_id;
|
return $this->_trace_id;
|
||||||
}
|
}
|
||||||
@@ -96,16 +96,16 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getStatusCode(): string
|
public function getStatusCode(): mixed
|
||||||
{
|
{
|
||||||
return $this->_status_code;
|
return $this->_status_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return ContentAsyncCheck|null
|
* @return ContentAsyncCheck
|
||||||
*/
|
*/
|
||||||
public static function instance(array $params)
|
public static function instance(array $params): static
|
||||||
{
|
{
|
||||||
static $class = null;
|
static $class = null;
|
||||||
if ($class === null) {
|
if ($class === null) {
|
||||||
@@ -118,7 +118,7 @@ class ContentAsyncCheck
|
|||||||
* @param $params
|
* @param $params
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
private function init($params)
|
private function init($params): static
|
||||||
{
|
{
|
||||||
foreach ($params as $item => $param) {
|
foreach ($params as $item => $param) {
|
||||||
$this->{'_' . $item} = $param;
|
$this->{'_' . $item} = $param;
|
||||||
@@ -129,7 +129,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isRisky()
|
public function isRisky(): bool
|
||||||
{
|
{
|
||||||
return intval($this->_isrisky) === 0;
|
return intval($this->_isrisky) === 0;
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ class ContentAsyncCheck
|
|||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isSuccess()
|
public function isSuccess(): bool
|
||||||
{
|
{
|
||||||
return $this->_status_code === 0;
|
return $this->_status_code === 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+139
-138
@@ -3,41 +3,38 @@
|
|||||||
|
|
||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Message
|
||||||
|
* @package wchat\wx
|
||||||
|
*/
|
||||||
class Message extends SmallProgram
|
class Message extends SmallProgram
|
||||||
{
|
{
|
||||||
const TEXT = 0;
|
|
||||||
const IMAGE = 1;
|
|
||||||
const VOICE = 2;
|
|
||||||
const NEWS = 3;
|
|
||||||
const VIDEO = 4;
|
|
||||||
const MUSIC = 5;
|
|
||||||
const MINIPROGRAMPAGE = 6;
|
|
||||||
const WXCARD = 7;
|
|
||||||
|
|
||||||
private $openid = '';
|
private array $msgData = [];
|
||||||
private $msgData = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $openid
|
* @param $openid
|
||||||
*/
|
*/
|
||||||
public function setOpenid(string $openid)
|
public function setOpenid($openid)
|
||||||
{
|
{
|
||||||
$this->openid = $openid;
|
|
||||||
$this->msgData['touser'] = $openid;
|
$this->msgData['touser'] = $openid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $content
|
* @param $content
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendTextNews(string $content)
|
public function sendTextNews($content): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'text';
|
$this->msgData['msgtype'] = 'text';
|
||||||
$this->msgData['text[content]'] = $content;
|
$this->msgData['text'] = ['content' => $content];
|
||||||
|
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
@@ -45,12 +42,12 @@ class Message extends SmallProgram
|
|||||||
/**
|
/**
|
||||||
* @param $media_id
|
* @param $media_id
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendImageNews(string $media_id)
|
public function sendImageNews($media_id): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'image';
|
$this->msgData['msgtype'] = 'image';
|
||||||
$this->msgData['image[media_id]'] = $media_id;
|
$this->msgData['image'] = ['media_id' => $media_id];
|
||||||
|
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
@@ -59,12 +56,12 @@ class Message extends SmallProgram
|
|||||||
/**
|
/**
|
||||||
* @param $media_id
|
* @param $media_id
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendVoiceNews(string $media_id)
|
public function sendVoiceNews($media_id): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'voice';
|
$this->msgData['msgtype'] = 'voice';
|
||||||
$this->msgData['voice[media_id]'] = $media_id;
|
$this->msgData['voice'] = ['media_id' => $media_id];
|
||||||
|
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
@@ -72,131 +69,138 @@ class Message extends SmallProgram
|
|||||||
/**
|
/**
|
||||||
* @param $media_id
|
* @param $media_id
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendMpNewsNews(string $media_id)
|
public function sendMpNewsNews($media_id): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'mpnews';
|
$this->msgData['msgtype'] = 'mpnews';
|
||||||
$this->msgData['mpnews[media_id]'] = $media_id;
|
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
||||||
|
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $title
|
* @param $title
|
||||||
* @param string $description
|
* @param $description
|
||||||
* @param string $url
|
* @param $url
|
||||||
* @param string $picurl
|
* @param $picurl
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendNewsNews(string $title, string $description, string $url, string $picurl)
|
public function sendNewsNews($title, $description, $url, $picurl): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'news';
|
$this->msgData['msgtype'] = 'news';
|
||||||
$this->msgData['news[articles][0][title]'] = $title;
|
$this->msgData['news'] = [
|
||||||
$this->msgData['news[articles][0][description]'] = $description;
|
'articles' => [
|
||||||
$this->msgData['news[articles][0][url]'] = $url;
|
[
|
||||||
$this->msgData['news[articles][0][picurl]'] = $picurl;
|
'title' => $title,
|
||||||
|
'description' => $description,
|
||||||
|
'url' => $url,
|
||||||
|
'picurl' => $picurl
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $title
|
* @param $title
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendCardNews(string $title)
|
public function sendCardNews($title): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'wxcard';
|
$this->msgData['msgtype'] = 'wxcard';
|
||||||
$this->msgData['wxcard[card_id]'] = $title;
|
$this->msgData['wxcard'] = ['card_id' => $title];
|
||||||
|
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $media_id
|
* @param $media_id
|
||||||
* @param string $thumb_media_id
|
* @param $thumb_media_id
|
||||||
* @param string $title
|
* @param $title
|
||||||
* @param string $description
|
* @param $description
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description)
|
public function sendVideoNews($media_id, $thumb_media_id, $title, $description): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'video';
|
$this->msgData['msgtype'] = 'video';
|
||||||
$this->msgData['video[media_id]'] = $media_id;
|
$this->msgData['video'] = [
|
||||||
$this->msgData['video[thumb_media_id]'] = $thumb_media_id;
|
'media_id' => [
|
||||||
$this->msgData['video[title]'] = $title;
|
'media_id' => $media_id,
|
||||||
$this->msgData['video[description]'] = $description;
|
'thumb_media_id' => $thumb_media_id,
|
||||||
|
'title' => $title,
|
||||||
|
'description' => $description
|
||||||
|
]
|
||||||
|
];
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $musicurl
|
* @param $musicurl
|
||||||
* @param string $hqmusicurl
|
* @param $hqmusicurl
|
||||||
* @param string $thumb_media_id
|
* @param $thumb_media_id
|
||||||
* @param string $title
|
* @param $title
|
||||||
* @param string $description
|
* @param $description
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description)
|
public function sendMusicNews($musicurl, $hqmusicurl, $thumb_media_id, $title, $description): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'music';
|
$this->msgData['msgtype'] = 'music';
|
||||||
$this->msgData['music[title]'] = $title;
|
$this->msgData['music'] = [
|
||||||
$this->msgData['music[description]'] = $description;
|
'title' => $title,
|
||||||
$this->msgData['music[musicurl]'] = $musicurl;
|
'description' => $description,
|
||||||
$this->msgData['music[hqmusicurl]'] = $hqmusicurl;
|
'musicurl' => $musicurl,
|
||||||
$this->msgData['music[thumb_media_id]'] = $thumb_media_id;
|
'hqmusicurl' => $hqmusicurl,
|
||||||
|
'thumb_media_id' => $thumb_media_id
|
||||||
|
];
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $head_content
|
* @param $head_content
|
||||||
* @param string $tail_content
|
* @param $tail_content
|
||||||
* @param array $menus
|
* @param array $menus
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendMenuNews(string $head_content, string $tail_content, array $menus = [])
|
public function sendMenuNews($head_content, $tail_content, array $menus = []): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'msgmenu';
|
$this->msgData['msgtype'] = 'msgmenu';
|
||||||
$this->msgData['msgmenu[head_content]'] = $head_content;
|
$this->msgData['msgmenu'] = [
|
||||||
$this->msgData['msgmenu[tail_content]'] = $tail_content;
|
'head_content' => $head_content,
|
||||||
|
'tail_content' => $tail_content,
|
||||||
|
];
|
||||||
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
||||||
throw new \Exception('菜单选项必须有2个');
|
throw new Exception('菜单选项必须有2个');
|
||||||
}
|
}
|
||||||
|
foreach ($menus as $val) {
|
||||||
foreach ($menus as $key => $val) {
|
|
||||||
$this->addNewsMenu($val['id'], $val['name']);
|
$this->addNewsMenu($val['id'], $val['name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
private $index = 0;
|
private int $index = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $menuName
|
* @param $menuName
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function addNewsMenu($id, $menuName)
|
public function addNewsMenu($id, $menuName): static
|
||||||
{
|
{
|
||||||
$this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id;
|
$lists['id'] = $id;
|
||||||
$this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName;
|
$lists['content'] = $menuName;
|
||||||
|
$this->msgData['msgmenu']['list'][$this->index] = $lists;
|
||||||
++$this->index;
|
++$this->index;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,109 +210,106 @@ class Message extends SmallProgram
|
|||||||
* @param $pagepath
|
* @param $pagepath
|
||||||
* @param $thumb_media_id
|
* @param $thumb_media_id
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id)
|
public function sendMiniprogrampageNews($title, $appid, $pagepath, $thumb_media_id): Result
|
||||||
{
|
{
|
||||||
$this->msgData['msgtype'] = 'msgmenu';
|
$this->msgData['msgtype'] = 'msgmenu';
|
||||||
$this->msgData['miniprogrampage[title]'] = $title;
|
$this->msgData['miniprogrampage'] = [
|
||||||
$this->msgData['miniprogrampage[appid]'] = $appid;
|
'title' => $title,
|
||||||
$this->msgData['miniprogrampage[pagepath]'] = $pagepath;
|
'appid' => $appid,
|
||||||
$this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id;
|
'pagepath' => $pagepath,
|
||||||
|
'thumb_media_id' => $thumb_media_id,
|
||||||
|
];
|
||||||
return $this->sendKefuMsg();
|
return $this->sendKefuMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filePath
|
* @param $filePath
|
||||||
* @param string $type
|
* @param $type
|
||||||
* @param bool $isPermanent
|
* @param bool $isPermanent
|
||||||
* @param string $title
|
* @param string $title
|
||||||
* @param string $introduction
|
* @param string $introduction
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '')
|
public function upload($filePath, $type, bool $isPermanent = false, string $title = '', string $introduction = ''): Result
|
||||||
{
|
{
|
||||||
if (!file_exists($filePath)) {
|
if (!file_exists($filePath)) {
|
||||||
throw new \Exception('文件不存在');
|
throw new Exception('文件不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
||||||
throw new \Exception('暂不支持的文件类型');
|
throw new Exception('暂不支持的文件类型');
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = $this->getAccessToken();
|
$token = $this->config->getAccessToken();
|
||||||
if ($isPermanent) {
|
if ($isPermanent) {
|
||||||
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
$url = "/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
||||||
} else {
|
} else {
|
||||||
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
$url = "/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
||||||
}
|
}
|
||||||
|
|
||||||
$mime = mime_content_type($filePath);
|
$mime = mime_content_type($filePath);
|
||||||
|
|
||||||
$real_path = new \CURLFile(realpath($filePath));
|
$real_path = new \CURLFile(realpath($filePath));
|
||||||
|
|
||||||
$data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime);
|
$data = ["media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime];
|
||||||
if ($isPermanent && $mime == 'video/mp3') {
|
if ($isPermanent && $mime == 'video/mp3') {
|
||||||
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||||
|
$client->post($url, $data);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
/** @var Result $body */
|
$this->msgData = [];
|
||||||
$data = $this->request->post($url, $data);
|
|
||||||
if (!$data->isResultsOK()) {
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
throw new \Exception($data->getMessage());
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data->getData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $mime
|
* @return array
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
private function checkExtinfo($mime)
|
public function getContents(): array
|
||||||
{
|
{
|
||||||
switch (strtolower($mime)) {
|
return $this->msgData;
|
||||||
case 'image/bmp':
|
|
||||||
case 'image/png':
|
|
||||||
case 'image/jpeg':
|
|
||||||
case 'image/jpg':
|
|
||||||
case 'image/gif':
|
|
||||||
break;
|
|
||||||
case 'mp3/wma/wav/amr':
|
|
||||||
break;
|
|
||||||
case 'mp4';
|
|
||||||
break;
|
|
||||||
case 'jpg';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new \Exception('不支持的文件格式');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function sendKefuMsg()
|
private function sendKefuMsg()
|
||||||
{
|
{
|
||||||
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
|
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
|
||||||
|
|
||||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setMethod(HttpClient::POST);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$this->request->setErrorField('errcode');
|
$client->post($url, $this->msgData);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->close();
|
||||||
|
|
||||||
/** @var Result $body */
|
$this->msgData = [];
|
||||||
$body = $this->request->post($url, $data);
|
|
||||||
|
|
||||||
if (!$body->isResultsOK()) {
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
throw new \Exception($body->getMessage());
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
}
|
}
|
||||||
return $body;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+158
-203
@@ -3,43 +3,44 @@
|
|||||||
|
|
||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
use wchat\common\Help;
|
||||||
|
|
||||||
class Notify extends SmallProgram
|
class Notify extends SmallProgram
|
||||||
{
|
{
|
||||||
public $appid = '';
|
public mixed $appid = '';
|
||||||
public $mch_id = '';
|
public mixed $mch_id = '';
|
||||||
public $device_info = '';
|
public mixed $device_info = '';
|
||||||
public $nonce_str = '';
|
public mixed $nonce_str = '';
|
||||||
public $sign = '';
|
public mixed $sign = '';
|
||||||
public $sign_type = '';
|
public mixed $sign_type = '';
|
||||||
public $result_code = '';
|
public mixed $result_code = '';
|
||||||
public $err_code = '';
|
public mixed $err_code = '';
|
||||||
public $err_code_des = '';
|
public mixed $err_code_des = '';
|
||||||
public $openid = '';
|
public mixed $openid = '';
|
||||||
public $is_subscribe = '';
|
public mixed $is_subscribe = '';
|
||||||
public $trade_type = '';
|
public mixed $trade_type = '';
|
||||||
public $bank_type = '';
|
public mixed $bank_type = '';
|
||||||
public $total_fee = '';
|
public mixed $total_fee = '';
|
||||||
public $settlement_total_fee = '';
|
public mixed $settlement_total_fee = '';
|
||||||
public $fee_type = '';
|
public mixed $fee_type = '';
|
||||||
public $cash_fee = '';
|
public mixed $cash_fee = '';
|
||||||
public $cash_fee_type = '';
|
public mixed $cash_fee_type = '';
|
||||||
public $coupon_fee = '';
|
public mixed $coupon_fee = '';
|
||||||
public $coupon_count = '';
|
public mixed $coupon_count = '';
|
||||||
public $coupon_type_n = '';
|
public mixed $coupon_type_n = '';
|
||||||
public $coupon_id_n = '';
|
public mixed $coupon_id_n = '';
|
||||||
public $coupon_fee_n = '';
|
public mixed $coupon_fee_n = '';
|
||||||
public $transaction_id = '';
|
public mixed $transaction_id = '';
|
||||||
public $out_trade_no = '';
|
public mixed $out_trade_no = '';
|
||||||
public $attach = '';
|
public mixed $attach = '';
|
||||||
public $time_end = '';
|
public mixed $time_end = '';
|
||||||
public $return_code = '';
|
public mixed $return_code = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* 判断是否完成支付
|
* 判断是否完成支付
|
||||||
*/
|
*/
|
||||||
public function isSuccess()
|
public function isSuccess(): bool
|
||||||
{
|
{
|
||||||
if ($this->getReturnCode() != "SUCCESS") {
|
if ($this->getReturnCode() != "SUCCESS") {
|
||||||
return false;
|
return false;
|
||||||
@@ -56,7 +57,7 @@ class Notify extends SmallProgram
|
|||||||
* @param array $params
|
* @param array $params
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setPayNotifyData(array $params)
|
public function setPayNotifyData(array $params): static
|
||||||
{
|
{
|
||||||
if (!$this->validation($params)) {
|
if (!$this->validation($params)) {
|
||||||
$this->setResultCode('FAIL');
|
$this->setResultCode('FAIL');
|
||||||
@@ -64,20 +65,30 @@ class Notify extends SmallProgram
|
|||||||
unset($params['result_code'], $params['err_code_des']);
|
unset($params['result_code'], $params['err_code_des']);
|
||||||
}
|
}
|
||||||
foreach ($params as $key => $val) {
|
foreach ($params as $key => $val) {
|
||||||
if (!property_exists($this, $key)) {
|
$this->__set($key, $val);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->$key = $val;
|
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, $value): void
|
||||||
|
{
|
||||||
|
if (property_exists($this, $name)) {
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validation(array $params)
|
public function validation(array $params): bool
|
||||||
{
|
{
|
||||||
$sign = $params['sign'];
|
$sign = $params['sign'];
|
||||||
unset($params['sign']);
|
unset($params['sign']);
|
||||||
@@ -92,507 +103,451 @@ class Notify extends SmallProgram
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getAppid()
|
public function getAppid(): mixed
|
||||||
{
|
{
|
||||||
return $this->appid;
|
return $this->appid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $appid
|
* @param mixed|string $appid
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setAppid($appid)
|
public function setAppid(mixed $appid): void
|
||||||
{
|
{
|
||||||
$this->appid = $appid;
|
$this->appid = $appid;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getMchId()
|
public function getMchId(): mixed
|
||||||
{
|
{
|
||||||
return $this->mch_id;
|
return $this->mch_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $mch_id
|
* @param mixed|string $mch_id
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setMchId($mch_id)
|
public function setMchId(mixed $mch_id): void
|
||||||
{
|
{
|
||||||
$this->mch_id = $mch_id;
|
$this->mch_id = $mch_id;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getDeviceInfo()
|
public function getDeviceInfo(): mixed
|
||||||
{
|
{
|
||||||
return $this->device_info;
|
return $this->device_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $device_info
|
* @param mixed|string $device_info
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setDeviceInfo($device_info)
|
public function setDeviceInfo(mixed $device_info): void
|
||||||
{
|
{
|
||||||
$this->device_info = $device_info;
|
$this->device_info = $device_info;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getNonceStr()
|
public function getNonceStr(): mixed
|
||||||
{
|
{
|
||||||
return $this->nonce_str;
|
return $this->nonce_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $nonce_str
|
* @param mixed|string $nonce_str
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setNonceStr($nonce_str)
|
public function setNonceStr(mixed $nonce_str): void
|
||||||
{
|
{
|
||||||
$this->nonce_str = $nonce_str;
|
$this->nonce_str = $nonce_str;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getSign()
|
public function getSign(): mixed
|
||||||
{
|
{
|
||||||
return $this->sign;
|
return $this->sign;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $sign
|
* @param mixed|string $sign
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setSign($sign)
|
public function setSign(mixed $sign): void
|
||||||
{
|
{
|
||||||
$this->sign = $sign;
|
$this->sign = $sign;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getSignType()
|
public function getSignType(): mixed
|
||||||
{
|
{
|
||||||
return $this->sign_type;
|
return $this->sign_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $sign_type
|
* @param mixed|string $sign_type
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setSignType($sign_type)
|
public function setSignType(mixed $sign_type): void
|
||||||
{
|
{
|
||||||
$this->sign_type = $sign_type;
|
$this->sign_type = $sign_type;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getResultCode()
|
public function getResultCode(): mixed
|
||||||
{
|
{
|
||||||
return $this->result_code;
|
return $this->result_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $result_code
|
* @param mixed|string $result_code
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setResultCode($result_code)
|
public function setResultCode(mixed $result_code): void
|
||||||
{
|
{
|
||||||
$this->result_code = $result_code;
|
$this->result_code = $result_code;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getErrCode()
|
public function getErrCode(): mixed
|
||||||
{
|
{
|
||||||
return $this->err_code;
|
return $this->err_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $err_code
|
* @param mixed|string $err_code
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setErrCode($err_code)
|
public function setErrCode(mixed $err_code): void
|
||||||
{
|
{
|
||||||
$this->err_code = $err_code;
|
$this->err_code = $err_code;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getErrCodeDes()
|
public function getErrCodeDes(): mixed
|
||||||
{
|
{
|
||||||
return $this->err_code_des;
|
return $this->err_code_des;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $err_code_des
|
* @param mixed|string $err_code_des
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setErrCodeDes($err_code_des)
|
public function setErrCodeDes(mixed $err_code_des): void
|
||||||
{
|
{
|
||||||
$this->err_code_des = $err_code_des;
|
$this->err_code_des = $err_code_des;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getOpenid()
|
public function getOpenid(): mixed
|
||||||
{
|
{
|
||||||
return $this->openid;
|
return $this->openid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $openid
|
* @param mixed|string $openid
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setOpenid($openid)
|
public function setOpenid(mixed $openid): void
|
||||||
{
|
{
|
||||||
$this->openid = $openid;
|
$this->openid = $openid;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getIsSubscribe()
|
public function getIsSubscribe(): mixed
|
||||||
{
|
{
|
||||||
return $this->is_subscribe;
|
return $this->is_subscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $is_subscribe
|
* @param mixed|string $is_subscribe
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setIsSubscribe($is_subscribe)
|
public function setIsSubscribe(mixed $is_subscribe): void
|
||||||
{
|
{
|
||||||
$this->is_subscribe = $is_subscribe;
|
$this->is_subscribe = $is_subscribe;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getTradeType()
|
public function getTradeType(): mixed
|
||||||
{
|
{
|
||||||
return $this->trade_type;
|
return $this->trade_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $trade_type
|
* @param mixed|string $trade_type
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setTradeType($trade_type)
|
public function setTradeType(mixed $trade_type): void
|
||||||
{
|
{
|
||||||
$this->trade_type = $trade_type;
|
$this->trade_type = $trade_type;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getBankType()
|
public function getBankType(): mixed
|
||||||
{
|
{
|
||||||
return $this->bank_type;
|
return $this->bank_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $bank_type
|
* @param mixed|string $bank_type
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setBankType($bank_type)
|
public function setBankType(mixed $bank_type): void
|
||||||
{
|
{
|
||||||
$this->bank_type = $bank_type;
|
$this->bank_type = $bank_type;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getTotalFee()
|
public function getTotalFee(): mixed
|
||||||
{
|
{
|
||||||
return $this->total_fee;
|
return $this->total_fee;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $total_fee
|
* @param mixed|string $total_fee
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setTotalFee($total_fee)
|
public function setTotalFee(mixed $total_fee): void
|
||||||
{
|
{
|
||||||
$this->total_fee = $total_fee;
|
$this->total_fee = $total_fee;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getSettlementTotalFee()
|
public function getSettlementTotalFee(): mixed
|
||||||
{
|
{
|
||||||
return $this->settlement_total_fee;
|
return $this->settlement_total_fee;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $settlement_total_fee
|
* @param mixed|string $settlement_total_fee
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setSettlementTotalFee($settlement_total_fee)
|
public function setSettlementTotalFee(mixed $settlement_total_fee): void
|
||||||
{
|
{
|
||||||
$this->settlement_total_fee = $settlement_total_fee;
|
$this->settlement_total_fee = $settlement_total_fee;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getFeeType()
|
public function getFeeType(): mixed
|
||||||
{
|
{
|
||||||
return $this->fee_type;
|
return $this->fee_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $fee_type
|
* @param mixed|string $fee_type
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setFeeType($fee_type)
|
public function setFeeType(mixed $fee_type): void
|
||||||
{
|
{
|
||||||
$this->fee_type = $fee_type;
|
$this->fee_type = $fee_type;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCashFee()
|
public function getCashFee(): mixed
|
||||||
{
|
{
|
||||||
return $this->cash_fee;
|
return $this->cash_fee;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $cash_fee
|
* @param mixed|string $cash_fee
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCashFee($cash_fee)
|
public function setCashFee(mixed $cash_fee): void
|
||||||
{
|
{
|
||||||
$this->cash_fee = $cash_fee;
|
$this->cash_fee = $cash_fee;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCashFeeType()
|
public function getCashFeeType(): mixed
|
||||||
{
|
{
|
||||||
return $this->cash_fee_type;
|
return $this->cash_fee_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $cash_fee_type
|
* @param mixed|string $cash_fee_type
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCashFeeType($cash_fee_type)
|
public function setCashFeeType(mixed $cash_fee_type): void
|
||||||
{
|
{
|
||||||
$this->cash_fee_type = $cash_fee_type;
|
$this->cash_fee_type = $cash_fee_type;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCouponFee()
|
public function getCouponFee(): mixed
|
||||||
{
|
{
|
||||||
return $this->coupon_fee;
|
return $this->coupon_fee;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $coupon_fee
|
* @param mixed|string $coupon_fee
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCouponFee($coupon_fee)
|
public function setCouponFee(mixed $coupon_fee): void
|
||||||
{
|
{
|
||||||
$this->coupon_fee = $coupon_fee;
|
$this->coupon_fee = $coupon_fee;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCouponCount()
|
public function getCouponCount(): mixed
|
||||||
{
|
{
|
||||||
return $this->coupon_count;
|
return $this->coupon_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $coupon_count
|
* @param mixed|string $coupon_count
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCouponCount($coupon_count)
|
public function setCouponCount(mixed $coupon_count): void
|
||||||
{
|
{
|
||||||
$this->coupon_count = $coupon_count;
|
$this->coupon_count = $coupon_count;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCouponTypeN()
|
public function getCouponTypeN(): mixed
|
||||||
{
|
{
|
||||||
return $this->coupon_type_n;
|
return $this->coupon_type_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $coupon_type_n
|
* @param mixed|string $coupon_type_n
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCouponTypeN($coupon_type_n)
|
public function setCouponTypeN(mixed $coupon_type_n): void
|
||||||
{
|
{
|
||||||
$this->coupon_type_n = $coupon_type_n;
|
$this->coupon_type_n = $coupon_type_n;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCouponIdN()
|
public function getCouponIdN(): mixed
|
||||||
{
|
{
|
||||||
return $this->coupon_id_n;
|
return $this->coupon_id_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $coupon_id_n
|
* @param mixed|string $coupon_id_n
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCouponIdN($coupon_id_n)
|
public function setCouponIdN(mixed $coupon_id_n): void
|
||||||
{
|
{
|
||||||
$this->coupon_id_n = $coupon_id_n;
|
$this->coupon_id_n = $coupon_id_n;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getCouponFeeN()
|
public function getCouponFeeN(): mixed
|
||||||
{
|
{
|
||||||
return $this->coupon_fee_n;
|
return $this->coupon_fee_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $coupon_fee_n
|
* @param mixed|string $coupon_fee_n
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setCouponFeeN($coupon_fee_n)
|
public function setCouponFeeN(mixed $coupon_fee_n): void
|
||||||
{
|
{
|
||||||
$this->coupon_fee_n = $coupon_fee_n;
|
$this->coupon_fee_n = $coupon_fee_n;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getTransactionId()
|
public function getTransactionId(): mixed
|
||||||
{
|
{
|
||||||
return $this->transaction_id;
|
return $this->transaction_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $transaction_id
|
* @param mixed|string $transaction_id
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setTransactionId($transaction_id)
|
public function setTransactionId(mixed $transaction_id): void
|
||||||
{
|
{
|
||||||
$this->transaction_id = $transaction_id;
|
$this->transaction_id = $transaction_id;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getOutTradeNo()
|
public function getOutTradeNo(): mixed
|
||||||
{
|
{
|
||||||
return $this->out_trade_no;
|
return $this->out_trade_no;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $out_trade_no
|
* @param mixed|string $out_trade_no
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setOutTradeNo($out_trade_no)
|
public function setOutTradeNo(mixed $out_trade_no): void
|
||||||
{
|
{
|
||||||
$this->out_trade_no = $out_trade_no;
|
$this->out_trade_no = $out_trade_no;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getAttach()
|
public function getAttach(): mixed
|
||||||
{
|
{
|
||||||
return $this->attach;
|
return $this->attach;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $attach
|
* @param mixed|string $attach
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setAttach($attach)
|
public function setAttach(mixed $attach): void
|
||||||
{
|
{
|
||||||
$this->attach = $attach;
|
$this->attach = $attach;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getTimeEnd()
|
public function getTimeEnd(): mixed
|
||||||
{
|
{
|
||||||
return $this->time_end;
|
return $this->time_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $time_end
|
* @param mixed|string $time_end
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setTimeEnd($time_end)
|
public function setTimeEnd(mixed $time_end): void
|
||||||
{
|
{
|
||||||
$this->time_end = $time_end;
|
$this->time_end = $time_end;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|string
|
||||||
*/
|
*/
|
||||||
public function getReturnCode()
|
public function getReturnCode(): mixed
|
||||||
{
|
{
|
||||||
return $this->return_code;
|
return $this->return_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $return_code
|
* @param mixed|string $return_code
|
||||||
* @return Notify
|
|
||||||
*/
|
*/
|
||||||
public function setReturnCode($return_code)
|
public function setReturnCode(mixed $return_code): void
|
||||||
{
|
{
|
||||||
$this->return_code = $return_code;
|
$this->return_code = $return_code;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-35
@@ -8,19 +8,20 @@
|
|||||||
|
|
||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
class PublicTemplate extends SmallProgram
|
class PublicTemplate extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $keywords = [];
|
private array $keywords = [];
|
||||||
private $templateId = '';
|
private string $templateId = '';
|
||||||
private $first = '';
|
private array $first = [];
|
||||||
private $remark = '';
|
private array $remark = [];
|
||||||
private $openId = '';
|
private string $openId = '';
|
||||||
private $defaultUrl = 'http://weixin.qq.com/download';
|
private string $defaultUrl = 'http://weixin.qq.com/download';
|
||||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
|
private string $sendUrl = '/cgi-bin/message/template/send';
|
||||||
private $miniprogram = [];
|
private array $miniprogram = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,25 +33,25 @@ class PublicTemplate extends SmallProgram
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $templateId
|
* @param $templateId
|
||||||
*/
|
*/
|
||||||
public function setTemplateId(string $templateId)
|
public function setTemplateId($templateId)
|
||||||
{
|
{
|
||||||
$this->templateId = $templateId;
|
$this->templateId = $templateId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $openId
|
* @param $openId
|
||||||
*/
|
*/
|
||||||
public function setOpenId(string $openId)
|
public function setOpenId($openId)
|
||||||
{
|
{
|
||||||
$this->openId = $openId;
|
$this->openId = $openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $defaultUrl
|
* @param $defaultUrl
|
||||||
*/
|
*/
|
||||||
public function setDefaultUrl(string $defaultUrl)
|
public function setDefaultUrl($defaultUrl)
|
||||||
{
|
{
|
||||||
$this->defaultUrl = $defaultUrl;
|
$this->defaultUrl = $defaultUrl;
|
||||||
}
|
}
|
||||||
@@ -60,7 +61,7 @@ class PublicTemplate extends SmallProgram
|
|||||||
* @param $context
|
* @param $context
|
||||||
* @param string $color
|
* @param string $color
|
||||||
*/
|
*/
|
||||||
public function replaceKeyword($name, $context, $color = '')
|
public function replaceKeyword($name, $context, string $color = '')
|
||||||
{
|
{
|
||||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||||
}
|
}
|
||||||
@@ -85,8 +86,9 @@ class PublicTemplate extends SmallProgram
|
|||||||
/**
|
/**
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param string $color
|
* @param string $color
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function setFirst($context, $color = '#f00')
|
public function setFirst($context, string $color = '#f00'): void
|
||||||
{
|
{
|
||||||
$this->first = [
|
$this->first = [
|
||||||
'value' => $context,
|
'value' => $context,
|
||||||
@@ -97,8 +99,9 @@ class PublicTemplate extends SmallProgram
|
|||||||
/**
|
/**
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param string $color
|
* @param string $color
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function setRemark($context, $color = '#000')
|
public function setRemark($context, string $color = '#000'): void
|
||||||
{
|
{
|
||||||
$this->remark = [
|
$this->remark = [
|
||||||
'value' => $context,
|
'value' => $context,
|
||||||
@@ -108,12 +111,13 @@ class PublicTemplate extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $appid
|
* @param $appid
|
||||||
* @param $pagepath
|
* @param string $pagepath
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function setMiniprogram($appid, $pagepath)
|
public function setMiniprogram($appid, string $pagepath): void
|
||||||
{
|
{
|
||||||
$this->miniprogram = [
|
$this->miniprogram = [
|
||||||
'appid' => $appid,
|
'appid' => $appid,
|
||||||
'pagepath' => $pagepath
|
'pagepath' => $pagepath
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -124,34 +128,37 @@ class PublicTemplate extends SmallProgram
|
|||||||
*
|
*
|
||||||
* 奴隶交易通知
|
* 奴隶交易通知
|
||||||
*/
|
*/
|
||||||
public function sendTemplate()
|
public function sendTemplate(): Result
|
||||||
{
|
{
|
||||||
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
|
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
||||||
|
|
||||||
$keywords = $this->keywords;
|
$keywords = $this->keywords;
|
||||||
$keywords['first'] = $this->first;
|
$keywords['first'] = $this->first;
|
||||||
$keywords['remark'] = $this->remark;
|
$keywords['remark'] = $this->remark;
|
||||||
|
|
||||||
$default = [
|
$default = [
|
||||||
"touser" => $this->openId,
|
"touser" => $this->openId,
|
||||||
"template_id" => $this->templateId,
|
"template_id" => $this->templateId,
|
||||||
"url" => $this->defaultUrl,
|
"url" => $this->defaultUrl,
|
||||||
"data" => $keywords,
|
"data" => $keywords,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!empty($this->miniprogram)) {
|
if (!empty($this->miniprogram)) {
|
||||||
$default['miniprogram'] = $this->miniprogram;
|
$default['miniprogram'] = $this->miniprogram;
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->post($url, $default);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
$this->request->setIsSSL(true);
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
$this->request->addHeader('Content-Type', 'application/json');
|
return new Result(code: 505, message: 'network error.');
|
||||||
$this->request->setErrorField('errcode');
|
}
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$body = json_decode($client->getBody(), true);
|
||||||
$result = $this->request->post($url, $params);
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
$result->append('postBody', $default);
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
return $result;
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,184 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/3/26 0026
|
|
||||||
* Time: 10:22
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace wchat\wx;
|
|
||||||
|
|
||||||
use wchat\common\HttpClient;
|
|
||||||
use wchat\common\Result;
|
|
||||||
use wchat\common\Help;
|
|
||||||
|
|
||||||
class Recharge extends SmallProgram
|
|
||||||
{
|
|
||||||
private $money = 0;
|
|
||||||
|
|
||||||
private $orderNo;
|
|
||||||
|
|
||||||
private $data = [];
|
|
||||||
|
|
||||||
private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
|
|
||||||
private $unifiedorder = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $money
|
|
||||||
* @param string $orderNo
|
|
||||||
* @param string $openId
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function recharge(int $money, string $orderNo, $openId = '')
|
|
||||||
{
|
|
||||||
if ($money < 0) {
|
|
||||||
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
|
|
||||||
}
|
|
||||||
$this->money = $money;
|
|
||||||
$this->orderNo = $orderNo;
|
|
||||||
$this->data['openid'] = $openId;
|
|
||||||
|
|
||||||
$config = $this->config;
|
|
||||||
$this->request->setCallback(function ($result, $body) use ($config) {
|
|
||||||
$data = Help::toArray($result);
|
|
||||||
if (isset($data['sign'])) {
|
|
||||||
$sign = $data['sign'];
|
|
||||||
unset($data['sign']);
|
|
||||||
}
|
|
||||||
$return = [];
|
|
||||||
$_sign = Help::sign($data, $config->getKey(), $config->getSignType());
|
|
||||||
if (!isset($sign) || $sign != $_sign) {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
|
|
||||||
} else {
|
|
||||||
$return['code'] = 0;
|
|
||||||
$return['data'] = $data;
|
|
||||||
$return['data']['postBody'] = $body;
|
|
||||||
if ($data['return_code'] == 'FAIL') {
|
|
||||||
$return['code'] = -1;
|
|
||||||
$return['message'] = $data['return_msg'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Result($return);
|
|
||||||
});
|
|
||||||
return $this->send($this->unifiedorder, $this->builder());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 'appId' => $result['appid'],
|
|
||||||
* 'nonceStr' => $result['nonce_str'],
|
|
||||||
* 'package' => 'prepay_id=' . $result['prepay_id'],
|
|
||||||
* 'signType' => 'MD5',
|
|
||||||
* 'timeStamp' => (string)time(),
|
|
||||||
* @param $prepay_id
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function reception($prepay_id)
|
|
||||||
{
|
|
||||||
$array = [
|
|
||||||
'appId' => $this->config->getAppid(),
|
|
||||||
'nonceStr' => Help::random(32),
|
|
||||||
'package' => 'prepay_id=' . $prepay_id,
|
|
||||||
'signType' => 'MD5',
|
|
||||||
'timeStamp' => (string)time(),
|
|
||||||
];
|
|
||||||
$key = $this->config->getKey();
|
|
||||||
$sign_type = $this->config->getSignType();
|
|
||||||
$array['paySign'] = Help::sign($array, $key, $sign_type);
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function builder()
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'mch_id' => $this->config->getMchId(),
|
|
||||||
'nonce_str' => Help::random(32),
|
|
||||||
'body' => $this->config->getBody(),
|
|
||||||
'out_trade_no' => $this->orderNo,
|
|
||||||
'total_fee' => $this->money,
|
|
||||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
|
||||||
'notify_url' => $this->config->getNotifyUrl(),
|
|
||||||
'trade_type' => $this->config->getTradeType(),
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->data = array_merge($data, $this->data);
|
|
||||||
|
|
||||||
$key = $this->config->getKey();
|
|
||||||
$sign_type = $this->config->getSignType();
|
|
||||||
|
|
||||||
$this->data['sign_type'] = $this->config->getSignType();
|
|
||||||
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
|
|
||||||
|
|
||||||
return Help::toXml($this->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSignData()
|
|
||||||
{
|
|
||||||
$data = $this->data;
|
|
||||||
$data['key'] = $this->config->getKey();
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $money
|
|
||||||
* @param $openid
|
|
||||||
* @param $order
|
|
||||||
* @param $desc
|
|
||||||
* @return Result
|
|
||||||
* @throws
|
|
||||||
*
|
|
||||||
* 提现
|
|
||||||
*/
|
|
||||||
public function cashWithdrawal($money, $openid, $order, $desc = '零钱提现')
|
|
||||||
{
|
|
||||||
$array = [
|
|
||||||
'nonce_str' => Help::random(32),
|
|
||||||
'partner_trade_no' => $order,
|
|
||||||
'mchid' => $this->config->getMchId(),
|
|
||||||
'mch_appid' => $this->config->getAppid(),
|
|
||||||
'openid' => $openid,
|
|
||||||
'check_name' => 'NO_CHECK',
|
|
||||||
'amount' => $money * 100,
|
|
||||||
'spbill_create_ip' => $this->config->getRemoteAddr(),
|
|
||||||
'desc' => $desc,
|
|
||||||
];
|
|
||||||
|
|
||||||
$key = $this->config->getKey();
|
|
||||||
$sign_type = $this->config->getSignType();
|
|
||||||
$array['sign'] = Help::sign($array, $key, $sign_type);
|
|
||||||
|
|
||||||
$this->request->setCallback(function ($data) {
|
|
||||||
$array = Help::toArray($data);
|
|
||||||
if ($array['return_code'] != 'SUCCESS') {
|
|
||||||
$data = ['code' => $array['return_code'], 'message' => $array['return_msg']];
|
|
||||||
} else if ($array['result_code'] != 'SUCCESS') {
|
|
||||||
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
|
|
||||||
} else {
|
|
||||||
$data = ['code' => 0, 'message' => '支付成功'];
|
|
||||||
}
|
|
||||||
return new Result($data);
|
|
||||||
});
|
|
||||||
return $this->send($this->transfers, Help::toXml($array));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
private function send($url, $data)
|
|
||||||
{
|
|
||||||
$this->request->setIsSSL(true);
|
|
||||||
$this->request->setMethod(HttpClient::POST);
|
|
||||||
$this->request->addHeader('Content-Type', 'text/xml');
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
return $this->request->send($url, $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+57
-28
@@ -4,6 +4,7 @@
|
|||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,29 +14,41 @@ use wchat\common\Result;
|
|||||||
class SecCheck extends SmallProgram
|
class SecCheck extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $_url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=';
|
private string $_url = '/wxa/img_sec_check?access_token=';
|
||||||
|
|
||||||
private $_msgUrl = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=';
|
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
||||||
|
|
||||||
private $_mediaCheckAsync = 'https://api.weixin.qq.com/wxa/media_check_async?access_token=';
|
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
||||||
|
|
||||||
const MEDIA_VIDEO = 1;
|
const MEDIA_VIDEO = 1;
|
||||||
const MEDIA_IMAGE = 1;
|
const MEDIA_IMAGE = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return array|Result|mixed
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function image($path = '')
|
public function image(string $path = ''): Result
|
||||||
{
|
{
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
return $this->sendError('文件不存在', 404);
|
return $this->sendError('文件不存在', 404);
|
||||||
}
|
}
|
||||||
$this->request->setUseSwoole(false);
|
$access_token = $this->config->getAccessToken();
|
||||||
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
|
||||||
$this->request->setErrorField('errcode');
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||||
return $this->request->post($this->_url . $this->config->getAccessToken(), ['media' => new \CURLFile($path)]);
|
$client->upload($this->_url . '?access_token=' . $access_token, [
|
||||||
|
'media' => new \CURLFile($path)
|
||||||
|
]);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
}
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -45,22 +58,26 @@ class SecCheck extends SmallProgram
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function mediaAsync(string $url, $type = SecCheck::MEDIA_IMAGE)
|
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
|
||||||
{
|
{
|
||||||
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
||||||
throw new \Exception('暂不支持的文件类型');
|
throw new \Exception('暂不支持的文件类型');
|
||||||
}
|
}
|
||||||
$this->request->setHeader('Content-Type', 'application/json');
|
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
||||||
$this->request->setErrorField('errcode');
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
$response = $this->request->post($this->_mediaCheckAsync . $this->config->getAccessToken(), [
|
$client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
|
||||||
'media_url' => $url,
|
$client->close();
|
||||||
'media_type' => $type
|
|
||||||
]);
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
if (!$response->isResultsOK()) {
|
return new Result(code: 505, message: 'network error.');
|
||||||
throw new \Exception($response->getMessage());
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
}
|
}
|
||||||
return $response->getData('trace_id');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,7 +85,7 @@ class SecCheck extends SmallProgram
|
|||||||
* @param $params
|
* @param $params
|
||||||
* @return ContentAsyncCheck|null
|
* @return ContentAsyncCheck|null
|
||||||
*/
|
*/
|
||||||
public function readByEvent($params)
|
public function readByEvent($params): ?ContentAsyncCheck
|
||||||
{
|
{
|
||||||
return ContentAsyncCheck::instance($params);
|
return ContentAsyncCheck::instance($params);
|
||||||
}
|
}
|
||||||
@@ -76,17 +93,29 @@ class SecCheck extends SmallProgram
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $content
|
* @param $content
|
||||||
* @return array|Result|mixed
|
* @return Result
|
||||||
*/
|
*/
|
||||||
public function text($content)
|
public function text($content): Result
|
||||||
{
|
{
|
||||||
if (empty($content)) {
|
if (empty($content)) {
|
||||||
return $this->sendError('文件不存在', 404);
|
return $this->sendError('文件不存在', 404);
|
||||||
}
|
}
|
||||||
$this->request->setHeader('Content-Type', 'application/json');
|
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
return $this->request->post($this->_msgUrl . $this->config->getAccessToken(), ['content' => $content]);
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->post($requestUrl, ['content' => $content]);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,37 +4,8 @@
|
|||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
use wchat\common\Miniprogarampage;
|
use wchat\common\Multiprogramming;
|
||||||
|
|
||||||
class SmallProgram extends Miniprogarampage
|
class SmallProgram extends Multiprogramming
|
||||||
{
|
{
|
||||||
|
|
||||||
protected static $instance;
|
|
||||||
|
|
||||||
private $url = 'https://api.weixin.qq.com/cgi-bin/token';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function generateAccess_token()
|
|
||||||
{
|
|
||||||
if (!empty($this->config->getToken())) {
|
|
||||||
return $this->config->getToken();
|
|
||||||
}
|
|
||||||
$query = [
|
|
||||||
'grant_type' => 'client_credential',
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'secret' => $this->config->getAppsecret()
|
|
||||||
];
|
|
||||||
$this->request->setErrorField('errcode');
|
|
||||||
$this->request->setErrorMsgField('errmsg');
|
|
||||||
$param = $this->request->get($this->url, $query);
|
|
||||||
if (!$param->isResultsOK()) {
|
|
||||||
throw new \Exception($param->getMessage());
|
|
||||||
}
|
|
||||||
return $param->getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-2
@@ -6,16 +6,28 @@ namespace wchat\wx;
|
|||||||
|
|
||||||
use wchat\common\Result;
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Subject
|
||||||
|
* @package wchat\wx
|
||||||
|
*/
|
||||||
class Subject extends \wchat\base\Subject
|
class Subject extends \wchat\base\Subject
|
||||||
{
|
{
|
||||||
|
|
||||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUrl(): string
|
public function getUrl(): string
|
||||||
{
|
{
|
||||||
return $this->sendUrl;
|
return 'cgi-bin/message/subscribe/send';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getHost(): string
|
||||||
|
{
|
||||||
|
return 'api.weixin.qq.com';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-2
@@ -14,11 +14,23 @@ namespace wchat\wx;
|
|||||||
*/
|
*/
|
||||||
class Template extends \wchat\base\Template
|
class Template extends \wchat\base\Template
|
||||||
{
|
{
|
||||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getUrl(): string
|
public function getUrl(): string
|
||||||
{
|
{
|
||||||
return $this->sendUrl;
|
return '/cgi-bin/message/wxopen/template/send';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getHost(): string
|
||||||
|
{
|
||||||
|
return 'api.weixin.qq.com';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,35 @@
|
|||||||
namespace wchat\wx;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
class Token extends SmallProgram
|
class Token extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function token(): Result
|
||||||
|
{
|
||||||
|
$query = [
|
||||||
|
'grant_type' => 'client_credential',
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'secret' => $this->config->getAppsecret()
|
||||||
|
];
|
||||||
|
$client = new Client('api.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->get('cgi-bin/token', $query);
|
||||||
|
$client->close();
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$body = json_decode($client->getBody(), true);
|
||||||
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
||||||
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
||||||
|
}
|
||||||
|
return new Result(code: 0, data: $body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V2;
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
|
use wchat\common\Help;
|
||||||
|
use wchat\common\Result;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV2AppPayment extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private string $uniformed = '/pay/unifiedorder';
|
||||||
|
|
||||||
|
use WxV2PayTait;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $money
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param string $spbill_create_ip
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function payment(int $money, string $orderNo, string $spbill_create_ip = '127.0.0.1'): Result
|
||||||
|
{
|
||||||
|
if ($money < 0) {
|
||||||
|
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $this->getInitCore($orderNo, $money);
|
||||||
|
$body['trade_type'] = 'APP';
|
||||||
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->withBody($this->sign($body));
|
||||||
|
$client->post($this->uniformed);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$data = Help::toArray($client->getBody());
|
||||||
|
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 503, message: $data['return_msg']);
|
||||||
|
}
|
||||||
|
if ($data['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 504, message: $data['err_code_des']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $prepay_id
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function reception(string $prepay_id): string
|
||||||
|
{
|
||||||
|
return $this->sign([
|
||||||
|
'appId' => $this->config->getAppid(),
|
||||||
|
'partnerid' => $this->config->getMchId(),
|
||||||
|
'prepayid' => $prepay_id,
|
||||||
|
'package' => 'Sign=WXPay',
|
||||||
|
'noncestr' => Help::random(32),
|
||||||
|
'timestamp' => time()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,210 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V2;
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
|
use Phalcon\Cache\Frontend\Json;
|
||||||
|
use wchat\common\Help;
|
||||||
|
use wchat\common\Result;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV2PayJsApi extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
private string $uniformed = '/pay/unifiedorder';
|
||||||
|
|
||||||
|
use WxV2PayTait;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $money
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param string $openId
|
||||||
|
* @param string $spbill_create_ip
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function applet(int $money, string $orderNo, string $openId, string $spbill_create_ip = '127.0.0.1'): Result
|
||||||
|
{
|
||||||
|
if ($money < 0) {
|
||||||
|
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $this->getInitCore($orderNo, $money);
|
||||||
|
$body['trade_type'] = 'JSAPI';
|
||||||
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||||
|
$body['openid'] = $openId;
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->withBody($this->sign($body));
|
||||||
|
$client->post($this->uniformed);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$data = Help::toArray($client->getBody());
|
||||||
|
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 503, message: $data['return_msg']);
|
||||||
|
}
|
||||||
|
if ($data['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 504, message: $data['err_code_des']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $money
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param string $app_name
|
||||||
|
* @param string $package_name
|
||||||
|
* @param string $spbill_create_ip
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function h5Android(int $money, string $orderNo, string $app_name, string $package_name, string $spbill_create_ip = '127.0.0.1'): Result
|
||||||
|
{
|
||||||
|
if ($money < 0) {
|
||||||
|
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $this->getInitCore($orderNo, $money);
|
||||||
|
$body['trade_type'] = 'MWEB';
|
||||||
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||||
|
$body['scene_info'] = json_encode([
|
||||||
|
'h5_info' => [
|
||||||
|
'type' => 'Android',
|
||||||
|
'app_name' => $app_name,
|
||||||
|
'package_name' => $package_name
|
||||||
|
]
|
||||||
|
], JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->withBody($this->sign($body));
|
||||||
|
$client->post($this->uniformed);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$data = Help::toArray($client->getBody());
|
||||||
|
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 503, message: $data['return_msg']);
|
||||||
|
}
|
||||||
|
if ($data['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 504, message: $data['err_code_des']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $money
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param string $app_name
|
||||||
|
* @param string $bundle_id
|
||||||
|
* @param string $spbill_create_ip
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function h5Ios(int $money, string $orderNo, string $app_name, string $bundle_id, string $spbill_create_ip = '127.0.0.1'): Result
|
||||||
|
{
|
||||||
|
if ($money < 0) {
|
||||||
|
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $this->getInitCore($orderNo, $money);
|
||||||
|
$body['trade_type'] = 'MWEB';
|
||||||
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||||
|
$body['scene_info'] = json_encode([
|
||||||
|
'h5_info' => [
|
||||||
|
'type' => 'IOS',
|
||||||
|
'app_name' => $app_name,
|
||||||
|
'bundle_id' => $bundle_id
|
||||||
|
]
|
||||||
|
], JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->withBody($this->sign($body));
|
||||||
|
$client->post($this->uniformed);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$data = Help::toArray($client->getBody());
|
||||||
|
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 503, message: $data['return_msg']);
|
||||||
|
}
|
||||||
|
if ($data['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 504, message: $data['err_code_des']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $money
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param string $wap_url
|
||||||
|
* @param string $wap_name
|
||||||
|
* @param string $spbill_create_ip
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function h5(int $money, string $orderNo, string $wap_url, string $wap_name, string $spbill_create_ip = '127.0.0.1'): Result
|
||||||
|
{
|
||||||
|
if ($money < 0) {
|
||||||
|
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $this->getInitCore($orderNo, $money);
|
||||||
|
$body['trade_type'] = 'MWEB';
|
||||||
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||||
|
$body['scene_info'] = json_encode([
|
||||||
|
'h5_info' => [
|
||||||
|
'type' => 'IOS',
|
||||||
|
'wap_url' => $wap_url,
|
||||||
|
'wap_name' => $wap_name
|
||||||
|
]
|
||||||
|
], JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->withBody($this->sign($body));
|
||||||
|
$client->post($this->uniformed);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
$data = Help::toArray($client->getBody());
|
||||||
|
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 503, message: $data['return_msg']);
|
||||||
|
}
|
||||||
|
if ($data['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: 504, message: $data['err_code_des']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $prepay_id
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function reception(string $prepay_id): string
|
||||||
|
{
|
||||||
|
return $this->sign([
|
||||||
|
'signType' => $this->config->getSignType(),
|
||||||
|
'package' => 'prepay_id=' . $prepay_id,
|
||||||
|
'nonceStr' => Help::random(32),
|
||||||
|
'timestamp' => time()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V2;
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
use wchat\common\Help;
|
||||||
|
|
||||||
|
trait WxV2PayTait
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 'appId' => $result['appid'],
|
||||||
|
* 'nonceStr' => $result['nonce_str'],
|
||||||
|
* 'package' => 'prepay_id=' . $result['prepay_id'],
|
||||||
|
* 'signType' => 'MD5',
|
||||||
|
* 'timeStamp' => (string)time(),
|
||||||
|
* @param $prepay_id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[ArrayShape(['appId' => "string", 'nonceStr' => "string", 'package' => "string", 'signType' => "string", 'timeStamp' => "string", 'paySign' => "string"])]
|
||||||
|
public function reception($prepay_id): array
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'appId' => $this->config->getAppid(),
|
||||||
|
'nonceStr' => Help::random(32),
|
||||||
|
'package' => 'prepay_id=' . $prepay_id,
|
||||||
|
'signType' => 'MD5',
|
||||||
|
'timeStamp' => (string)time(),
|
||||||
|
];
|
||||||
|
$key = $this->config->getKey();
|
||||||
|
$sign_type = $this->config->getSignType();
|
||||||
|
$array['paySign'] = Help::sign($array, $key, $sign_type);
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param float|int $total
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[ArrayShape(['appid' => "string", 'mch_id' => "string", 'nonce_str' => "string", 'body' => "string", 'out_trade_no' => "string", 'total_fee' => "float|int", 'spbill_create_ip' => "mixed", 'notify_url' => "string", 'trade_type' => "string"])]
|
||||||
|
protected function getInitCore(string $orderNo, float|int $total): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'mch_id' => $this->config->getMchId(),
|
||||||
|
'nonce_str' => Help::random(32),
|
||||||
|
'body' => $this->config->getBody(),
|
||||||
|
'out_trade_no' => $orderNo,
|
||||||
|
'total_fee' => $total,
|
||||||
|
'notify_url' => $this->config->getNotifyUrl(),
|
||||||
|
'trade_type' => $this->config->getTradeType(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function sign(array $data): string
|
||||||
|
{
|
||||||
|
$key = $this->config->getKey();
|
||||||
|
$sign_type = $this->config->getSignType();
|
||||||
|
|
||||||
|
$data['sign'] = Help::sign($data, $key, $sign_type);
|
||||||
|
|
||||||
|
return Help::toXml($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $orderNo
|
||||||
|
* @param int|float $money
|
||||||
|
* @param string $openid
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function builder(string $orderNo, int|float $money, string $openid): string
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'mch_id' => $this->config->getMchId(),
|
||||||
|
'nonce_str' => Help::random(32),
|
||||||
|
'body' => $this->config->getBody(),
|
||||||
|
'out_trade_no' => $orderNo,
|
||||||
|
'total_fee' => $money,
|
||||||
|
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||||
|
'notify_url' => $this->config->getNotifyUrl(),
|
||||||
|
'trade_type' => $this->config->getTradeType(),
|
||||||
|
'openid' => $openid
|
||||||
|
];
|
||||||
|
|
||||||
|
$key = $this->config->getKey();
|
||||||
|
$sign_type = $this->config->getSignType();
|
||||||
|
|
||||||
|
$data['sign'] = Help::sign($data, $key, $sign_type);
|
||||||
|
|
||||||
|
return Help::toXml($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V2;
|
||||||
|
|
||||||
|
use Kiri\Client;
|
||||||
|
use wchat\common\Help;
|
||||||
|
use wchat\common\Result;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV2Withdrawal extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
private string $transfers = '/mmpaymkttransfers/promotion/transfers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int|float $money
|
||||||
|
* @param string $openid
|
||||||
|
* @param string $order
|
||||||
|
* @param string $desc
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function payment(int|float $money, string $openid, string $order, string $desc = '零钱提现'): Result
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'nonce_str' => Help::random(32),
|
||||||
|
'partner_trade_no' => $order,
|
||||||
|
'mchid' => $this->config->getMchId(),
|
||||||
|
'mch_appid' => $this->config->getAppid(),
|
||||||
|
'openid' => $openid,
|
||||||
|
'check_name' => 'NO_CHECK',
|
||||||
|
'amount' => $money * 100,
|
||||||
|
'spbill_create_ip' => $this->config->getRemoteAddr(),
|
||||||
|
'desc' => $desc,
|
||||||
|
];
|
||||||
|
|
||||||
|
$key = $this->config->getKey();
|
||||||
|
$sign_type = $this->config->getSignType();
|
||||||
|
$array['sign'] = Help::sign($array, $key, $sign_type);
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||||
|
$client->withHeader(['Content-Type' => 'application/json']);
|
||||||
|
$client->withBody($body = Help::toXml($array));
|
||||||
|
$client->post($this->transfers);
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||||
|
return new Result(code: 505, message: 'network error.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = Help::toArray($client->getBody());
|
||||||
|
|
||||||
|
$data['body'] = $body;
|
||||||
|
if ($data['return_code'] == 'FAIL') {
|
||||||
|
return new Result(code: $array['return_code'], message: $data['return_msg'], data: $data);
|
||||||
|
} else if ($array['result_code'] != 'SUCCESS') {
|
||||||
|
return new Result(code: $array['err_code'], message: $data['err_code_des'], data: $data);
|
||||||
|
} else {
|
||||||
|
return new Result(code: 0, message: '提现成功', data: $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V3;
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
|
||||||
|
class TransferDetail
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public string $out_detail_no,
|
||||||
|
public int|float $transfer_amount,
|
||||||
|
public string $transfer_remark,
|
||||||
|
public string $openid,
|
||||||
|
public string $user_name
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[ArrayShape(['out_detail_no' => "string", 'transfer_amount' => "float|int", 'transfer_remark' => "string", 'openid' => "string", 'user_name' => "string"])]
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'out_detail_no' => $this->out_detail_no,
|
||||||
|
'transfer_amount' => $this->transfer_amount,
|
||||||
|
'transfer_remark' => $this->transfer_remark,
|
||||||
|
'openid' => $this->openid,
|
||||||
|
'user_name' => $this->user_name,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V3;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV3AppPayment extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
use WxV3PaymentTait;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $orderNo
|
||||||
|
* @param int $total
|
||||||
|
* @param string|null $openId
|
||||||
|
* @param string $payer_client_ip
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||||
|
{
|
||||||
|
$body = $this->getInitCore($orderNo, $total);
|
||||||
|
|
||||||
|
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||||
|
|
||||||
|
$sign = $this->signature('POST', '/v3/pay/transactions/components', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||||
|
$client->withAddedHeader('Authorization', $sign)->withContentType('application/json')
|
||||||
|
->withAgent('application/json')->withBody($json)
|
||||||
|
->post('/v3/pay/transactions/components');
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
$json = json_decode($client->getBody(), TRUE);
|
||||||
|
if (!isset($json['prepay_id'])) {
|
||||||
|
throw new Exception('微信支付调用失败');
|
||||||
|
}
|
||||||
|
return $this->createResponse($json, $body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V3;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
|
use Kiri\Message\Stream;
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV3NativePayment extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
use WxV3PaymentTait;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $orderNo
|
||||||
|
* @param int $total
|
||||||
|
* @param string|null $openId
|
||||||
|
* @param string $payer_client_ip
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
#[ArrayShape(['code_url' => "string"])]
|
||||||
|
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||||
|
{
|
||||||
|
$body = $this->getInitCore($orderNo, $total);
|
||||||
|
|
||||||
|
$sign = $this->signature('POST', '/v3/pay/transactions/native', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||||
|
$client->withAddedHeader('Authorization', $sign)->withContentType('application/json')
|
||||||
|
->withAgent('application/json')->withBody(new Stream($json))
|
||||||
|
->post('/v3/pay/transactions/native');
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
$json = json_decode($client->getBody(), TRUE);
|
||||||
|
if (!isset($json['code_url'])) {
|
||||||
|
throw new Exception('微信支付调用失败');
|
||||||
|
}
|
||||||
|
return ['code_url' => $json['code_url']];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V3;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Client;
|
||||||
|
use Kiri\Message\Stream;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV3Payment extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
use WxV3PaymentTait;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $orderNo
|
||||||
|
* @param int $total
|
||||||
|
* @param string|null $openId
|
||||||
|
* @param string $payer_client_ip
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||||
|
{
|
||||||
|
$body = $this->getInitCore($orderNo, $total);
|
||||||
|
$body['payer'] = ['openid' => $openId];
|
||||||
|
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||||
|
|
||||||
|
$sign = $this->signature('POST', '/v3/pay/transactions/jsapi', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||||
|
$client->withAddedHeader('Authorization', $sign)
|
||||||
|
->withContentType('application/json')
|
||||||
|
->withAddedHeader('User-Agent', 'application/json')
|
||||||
|
->withBody(new Stream($json))
|
||||||
|
->post('/v3/pay/transactions/jsapi');
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
$json = json_decode($client->getBody(), TRUE);
|
||||||
|
if (!isset($json['prepay_id'])) {
|
||||||
|
throw new Exception('微信支付调用失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->createResponse($json, $body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V3;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use wchat\common\Help;
|
||||||
|
use function Sodium\crypto_aead_aes256gcm_decrypt;
|
||||||
|
use function Sodium\crypto_aead_aes256gcm_is_available;
|
||||||
|
|
||||||
|
const KEY_LENGTH_BYTE = 32;
|
||||||
|
const AUTH_TAG_LENGTH_BYTE = 16;
|
||||||
|
|
||||||
|
trait WxV3PaymentTait
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $orderNo
|
||||||
|
* @param $total
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getInitCore($orderNo, $total): array
|
||||||
|
{
|
||||||
|
$body['appid'] = $this->getConfig()->getAppid();
|
||||||
|
$body['mchid'] = $this->getConfig()->getMchId();
|
||||||
|
$body['description'] = $this->getConfig()->getBody();
|
||||||
|
$body['out_trade_no'] = $orderNo;
|
||||||
|
$body['notify_url'] = $this->getConfig()->getNotifyUrl();
|
||||||
|
$body['amount'] = ['total' => $total, 'currency' => 'CNY'];
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $http_method
|
||||||
|
* @param string $canonical_url
|
||||||
|
* @param string $body
|
||||||
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function signature(string $http_method, string $canonical_url, string $body = ''): string
|
||||||
|
{
|
||||||
|
$message = $http_method . "\n" . $canonical_url . "\n" . ($time = time()) . "\n" . ($rand = md5(random_bytes(32))) . "\n" . $body . "\n";
|
||||||
|
|
||||||
|
$sign = $this->openssl_signature($message);
|
||||||
|
|
||||||
|
return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $this->getConfig()->getSchema(),
|
||||||
|
$this->getConfig()->getMchId(), $rand, $time, $this->getConfig()->getSerialNo(), $sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $body
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function openssl_signature($body): string
|
||||||
|
{
|
||||||
|
$pem = file_get_contents($this->getConfig()->getSslCert());
|
||||||
|
|
||||||
|
$mch_private_key = openssl_get_privatekey($pem);
|
||||||
|
|
||||||
|
openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
|
||||||
|
return base64_encode($raw_sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $json
|
||||||
|
* @param $body
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function createResponse($json, $body): array
|
||||||
|
{
|
||||||
|
$responseArray['appId'] = $body['appid'];
|
||||||
|
$responseArray['timeStamp'] = (string)time();
|
||||||
|
$responseArray['nonceStr'] = Help::random(32);
|
||||||
|
$responseArray['package'] = "prepay_id=" . $json['prepay_id'];
|
||||||
|
|
||||||
|
$responseBody = $responseArray['appId'] . PHP_EOL . $responseArray['timeStamp'] . PHP_EOL . $responseArray['nonceStr'] . PHP_EOL . $responseArray['package'] . PHP_EOL;
|
||||||
|
|
||||||
|
$responseArray['signType'] = 'RSA';
|
||||||
|
$responseArray['signBody'] = $responseBody;
|
||||||
|
$responseArray['paySign'] = $this->openssl_signature($responseBody);
|
||||||
|
$responseArray['prepay_id'] = $json['prepay_id'];
|
||||||
|
|
||||||
|
return $responseArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $associatedData
|
||||||
|
* @param $nonceStr
|
||||||
|
* @param $ciphertext
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
|
public function decryptToString($associatedData, $nonceStr, $ciphertext): bool|string
|
||||||
|
{
|
||||||
|
$ciphertext = \base64_decode($ciphertext);
|
||||||
|
if (strlen($ciphertext) <= AUTH_TAG_LENGTH_BYTE) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if (function_exists('\sodium\crypto_aead_aes256gcm_is_available') && crypto_aead_aes256gcm_is_available()) {
|
||||||
|
return crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, 'XGwwZbmMXy6sD5w0IrxfaBHLl7b7jCaR');
|
||||||
|
}
|
||||||
|
if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && crypto_aead_aes256gcm_is_available()) {
|
||||||
|
return crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, 'XGwwZbmMXy6sD5w0IrxfaBHLl7b7jCaR');
|
||||||
|
}
|
||||||
|
if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
|
||||||
|
$ctext = substr($ciphertext, 0, -AUTH_TAG_LENGTH_BYTE);
|
||||||
|
$authTag = substr($ciphertext, -AUTH_TAG_LENGTH_BYTE);
|
||||||
|
return \openssl_decrypt($ctext, 'aes-256-gcm', 'XGwwZbmMXy6sD5w0IrxfaBHLl7b7jCaR', \OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData);
|
||||||
|
}
|
||||||
|
throw new \RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx\V3;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
use Kiri\Client;
|
||||||
|
use Kiri\Message\Stream;
|
||||||
|
use wchat\wx\SmallProgram;
|
||||||
|
|
||||||
|
class WxV3Withdrawal extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
use WxV3PaymentTait;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $orderNo
|
||||||
|
* @param string $batch_name
|
||||||
|
* @param string $batch_remark
|
||||||
|
* @param TransferDetail[] $details
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function payment($orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||||
|
{
|
||||||
|
$body = $this->create($orderNo, $batch_name, $batch_remark, $details);
|
||||||
|
|
||||||
|
$sign = $this->signature('POST', '/v3/pay/transactions/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
|
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||||
|
$client->withAddedHeader('Authorization', $sign)
|
||||||
|
->withContentType('application/json')
|
||||||
|
->withAddedHeader('User-Agent', 'application/json')
|
||||||
|
->withBody(new Stream($json))
|
||||||
|
->post('/v3/pay/transactions/batches');
|
||||||
|
$client->close();
|
||||||
|
|
||||||
|
$json = json_decode($client->getBody(), TRUE);
|
||||||
|
if (!isset($json['prepay_id'])) {
|
||||||
|
throw new Exception('微信支付调用失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->createResponse($json, $body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[ArrayShape(['transfer_detail_list' => "array", 'total_amount' => "int", 'total_num' => "int", 'batch_remark' => "string", 'batch_name' => "string", 'out_batch_no' => ""])]
|
||||||
|
private function create($orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||||
|
{
|
||||||
|
$total = 0;
|
||||||
|
$body = ['transfer_detail_list' => []];
|
||||||
|
$body['out_batch_no'] = $orderNo;
|
||||||
|
$body['batch_name'] = $batch_name;
|
||||||
|
$body['batch_remark'] = $batch_remark;
|
||||||
|
$body['total_num'] = count($details);
|
||||||
|
foreach ($details as $detail) {
|
||||||
|
$total += $detail->transfer_amount;
|
||||||
|
$body['transfer_detail_list'][] = $detail->toArray();
|
||||||
|
}
|
||||||
|
$body['total_amount'] = $total;
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
use Kiri\Di\Container;
|
||||||
|
use ReflectionException;
|
||||||
|
use wchat\common\Config;
|
||||||
|
|
||||||
|
class WxFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $class
|
||||||
|
* @param Config $config
|
||||||
|
* @return object|null
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public static function get($class, Config $config): ?object
|
||||||
|
{
|
||||||
|
$container = Container::instance();
|
||||||
|
$object = $container->get($class);
|
||||||
|
$object->setConfig($config);
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user