Compare commits
496 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b87232bbc0 | |||
| d9528679ff | |||
| 09544ee4a2 | |||
| a7c6939a9f | |||
| 0578ba384b | |||
| 7abbaaca46 | |||
| d574ce2c42 | |||
| 2275ed92d1 | |||
| 48467b88ce | |||
| e87728266b | |||
| ae67dce77b | |||
| 2ff7ceb66b | |||
| c74c3b004b | |||
| 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 | |||
| 6539c988e8 | |||
| 74d55943cf | |||
| 954cf6999c | |||
| 1b9b6c4af6 | |||
| 6f2852ecbf | |||
| e586437334 | |||
| ba578732b9 | |||
| 4cd04c4f42 | |||
| 455e0f9bcb | |||
| 44627516e8 | |||
| b896ca4143 | |||
| 1ab20dbae4 | |||
| 01f8429110 | |||
| 35696bb8fc | |||
| 6c1c1a379b | |||
| 9c415292a4 | |||
| 0a3c8ad5c3 | |||
| 4127dc283a | |||
| caf2960d4a | |||
| ebede84969 | |||
| bed2fe1093 | |||
| 1e9555a3d6 | |||
| 5475e2ba14 | |||
| 3c5f02d52a | |||
| 8152b1b85e | |||
| 8952e72708 | |||
| 296086a7ae | |||
| 83a86a2a57 | |||
| 403373db84 | |||
| 758917bd0b | |||
| e3831ad4d8 | |||
| cf4c0259f7 | |||
| 7b7020d66f | |||
| 7e29cbb5ef | |||
| 3cda6f409e | |||
| d4a4154643 | |||
| f626a73a37 | |||
| b8e4a80c3a | |||
| 07df6767b4 | |||
| 44146ad916 | |||
| 7bda1dbcce | |||
| a7d2121bf0 | |||
| 28918928c6 | |||
| 69c27d8f2a | |||
| 5b46609863 | |||
| 15ad099c45 | |||
| 6a61697c0d | |||
| ba4ae203b4 | |||
| 1050b7f6bb | |||
| a4bab25b3b | |||
| d983f7785b | |||
| fa4aea6b2c | |||
| 3f98508539 | |||
| e3d53af185 | |||
| 5a42d843b9 | |||
| ac4db69cb3 | |||
| 980edd9d8a | |||
| 7ed51596da | |||
| 06c93400b4 | |||
| 10a29dafee | |||
| 1d81d3e7f7 | |||
| 4a1b152daa | |||
| 4bbdf5622b | |||
| dbb41245ee | |||
| b2e46c64c6 | |||
| a67ecd7493 | |||
| f8103f93b9 | |||
| db6d6a0611 | |||
| 2d08ed945a | |||
| 04a86e3e17 | |||
| 9d82b5399c | |||
| 67acf5e750 | |||
| 1f492a1521 | |||
| 78348ae2f7 | |||
| 5ee4e994c7 | |||
| a4f51ef586 | |||
| 608a3b8639 | |||
| cb25fce004 | |||
| 07a00bd4b0 | |||
| 7bdbf14baf | |||
| cda357bc68 | |||
| dd32f8003a | |||
| b27f50d1e2 | |||
| e9b465fd20 | |||
| d8cae08cb0 | |||
| 2d37e4e6d7 | |||
| 92453ca02c | |||
| 2a1bf128b1 | |||
| a826961afa | |||
| 73c340844b | |||
| dc6eb2bae2 | |||
| 94b8a217d5 | |||
| df41369095 | |||
| c7484da782 | |||
| 6fc876f59d | |||
| 339532536f | |||
| 08878a7090 | |||
| 2c2fe5d5a1 | |||
| 92d7ba8ff5 | |||
| fa1c7b8363 | |||
| b460fd2518 | |||
| f022210d81 | |||
| 9dd8ea2d0b | |||
| 7eeaef4c3b | |||
| 796ed52624 | |||
| 7ee32b10b6 | |||
| cdc74e59a9 | |||
| 424634ee2b | |||
| 7f04bc8af7 | |||
| 30bb2cf1c5 | |||
| b4a6ed706f | |||
| 156ef4db41 | |||
| e9c21d404a | |||
| 5d510cadb6 | |||
| 2ea3bd28c8 | |||
| 809246f50e | |||
| a7bce22f0c | |||
| f7128047f4 | |||
| 9d5b7b5101 | |||
| aa8b93799b | |||
| f6daf53a58 | |||
| f08bf962b1 | |||
| 29758ebf52 | |||
| cdf806a390 | |||
| def9dbb297 | |||
| 350016e83a | |||
| 4b87f485b6 | |||
| ba796bc373 | |||
| d32b6e85ec | |||
| 937a5ae22c | |||
| e3a1b3e777 | |||
| 331dcaa325 | |||
| e572352208 | |||
| d7189c3db8 | |||
| 760ccec8a0 | |||
| 50d2cbcd24 | |||
| 5a4968d3b1 | |||
| 74df55e7aa | |||
| 73e08b1c0d | |||
| 572e1b592a | |||
| e0feb1c891 | |||
| 5087d6d306 | |||
| a88b42a26d | |||
| 535d3b3e82 | |||
| ea822241a1 | |||
| c8ead691e6 | |||
| ac3d99b4d9 | |||
| 3f2a69a05b | |||
| ef89db4c65 | |||
| 347a67ffc5 | |||
| 5217b1e535 | |||
| 4718d4d4a2 | |||
| 1d3abdf9f0 | |||
| 12d55949c7 | |||
| ea2f69e224 | |||
| 294303298e | |||
| 03b08f79fe | |||
| 81e9fe4b54 | |||
| 0f954c111f | |||
| 291e882069 | |||
| 0a16cc14e4 | |||
| 56cc706c67 | |||
| c645d63f97 | |||
| 53030480ea | |||
| 6e270b760c | |||
| 6318a796eb | |||
| 93b178e855 | |||
| fe99e03799 | |||
| 82d7e48604 | |||
| e8175a650a | |||
| 79d9295f6b | |||
| b290c3dc77 | |||
| dfec642731 | |||
| eaca17753a | |||
| cf9a45b410 | |||
| 85069f1f70 | |||
| f746766fc6 | |||
| cdd881d694 | |||
| 7c9981c63d | |||
| adb2c7d3e1 | |||
| ea1f27bd21 | |||
| d17392ffb8 | |||
| 2682cdacf8 | |||
| 889d8a438c | |||
| ace3d67552 | |||
| deb06e20a6 | |||
| 9780ee345b | |||
| 744f464db4 | |||
| baf07b7ab2 | |||
| ab2e7e3900 | |||
| 583630d0ca | |||
| ea6c53796a | |||
| 8a3f8ac479 | |||
| 67d27159f3 | |||
| c34c78320b | |||
| fbe3aa4ff7 | |||
| e07f50ac98 | |||
| 049462cfb4 | |||
| d4826968eb | |||
| 7885d0f572 | |||
| 114d783c63 | |||
| 4c08a2dcce | |||
| 0f3f86dfa2 | |||
| 28557514b0 | |||
| ccb6ed6c08 | |||
| 22196b5c62 | |||
| 0b10b3568a | |||
| 4be259587c | |||
| 2a2b163188 | |||
| 0eb014d511 | |||
| 48adcda749 | |||
| d586e2aee2 | |||
| 40ac73f40f | |||
| fec5a10e32 | |||
| ee3f729fc4 | |||
| 4764db18c5 | |||
| 2753f555b3 | |||
| 155b1374b5 | |||
| 07b7886bd7 | |||
| 8d4f278c6c | |||
| 7b13db2330 | |||
| e13a898a54 | |||
| 40f73f7df8 | |||
| 8a40496206 | |||
| 3bca09cfe2 | |||
| 4cbb26c8aa | |||
| 048b864f9e | |||
| c8130cb02c | |||
| 93be947fc8 | |||
| 85728ef47a | |||
| 6c74fd5fae | |||
| 483b1f6da2 | |||
| 564cd968e5 | |||
| df89d8e544 | |||
| 91878579b6 | |||
| 18d58c7182 | |||
| 540b15ceba | |||
| ff93c07540 | |||
| 213451e345 | |||
| 2ab9714f96 | |||
| 0417e76c26 | |||
| 9c7a23d802 | |||
| e435a3888b | |||
| f7863891c2 | |||
| 42365a696d | |||
| f35f42944f | |||
| f0a0cd586a | |||
| 2cc181bf7b | |||
| d89a05bc07 | |||
| f52d8ff4a8 | |||
| 089f52c585 | |||
| c9d7f52845 | |||
| 5ef309de4e | |||
| 36875f19e0 | |||
| f01c5e35d8 | |||
| da21f45554 | |||
| 2ca6eb7b41 | |||
| 0220bb69b0 | |||
| 44dd66dc6b | |||
| 72055c6591 | |||
| 145e680740 | |||
| 9934adad01 | |||
| f7fe347ec3 | |||
| 70d0de22ef | |||
| 77a925ccc7 | |||
| 224bd81184 | |||
| 9f87d4f8f9 | |||
| 5fcbf18271 | |||
| 3d16592366 | |||
| b140617f23 | |||
| f453f98b38 | |||
| 6156ef2cb4 | |||
| dea36a9be5 | |||
| 2745650d39 | |||
| 96bc5a4d8b | |||
| 77417cdf5a | |||
| 824969968c | |||
| 11745b8b66 | |||
| 7db7ad1ea0 | |||
| 976b936396 | |||
| 7c849555f4 | |||
| b6690c18d5 | |||
| 3284a0922e | |||
| 97df0910ec | |||
| daba7fe499 | |||
| 6148a56126 | |||
| f68080387a | |||
| 496d2a95aa | |||
| 6ae53dc7d4 | |||
| 775cdeab7c | |||
| 6df3d38880 | |||
| 6677193b35 | |||
| 4ed83cb6d8 | |||
| f7893e08ae | |||
| fa8008d6d8 | |||
| 7f138ef255 | |||
| 62a2326e23 | |||
| 8eb5922d10 | |||
| dbfbbf75b0 | |||
| ddaf80a14d | |||
| af316b799b | |||
| 4784354c22 | |||
| 4a3a3a5091 | |||
| 7011a4569d | |||
| 27d8b60845 | |||
| b2bf378b7f | |||
| b2d477f0cf | |||
| f2dc33fca4 | |||
| d605600657 | |||
| 6ff5ecc853 | |||
| 09776fc906 | |||
| c9d0f86620 | |||
| 33de57b515 | |||
| 678543151f | |||
| e0925d86c5 | |||
| 658b951fe9 | |||
| 7aa22e2e62 | |||
| 31d75529bc | |||
| f13a0e80ff | |||
| df23a0ecf4 | |||
| ea3790579c | |||
| aa513a397f | |||
| 447390a87b | |||
| b1524a7eaf | |||
| a4071f1ad8 | |||
| a376fe8f4f | |||
| aaae917b72 | |||
| d7953bc801 |
@@ -1,3 +1,4 @@
|
||||
/.idea/php.xml
|
||||
vendor/
|
||||
composer.lock
|
||||
.idea/*
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
use wchat\common\Config;
|
||||
use wchat\common\Progaram;
|
||||
|
||||
/**
|
||||
* Class Container
|
||||
*/
|
||||
class Container
|
||||
{
|
||||
|
||||
/** @var Container */
|
||||
private static $_instance;
|
||||
|
||||
private $config;
|
||||
|
||||
private $container = [];
|
||||
|
||||
const QQ_SMALL_PROGRAM = 'QQ_SMALL_PROGRAM';
|
||||
const QQ_LITTLE_GAME = 'QQ_LITTLE_GAME';
|
||||
const WX_LITTLE_GAME = 'WX_LITTLE_GAME';
|
||||
const WX_SMALL_PROGRAM = 'WX_SMALL_PROGRAM';
|
||||
const WX_OFFICIAL_ACCOUNT = 'WX_OFFICIAL_ACCOUNT';
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $config
|
||||
* @return mixed|object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public static function newInstance($class, $config)
|
||||
{
|
||||
if (!(static::$_instance instanceof Container)) {
|
||||
static::$_instance = new Container();
|
||||
}
|
||||
static::$_instance->generate($config);
|
||||
|
||||
if (static::$_instance->exists($class)) {
|
||||
$newInstance = static::$_instance->get($class);
|
||||
if (method_exists($newInstance, 'setConfig')) {
|
||||
$newInstance->setConfig($config);
|
||||
}
|
||||
return $newInstance;
|
||||
} else {
|
||||
return static::$_instance->createObject($class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param wchat\common\Config $config
|
||||
* @return $this
|
||||
*/
|
||||
public function generate(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function createObject($class)
|
||||
{
|
||||
$newInstance = new \ReflectionClass($class);
|
||||
if (!$newInstance->isInstantiable()) {
|
||||
throw new Exception('Class Con\'t instance.');
|
||||
}
|
||||
$newInstance = $newInstance->newInstance();
|
||||
if (method_exists($newInstance, 'setConfig')) {
|
||||
$newInstance->setConfig($this->config);
|
||||
}
|
||||
|
||||
$this->container[$class] = $newInstance;
|
||||
return $newInstance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|object|ReflectionClass
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return $this->container[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($name)
|
||||
{
|
||||
return array_key_exists($name, $this->container) && $this->container[$name] instanceof Progaram;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
```php
|
||||
|
||||
$config = new \wchat\Config();
|
||||
$config = new \common\Config();
|
||||
$config->setAppid('');
|
||||
$config->setAppsecret('');
|
||||
$config->setMchId('');
|
||||
|
||||
+29
-23
@@ -1,25 +1,31 @@
|
||||
{
|
||||
"name": "wchat/wchat",
|
||||
"description": "一个参数效验扩展",
|
||||
"keywords": [
|
||||
"validate"
|
||||
],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "xianglin",
|
||||
"email": "xianglin@163.com"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"wchat\\": "wx"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">= 7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"swoole/ide-helper": "@dev"
|
||||
}
|
||||
"name": "wchat/wchat",
|
||||
"description": "一个参数效验扩展",
|
||||
"keywords": [
|
||||
"validate"
|
||||
],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "xianglin",
|
||||
"email": "xianglin@163.com"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"wchat\\": "./wchat/"
|
||||
},
|
||||
"files": [
|
||||
"Container.php"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">= 8.0",
|
||||
"game-worker/kiri-container": "~v1.9",
|
||||
"game-worker/kiri-client": "~v2.8",
|
||||
"psr/container": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"swoole/ide-helper": "@dev"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,39 +5,37 @@
|
||||
* Date: 2018/7/19 0019
|
||||
* Time: 18:38
|
||||
*/
|
||||
//include_once __DIR__ . '/wchat/common/Progaram.php';
|
||||
//include_once __DIR__ . '/wchat/common/Miniprogarampage.php';
|
||||
//include_once __DIR__ . '/wchat/common/Help.php';
|
||||
//include_once __DIR__ . '/wchat/common/Config.php';
|
||||
//include_once __DIR__ . '/wchat/common/Result.php';
|
||||
//include_once __DIR__ . '/wchat/common/HttpClient.php';
|
||||
//include_once __DIR__ . '/wchat/qq/SmallProgram.php';
|
||||
//include_once __DIR__ . '/wchat/base/Template.php';
|
||||
//include_once __DIR__ . '/wchat/qq/Template.php';
|
||||
|
||||
$config = new \wchat\Config();
|
||||
$config->setAppid('');
|
||||
$config->setAppsecret('');
|
||||
$config->setMchId('');
|
||||
$config->setKey('');
|
||||
$config->setRemoteAddr('');
|
||||
$config->setDeviceInfo('');
|
||||
$config->setAccessToken('');
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use wchat\common\HttpClient;
|
||||
|
||||
$instance = \wchat\Wx::getMiniProGaRamPage();
|
||||
$instance->setConfig($config);
|
||||
\Swoole\Coroutine::create(function () {
|
||||
$data = '{"id":"681133","username":"","nickname":"???? 闫 ????","openId":"ovnGZ5cj4GtvTeaRZqxYtTwp4lCc","gzhOpenId":"null","phone":"","email":"","password":"","server":0,"xcx_server":0,"unionID":"","avatar":"https:\/\/wx.qlogo.cn\/mmopen\/vi_32\/mGkblm7Ffz4o0prJKnqysoicFWYmwk2YzDqGecsAA10a1QSsRDEH70IserwXKBVMSjBHCaq7Z8YIeccXEHJt9lw\/132","sex":2,"city":"Shijiazhuang","province":"Hebei","country":"China","createTime":1530404031,"modifyTime":1530404048,"lastSeetime":0,"status":1,"groupId":0}';
|
||||
|
||||
$recharge = $instance->getRecharge();
|
||||
$recharge->cashWithdrawal(1, 'xxx', 'ooo');
|
||||
$recharge->recharge(1, '', '');
|
||||
var_dump(PHP_LIBDIR, PHP_CONFIG_FILE_SCAN_DIR);
|
||||
|
||||
$account = $instance->getAccount();
|
||||
$account->setSavePath('');
|
||||
$account->login('');
|
||||
$account->createwxaqrcode('pages/index/index',200);
|
||||
$account->getwxacode('pages/index/index',150,true);
|
||||
$account->getwxacodeunlimit('pages/index/index',150,true);
|
||||
$config = new \wchat\common\Config();
|
||||
$config->setAppid('1110444685');
|
||||
$config->setAppsecret('XTK81p7WS0PqWjiF');
|
||||
$config->setUsrSwoole(true);
|
||||
$config->setAccessToken('g3JqSiACJQuYuE7Hz683mz5oAi8aYuUktCc2C3a92IeB0_Kx9E92tJTtRQVUBAbucCSt');
|
||||
|
||||
/** @var \wchat\qq\SecCheck $model */
|
||||
$model = \wchat\qq\SecCheck::getInstance($config);
|
||||
var_dump($model->text('德生科技还是看得见粉红色的空间'));
|
||||
|
||||
$message = $instance->getMessage();
|
||||
$message->setOpenid('');
|
||||
$message->sendCardNews('');
|
||||
// $json = json_decode($data, true);
|
||||
//
|
||||
// var_dump($json);
|
||||
|
||||
$template = $instance->getTemplate();
|
||||
$template->setOpenId('');
|
||||
$template->setFormId('');
|
||||
$template->setTemplateId('');
|
||||
$template->setPage('');
|
||||
$template->sendTemplate();
|
||||
});
|
||||
|
||||
@@ -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('@'));
|
||||
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
* @package wchat\base
|
||||
*/
|
||||
abstract class Subject extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
private string $oac_appid = '';
|
||||
private string $use_robot = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
*/
|
||||
public function setPage($page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword($emphasis_keyword)
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, string $color = '')
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
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
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$access_token = $this->config->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
if (!empty($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);
|
||||
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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 $result
|
||||
* @return mixed
|
||||
*/
|
||||
public function reset($result)
|
||||
{
|
||||
$this->openId = '';
|
||||
$this->keywords = [];
|
||||
$this->templateId = '';
|
||||
$this->page = '';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\base;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Multiprogramming;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Template
|
||||
*/
|
||||
abstract class Template extends Multiprogramming
|
||||
{
|
||||
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private string $formId = '';
|
||||
private string $openId = '';
|
||||
private string $page = 'pages/index/index';
|
||||
private string $emphasis_keyword = '';
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $formId
|
||||
*/
|
||||
public function setFormId($formId)
|
||||
{
|
||||
$this->formId = $formId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
*/
|
||||
public function setPage($page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword($emphasis_keyword)
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, string $color = '')
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
abstract public function getUrl();
|
||||
|
||||
abstract public function getHost();
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$access_token = $this->config->getAccessToken();
|
||||
if (empty($access_token)) {
|
||||
throw new \Exception('request access_token con\'t null.');
|
||||
}
|
||||
|
||||
$params = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"form_id" => $this->formId,
|
||||
"data" => $this->keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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
|
||||
* @return void
|
||||
*/
|
||||
private function reset($result): void
|
||||
{
|
||||
$this->openId = '';
|
||||
$this->keywords = [];
|
||||
$this->formId = '';
|
||||
$this->templateId = '';
|
||||
$this->page = '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat;
|
||||
namespace wchat\common;
|
||||
|
||||
|
||||
class Config
|
||||
@@ -12,7 +12,7 @@ class Config
|
||||
*
|
||||
* 小程序ID
|
||||
*/
|
||||
private $appid = '';
|
||||
private string $appid = '';
|
||||
|
||||
|
||||
/**
|
||||
@@ -20,93 +20,473 @@ class Config
|
||||
*
|
||||
* 商户号ID
|
||||
*/
|
||||
private $mch_id = '';
|
||||
private string $mch_id = '';
|
||||
|
||||
/**
|
||||
* @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';
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 随机字符串
|
||||
*/
|
||||
private $nonce_str = '';
|
||||
private string $nonce_str = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 商品简单描述
|
||||
*/
|
||||
private $body = '好友默契Pk充值!';
|
||||
private string $body = '好友默契Pk充值!';
|
||||
|
||||
/**
|
||||
* @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
|
||||
*
|
||||
* 终端IP
|
||||
*/
|
||||
private $spbill_create_ip = "";
|
||||
private string $spbill_create_ip = "";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 异步回调地址
|
||||
*/
|
||||
private $notify_url = "";
|
||||
private string $notify_url = "";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 交易类型
|
||||
*/
|
||||
private $trade_type = 'JSAPI';
|
||||
private string $trade_type = 'JSAPI';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 签名方式
|
||||
*/
|
||||
private $sign_type = 'MD5';
|
||||
private string $sign_type = 'MD5';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* 商户接口地址
|
||||
*/
|
||||
private $mch_host = 'https://api.mch.weixin.qq.com';
|
||||
private string $mch_host = 'https://api.mch.weixin.qq.com';
|
||||
|
||||
/**
|
||||
* @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 $ssl_key = '';
|
||||
private string $ssl_cert = '';
|
||||
private string $ssl_key = '';
|
||||
private string $ssl_ca = '';
|
||||
private int $port = 443;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $key = '';
|
||||
private $access_token = '';
|
||||
private string $key = '';
|
||||
private string $access_token = '';
|
||||
private string $agent = '';
|
||||
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
|
||||
*/
|
||||
public function getSslCa(): string
|
||||
{
|
||||
return $this->ssl_ca;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ssl_ca
|
||||
*/
|
||||
public function setSslCa(string $ssl_ca): void
|
||||
{
|
||||
$this->ssl_ca = $ssl_ca;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPort(): string
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $port
|
||||
*/
|
||||
public function setPort(string $port): void
|
||||
{
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*/
|
||||
public function setKey(string $key): void
|
||||
{
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
@@ -119,289 +499,57 @@ class Config
|
||||
/**
|
||||
* @param string $access_token
|
||||
*/
|
||||
public function setAccessToken(string $access_token)
|
||||
public function setAccessToken(string $access_token): void
|
||||
{
|
||||
$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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
$this->usrSwoole = $usrSwoole;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param array $configs
|
||||
*/
|
||||
public function __construct(array $configs)
|
||||
{
|
||||
$this->setConfigs($configs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $configs
|
||||
* @return $this
|
||||
*/
|
||||
public function setConfigs(array $configs)
|
||||
protected function setConfigs(array $configs): static
|
||||
{
|
||||
if (empty($configs)) {
|
||||
return $this;
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
|
||||
class Decode
|
||||
{
|
||||
|
||||
private string $sessionKey;
|
||||
private string $iv;
|
||||
private string $encryptedData;
|
||||
private string $appId;
|
||||
|
||||
|
||||
private int $OK = 0;
|
||||
private int $IllegalAesKey = -41001;
|
||||
private int $IllegalIv = -41002;
|
||||
private int $IllegalBuffer = -41003;
|
||||
private int $DecodeBase64Error = -41004;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSessionKey(): string
|
||||
{
|
||||
return $this->sessionKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sessionKey
|
||||
*/
|
||||
public function setSessionKey(string $sessionKey): void
|
||||
{
|
||||
$this->sessionKey = $sessionKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIv(): string
|
||||
{
|
||||
return $this->iv;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iv
|
||||
*/
|
||||
public function setIv(string $iv): void
|
||||
{
|
||||
$this->iv = $iv;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncryptedData(): string
|
||||
{
|
||||
return $this->encryptedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $encryptedData
|
||||
*/
|
||||
public function setEncryptedData(string $encryptedData): void
|
||||
{
|
||||
$this->encryptedData = $encryptedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAppId(): string
|
||||
{
|
||||
return $this->appId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $appId
|
||||
*/
|
||||
public function setAppId(string $appId): void
|
||||
{
|
||||
$this->appId = $appId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $asArray
|
||||
* @return array|mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function decode($asArray): object|array
|
||||
{
|
||||
if (strlen($this->sessionKey) != 24) {
|
||||
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
||||
}
|
||||
|
||||
$aesKey = base64_decode($this->sessionKey);
|
||||
if (strlen($this->iv) != 24) {
|
||||
throw new \Exception('base64解密失败', $this->IllegalIv);
|
||||
}
|
||||
|
||||
$aesIV = base64_decode($this->iv);
|
||||
$aesCipher = base64_decode($this->encryptedData);
|
||||
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
||||
if ($result === false) {
|
||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||
}
|
||||
|
||||
$dataObj = json_decode($result);
|
||||
if ($dataObj->watermark->appid != $this->appId) {
|
||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||
}
|
||||
return $asArray ? get_object_vars($dataObj) : $dataObj;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
|
||||
class Help extends Multiprogramming
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function toXml(array $data): string
|
||||
{
|
||||
$xml = "<xml>";
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
|
||||
continue;
|
||||
}
|
||||
if (is_numeric($val)) {
|
||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||
} else {
|
||||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
$xml .= "</xml>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @return string
|
||||
*/
|
||||
private static function xmlChild(array $array): string
|
||||
{
|
||||
$string = '';
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$string .= static::xmlChild($value);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_numeric($value)) {
|
||||
$string .= "<" . $key . ">" . $value . "</" . $key . ">";
|
||||
} else {
|
||||
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @return array|null
|
||||
*/
|
||||
public static function toArray($xml): ?array
|
||||
{
|
||||
if (is_array($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) {
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $json
|
||||
* @return string
|
||||
*/
|
||||
public static function toJson(mixed $json): string
|
||||
{
|
||||
if (is_object($json)) {
|
||||
$json = get_object_vars($json);
|
||||
}
|
||||
if (is_array($json)) {
|
||||
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$matchQuote = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
|
||||
if (preg_match($matchQuote, $json)) {
|
||||
$json = self::xmlToArray($json);
|
||||
} else {
|
||||
$json = json_decode($json, true);
|
||||
}
|
||||
if (!is_array($json)) {
|
||||
$json = [];
|
||||
}
|
||||
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
* @return string
|
||||
*
|
||||
* 随机字符串
|
||||
*/
|
||||
public static function random(int $length = 20): string
|
||||
{
|
||||
$res = [];
|
||||
$str = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$str .= strtoupper($str) . '1234567890';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$rand = substr($str, rand(0, strlen($str) - 2), 1);
|
||||
if (empty($rand)) {
|
||||
$rand = substr($str, strlen($str) - 3, 1);
|
||||
}
|
||||
$res[] = $rand;
|
||||
}
|
||||
|
||||
return implode($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param $key
|
||||
* @param $type
|
||||
* @return string
|
||||
*/
|
||||
public static function sign(array $array, $key, $type): string
|
||||
{
|
||||
ksort($array);
|
||||
$buff = "";
|
||||
foreach ($array as $k => $v) {
|
||||
if ($k != "sign" && $v != "" && !is_array($v)) {
|
||||
$buff .= $k . "=" . $v . "&";
|
||||
}
|
||||
}
|
||||
$string = trim($buff, "&") . '&key=' . $key;
|
||||
if (strtoupper($type) == 'MD5') {
|
||||
return strtoupper(md5($string));
|
||||
} else {
|
||||
return hash_hmac('sha256', $string, $key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
|
||||
interface Progaram
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\common;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Client;
|
||||
|
||||
/**
|
||||
* Class Result
|
||||
*
|
||||
* @package app\components
|
||||
*
|
||||
* @property $code
|
||||
* @property $message
|
||||
* @property $count
|
||||
* @property $data
|
||||
*/
|
||||
class Result
|
||||
{
|
||||
|
||||
private ?int $startTime;
|
||||
private ?int $requestTime;
|
||||
private ?int $runTime;
|
||||
|
||||
/**
|
||||
* @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 string $data
|
||||
* @return static
|
||||
*/
|
||||
public static function wxInit(string $data): static
|
||||
{
|
||||
$json = json_decode($data, true);
|
||||
if (isset($json['errcode']) && $json['errcode'] != 0) {
|
||||
return new Result(code: $json['errcode'], message: $json['errmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Client $client
|
||||
* @return static
|
||||
*/
|
||||
public static function init(Client $client): static
|
||||
{
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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 $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->$name = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])]
|
||||
public function getTime(): array
|
||||
{
|
||||
return [
|
||||
'startTime' => $this->startTime,
|
||||
'requestTime' => $this->requestTime,
|
||||
'runTime' => $this->runTime,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $data
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setAttr($key, $data): static
|
||||
{
|
||||
if (!property_exists($this, $key)) {
|
||||
throw new Exception('未查找到相应对象属性');
|
||||
}
|
||||
$this->$key = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isResultsOK(): bool
|
||||
{
|
||||
if (!is_numeric($this->code)) {
|
||||
return false;
|
||||
}
|
||||
return $this->code == 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData(string $name = ''): mixed
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return Result
|
||||
*/
|
||||
public function login(string $code): Result
|
||||
{
|
||||
$param['appid'] = $this->config->getAppid();
|
||||
$param['secret'] = $this->config->getAppsecret();
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
$client = new Client('api.q.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('sns/jscode2session', $param);
|
||||
$client->close();
|
||||
|
||||
return Result::init($client);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
use Exception;
|
||||
use wchat\common\Help;
|
||||
|
||||
/**
|
||||
* Class Notify
|
||||
* @package wchat\qq
|
||||
*/
|
||||
class Notify extends SmallProgram
|
||||
{
|
||||
public mixed $appid = null;
|
||||
public mixed $mch_id = null;
|
||||
public mixed $nonce_str = null;
|
||||
public mixed $sign = null;
|
||||
public mixed $device_info = null;
|
||||
public mixed $trade_type = null;
|
||||
public mixed $trade_state = null;
|
||||
public mixed $bank_type = null;
|
||||
public mixed $fee_type = null;
|
||||
public mixed $total_fee = null;
|
||||
public mixed $cash_fee = null;
|
||||
public mixed $coupon_fee = null;
|
||||
public mixed $transaction_id = null;
|
||||
public mixed $out_trade_no = null;
|
||||
public mixed $attach = null;
|
||||
public mixed $time_end = null;
|
||||
public mixed $openid = null;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* 判断是否完成支付
|
||||
*/
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->trade_state === 'SUCCESS';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setPayNotifyData(array $params): static
|
||||
{
|
||||
if (!$this->validation($params)) {
|
||||
throw new Exception('签名错误!');
|
||||
}
|
||||
foreach ($params as $key => $val) {
|
||||
$this->$key = $val;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function __set(string $name, $value): void
|
||||
{
|
||||
if (property_exists($this, $name)) {
|
||||
$this->{$name} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public function validation(array $params): bool
|
||||
{
|
||||
$sign = $params['sign'];
|
||||
unset($params['sign']);
|
||||
|
||||
$signType = $this->config->getSignType();
|
||||
$privateKey = $this->config->getKey();
|
||||
$nowSign = Help::sign($params, $privateKey, $signType);
|
||||
if ($sign === $nowSign) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getAppid()
|
||||
{
|
||||
return $this->appid;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:22
|
||||
*/
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
use wchat\common\Help;
|
||||
|
||||
class Recharge extends SmallProgram
|
||||
{
|
||||
private float $money = 0;
|
||||
|
||||
private string $orderNo;
|
||||
|
||||
private array $data = [];
|
||||
|
||||
private string $spill_create_ip = '';
|
||||
|
||||
private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi';
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setSpillCreateIp($value)
|
||||
{
|
||||
$this->spill_create_ip = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $openId
|
||||
* @return Result
|
||||
* @throws
|
||||
*/
|
||||
public function recharge(int $money, string $orderNo, string $openId = ''): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 500, message: '充值金额不能小于0.');
|
||||
}
|
||||
$this->money = $money;
|
||||
$this->orderNo = $orderNo;
|
||||
$this->data['openid'] = $openId;
|
||||
|
||||
$client = new Client('qpay.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/xml']);
|
||||
$client->withBody($this->builder());
|
||||
$client->post($this->uniformer);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
|
||||
$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 Help::sign([
|
||||
'signType' => $this->config->getSignType(),
|
||||
'package' => 'prepay_id=' . $prepay_id,
|
||||
'nonceStr' => Help::random(32),
|
||||
'timestamp' => time()
|
||||
], $this->getConfig()->getKey(), $this->getConfig()->getSignType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder(): string
|
||||
{
|
||||
$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);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Redhat extends SmallProgram
|
||||
{
|
||||
|
||||
private string $charset = 'UTF8';
|
||||
private string $sign = '';
|
||||
private string $mch_billno = '';
|
||||
private string $mch_name = '';
|
||||
private string $re_openid = '';
|
||||
private string $total_amount = '';
|
||||
private string $total_num = '';
|
||||
private string $wishing = '';
|
||||
private string $act_name = '';
|
||||
private string $icon_id = '';
|
||||
private string $banner_id = '';
|
||||
private string $not_send_msg = '';
|
||||
private string $min_value = '';
|
||||
private string $max_value = '';
|
||||
|
||||
|
||||
private string $sendUrl = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||
private string $searchUrl = '/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCharset(): string
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $charset
|
||||
*/
|
||||
public function setCharset(string $charset): void
|
||||
{
|
||||
$this->charset = $charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSign(): string
|
||||
{
|
||||
return $this->sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sign
|
||||
*/
|
||||
public function setSign(string $sign): void
|
||||
{
|
||||
$this->sign = $sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMchBillno(): string
|
||||
{
|
||||
return $this->mch_billno;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_billno
|
||||
*/
|
||||
public function setMchBillno(string $mch_billno): void
|
||||
{
|
||||
$this->mch_billno = $mch_billno;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMchName(): string
|
||||
{
|
||||
return $this->mch_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mch_name
|
||||
*/
|
||||
public function setMchName(string $mch_name): void
|
||||
{
|
||||
$this->mch_name = $mch_name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getReOpenid(): string
|
||||
{
|
||||
return $this->re_openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $re_openid
|
||||
*/
|
||||
public function setReOpenid(string $re_openid): void
|
||||
{
|
||||
$this->re_openid = $re_openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTotalAmount(): string
|
||||
{
|
||||
return $this->total_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $total_amount
|
||||
*/
|
||||
public function setTotalAmount(string $total_amount): void
|
||||
{
|
||||
$this->total_amount = $total_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTotalNum(): string
|
||||
{
|
||||
return $this->total_num;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $total_num
|
||||
*/
|
||||
public function setTotalNum(string $total_num): void
|
||||
{
|
||||
$this->total_num = $total_num;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getWishing(): string
|
||||
{
|
||||
return $this->wishing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $wishing
|
||||
*/
|
||||
public function setWishing(string $wishing): void
|
||||
{
|
||||
$this->wishing = $wishing;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getActName(): string
|
||||
{
|
||||
return $this->act_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $act_name
|
||||
*/
|
||||
public function setActName(string $act_name): void
|
||||
{
|
||||
$this->act_name = $act_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIconId(): string
|
||||
{
|
||||
return $this->icon_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $icon_id
|
||||
*/
|
||||
public function setIconId(string $icon_id): void
|
||||
{
|
||||
$this->icon_id = $icon_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBannerId(): string
|
||||
{
|
||||
return $this->banner_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $banner_id
|
||||
*/
|
||||
public function setBannerId(string $banner_id): void
|
||||
{
|
||||
$this->banner_id = $banner_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNotSendMsg(): string
|
||||
{
|
||||
return $this->not_send_msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $not_send_msg
|
||||
*/
|
||||
public function setNotSendMsg(string $not_send_msg): void
|
||||
{
|
||||
$this->not_send_msg = $not_send_msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMinValue(): string
|
||||
{
|
||||
return $this->min_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $min_value
|
||||
*/
|
||||
public function setMinValue(string $min_value): void
|
||||
{
|
||||
$this->min_value = $min_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMaxValue(): string
|
||||
{
|
||||
return $this->max_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $max_value
|
||||
*/
|
||||
public function setMaxValue(string $max_value): void
|
||||
{
|
||||
$this->max_value = $max_value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* 构建请求参数
|
||||
*/
|
||||
public function generate(): array
|
||||
{
|
||||
$requestParam = [];
|
||||
$requestParam['charset'] = $this->getCharset();
|
||||
$requestParam['nonce_str'] = Help::random(30);
|
||||
$requestParam['mch_billno'] = $this->getMchBillno();
|
||||
$requestParam['mch_id'] = $this->config->getMchId();
|
||||
$requestParam['mch_name'] = $this->getMchName();
|
||||
$requestParam['qqappid'] = $this->config->getAppid();
|
||||
$requestParam['re_openid'] = $this->getReOpenid();
|
||||
$requestParam['total_amount'] = $this->getTotalAmount() * 100;
|
||||
$requestParam['min_value'] = $this->getMinValue() * 100;
|
||||
$requestParam['max_value'] = $this->getMaxValue() * 100;
|
||||
$requestParam['total_num'] = $this->getTotalNum();
|
||||
$requestParam['wishing'] = $this->getWishing();
|
||||
$requestParam['act_name'] = $this->getActName();
|
||||
$requestParam['icon_id'] = $this->getIconId();
|
||||
$requestParam['notify_url'] = $this->config->getNotifyUrl();
|
||||
|
||||
$requestParam['sign'] = $this->builderSign($requestParam);
|
||||
|
||||
return $requestParam;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $requestParam
|
||||
* @return string
|
||||
*/
|
||||
private function builderSign($requestParam): string
|
||||
{
|
||||
return Help::sign($requestParam, $this->config->getKey(), $this->config->getSignType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
*/
|
||||
public function redEnvelopes(): Result
|
||||
{
|
||||
$client = new Client('api.qpay.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/x-www-form-urlencoded']);
|
||||
$client->withSslKeyFile($this->config->getSslKey());
|
||||
$client->withSslCertFile($this->config->getSslCert());
|
||||
$client->withCa($this->config->getSslCa());
|
||||
$client->post($this->sendUrl, http_build_query($this->generate()));
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
||||
}
|
||||
if ($json['retcode'] != 0) {
|
||||
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $listid
|
||||
* @param $orderNo
|
||||
* @return Result
|
||||
*/
|
||||
public function searchByOrderNo($listid, $orderNo = null): Result
|
||||
{
|
||||
$requestParam['nonce_str'] = Help::random(31);
|
||||
$requestParam['mch_id'] = $this->config->getMchId();
|
||||
$requestParam['listid'] = $listid;
|
||||
if (!empty($orderNo)) {
|
||||
$requestParam['mch_billno'] = $orderNo;
|
||||
}
|
||||
$requestParam['sign'] = $this->builderSign($requestParam);
|
||||
$client = new Client('qpay.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($this->searchUrl, $requestParam);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
if (isset($json['result']) && $json['result'] != 'SUCCESS') {
|
||||
return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!');
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class SecCheck
|
||||
* @package qq
|
||||
*/
|
||||
class SecCheck extends SmallProgram
|
||||
{
|
||||
|
||||
private string $_url = '/api/json/security/ImgSecCheck?access_token=';
|
||||
|
||||
private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return Result
|
||||
*/
|
||||
public function image(string $path = ''): Result
|
||||
{
|
||||
if (!file_exists($path)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
|
||||
$real_path = new \CURLFile($path);
|
||||
$data = [
|
||||
'appid' => $this->config->getAppid(),
|
||||
'media' => $real_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: $client->getBody());
|
||||
}
|
||||
$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 string $content
|
||||
* @return Result
|
||||
*/
|
||||
public function text(string $content): Result
|
||||
{
|
||||
if (empty($content)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$url = '/' . ltrim($this->_url, '/') . $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.q.qq.com', 443, true);
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use wchat\common\Multiprogramming;
|
||||
|
||||
class SmallProgram extends Multiprogramming
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
* @package wchat\qq
|
||||
*/
|
||||
class Subject extends \wchat\base\Subject
|
||||
{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return '/api/json/subscribe/SendSubscriptionMessage';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return 'api.q.qq.com';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
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: $client->getBody());
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq\pay;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
/**
|
||||
* Class Cash_Bonus
|
||||
* @package wchat\qq
|
||||
*/
|
||||
class Cash_Bonus extends SmallProgram
|
||||
{
|
||||
|
||||
public string $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||
|
||||
private array $_errors = [
|
||||
'66228701' => '红包个数超出限制',
|
||||
'66228705' => '总金额超出限制',
|
||||
'66228706' => '总金额不足以按最小金额领取每个红包',
|
||||
'66228707' => '商户签名校验失败',
|
||||
'66228708' => '重入??',
|
||||
'66228709' => 'openid转换uin失败',
|
||||
'66228711' => '商户订单中的商户号有误',
|
||||
'66228712' => '商户订单中的日期超过范围',
|
||||
'66228713' => '余额不足',
|
||||
'66228715' => '用户未关注公众号,发送AIO消息失败',
|
||||
'66229716' => '用户禁用公众号,发AIO消息失败'
|
||||
];
|
||||
|
||||
private array $_requestParams = [];
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setMchName($value)
|
||||
{
|
||||
$this->_requestParams['mch_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setActName($value)
|
||||
{
|
||||
$this->_requestParams['act_name'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setWishing($value)
|
||||
{
|
||||
$this->_requestParams['wishing'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setIconId($value)
|
||||
{
|
||||
$this->_requestParams['icon_id'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setBannerId($value)
|
||||
{
|
||||
$this->_requestParams['banner_id'] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $mch_billno
|
||||
* @param $openId
|
||||
* @param $price
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mch_send($mch_billno, $openId, $price): Result
|
||||
{
|
||||
$client = new Client('api.qpay.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withSslCertFile($this->getConfig()->getSslCert());
|
||||
$client->withSslKeyFile($this->getConfig()->getSslKey());
|
||||
$client->withCa($this->getConfig()->getSslCa());
|
||||
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
|
||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
||||
} else if ($json['retcode'] != 0) {
|
||||
if (empty($json['retmsg']) && isset($this->_errors[$json['retcode']])) {
|
||||
$json['retmsg'] = $this->_errors[$json['retcode']];
|
||||
}
|
||||
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mch_billno
|
||||
* @param $openId
|
||||
* @param $price
|
||||
* @return array
|
||||
*/
|
||||
private function orderConfig($mch_billno, $openId, $price): array
|
||||
{
|
||||
$requestParam['charset'] = 1;
|
||||
$requestParam['nonce_str'] = Help::random(32);
|
||||
$requestParam['mch_billno'] = $mch_billno;
|
||||
$requestParam['mch_id'] = $this->getConfig()->getMchId();
|
||||
$requestParam['qqappid'] = $this->getConfig()->getAppid();
|
||||
$requestParam['re_openid'] = $openId;
|
||||
$requestParam['total_amount'] = $price * 100;
|
||||
$requestParam['min_value'] = $price * 100;
|
||||
$requestParam['max_value'] = $price * 100;
|
||||
$requestParam['total_num'] = 1;
|
||||
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
|
||||
|
||||
if (!empty($this->_requestParams)) {
|
||||
$requestParam = array_merge($requestParam, $this->_requestParams);
|
||||
}
|
||||
|
||||
$requestParam['sign'] = Help::sign($requestParam, $this->getConfig()->getKey(), 'MD5');
|
||||
|
||||
return $requestParam;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mchOrderNo(): string
|
||||
{
|
||||
return implode([
|
||||
$this->getConfig()->getMchId(),
|
||||
date('Ymd'),
|
||||
random_int(11, 99),
|
||||
date('His'),
|
||||
random_int(11, 99)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $requestParams
|
||||
* @return bool
|
||||
*/
|
||||
public function validator(array $requestParams): bool
|
||||
{
|
||||
$notifySign = $requestParams['sign'];
|
||||
unset($requestParams['sign']);
|
||||
$sign = Help::sign($requestParams, $this->getConfig()->getKey(), 'MD5');
|
||||
|
||||
if ($sign !== $notifySign) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq\pay;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
use wchat\qq\SmallProgram;
|
||||
|
||||
/**
|
||||
* Class Enterprise_payment
|
||||
* @package wchat\qq\pay
|
||||
*/
|
||||
class Enterprise_payment extends SmallProgram
|
||||
{
|
||||
public string $_cash = '/cgi-bin/epay/qpay_epay_b2c.cgi';
|
||||
|
||||
private $_errors = [
|
||||
'SYSTEMERROR' => '系统错误',
|
||||
'PARAM_ERROR' => '请求参数未按指引进行填写',
|
||||
'SIGNERROR' => '参数签名结果不正确',
|
||||
'OP_USER_PASSWD_ERROR' => '操作员密码校验失败',
|
||||
'OP_USER_AUTH_ERROR' => '操作员权限错误',
|
||||
'TRANSFER_FEE_LIMIT_ERROR' => '转账限额错误',
|
||||
'TRANSFER_FAIL' => '收款用户的账户不支持收款,收款失败',
|
||||
'NOTENOUGH' => '商户营销账户的余额不足',
|
||||
'ORDERNOTEXIST' => '转账订单不存在',
|
||||
'APPID_OR_OPENID_ERR' => 'appid 或 openid 非法',
|
||||
'TOTAL_FEE_OUT_OF_LIMIT' => '单笔限额检查失败',
|
||||
'SPID_NOT_ALLOW' => '当前商户不支持企业付款',
|
||||
'REALNAME_CHECK_ERROR' => '实名检查失败',
|
||||
'RE_USER_NAME_CHECK_ERROR' => '用户真实姓名校验失败',
|
||||
'INVALID_CERTIFICATE' => '证书非法',
|
||||
];
|
||||
|
||||
private $_requestParams = [];
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setOpUserId($value)
|
||||
{
|
||||
$this->_requestParams['op_user_id'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setOpUserPassword($value)
|
||||
{
|
||||
$this->_requestParams['op_user_passwd'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setSpbillCreateIp($value)
|
||||
{
|
||||
$this->_requestParams['spbill_create_ip'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mch_billno
|
||||
* @param $openId
|
||||
* @param $price
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mch_send($mch_billno, $openId, $price)
|
||||
{
|
||||
$client = new Client('api.qpay.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withSslCertFile($this->getConfig()->getSslCert());
|
||||
$client->withSslKeyFile($this->getConfig()->getSslKey());
|
||||
$client->withCa($this->getConfig()->getSslCa());
|
||||
$client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$json = json_decode($client->getBody(), true);
|
||||
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, data: $json['return_msg'] ?? $json['retmsg']);
|
||||
} else if ($json['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 500, data: $this->_errors[$json['err_code']] ?? $json['err_code_desc']);
|
||||
} else {
|
||||
return new Result(code: 0, data: $json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mch_billno
|
||||
* @param $openId
|
||||
* @param $price
|
||||
* @return array
|
||||
*/
|
||||
private function orderConfig($mch_billno, $openId, $price): array
|
||||
{
|
||||
$requestParam['input_charset'] = 'UTF-8';
|
||||
$requestParam['nonce_str'] = Help::random(32);
|
||||
$requestParam['out_trade_no'] = $mch_billno;
|
||||
$requestParam['mch_id'] = $this->getConfig()->getMchId();
|
||||
$requestParam['appid'] = $this->getConfig()->getAppid();
|
||||
$requestParam['openid'] = $openId;
|
||||
$requestParam['fee_type'] = 'CNY';
|
||||
$requestParam['total_fee'] = $price * 100;
|
||||
$requestParam['memo'] = $this->getConfig()->getBody();
|
||||
$requestParam['notify_url'] = $this->getConfig()->getNotifyUrl();
|
||||
$requestParam['spbill_create_ip'] = $this->getConfig()->getNotifyUrl();
|
||||
|
||||
if (!empty($this->_requestParams) && is_array($this->_requestParams)) {
|
||||
$requestParam = array_merge($requestParam, $this->_requestParams);
|
||||
}
|
||||
|
||||
$requestParam['sign'] = Help::sign($requestParam, $this->getConfig()->getKey(), 'MD5');
|
||||
|
||||
return $requestParam;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mchOrderNo(): string
|
||||
{
|
||||
return implode([
|
||||
$this->getConfig()->getMchId(),
|
||||
date('Ymd'),
|
||||
random_int(11, 99),
|
||||
date('His'),
|
||||
random_int(11, 99)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $requestParams
|
||||
* @return bool
|
||||
*/
|
||||
public function validator(array $requestParams): bool
|
||||
{
|
||||
$notifySign = $requestParams['sign'];
|
||||
unset($requestParams['sign']);
|
||||
$sign = Help::sign($requestParams, $this->getConfig()->getKey(), 'MD5');
|
||||
|
||||
if ($sign !== $notifySign) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\qq\result;
|
||||
|
||||
/**
|
||||
* Class RedHatResult
|
||||
* @package qq\result
|
||||
*/
|
||||
class RedHatResult
|
||||
{
|
||||
|
||||
public mixed $result;
|
||||
public mixed $res_info;
|
||||
|
||||
public mixed $listid;
|
||||
public mixed $state;
|
||||
public mixed $total_num;
|
||||
public mixed $recv_num;
|
||||
public mixed $total_amount;
|
||||
public mixed $recv_amount;
|
||||
public mixed $recv_details;
|
||||
public mixed $uin;
|
||||
|
||||
|
||||
const RED_HAT_STATUS_PAID = 1;
|
||||
const RED_HAT_STATUS_SNATCHED = 2;
|
||||
const RED_HAT_STATUS_EXPIRED = 3;
|
||||
const RED_HAT_STATUS_REFUNDED = 4;
|
||||
|
||||
|
||||
/**
|
||||
* RedHatResult constructor.
|
||||
* @param array $array
|
||||
*/
|
||||
public function __construct(array $array)
|
||||
{
|
||||
$this->init($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
* @return void
|
||||
*/
|
||||
private function init($array): void
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if (!property_exists($this, $key)) {
|
||||
continue;
|
||||
}
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($field): mixed
|
||||
{
|
||||
return $this->$field;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* 是否已完成支付
|
||||
*/
|
||||
public function isPaid(): bool
|
||||
{
|
||||
return $this->state == self::RED_HAT_STATUS_PAID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* 是否已抢完
|
||||
*/
|
||||
public function isSnatched(): bool
|
||||
{
|
||||
return $this->state == self::RED_HAT_STATUS_SNATCHED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired(): bool
|
||||
{
|
||||
return $this->state == self::RED_HAT_STATUS_EXPIRED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRefunded(): bool
|
||||
{
|
||||
return $this->state == self::RED_HAT_STATUS_REFUNDED;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Decode;
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return Result
|
||||
*/
|
||||
public function login($code): Result
|
||||
{
|
||||
$param['appid'] = $this->config->getAppid();
|
||||
$param['secret'] = $this->config->getAppsecret();
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('sns/jscode2session', $param);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPublicUserInfo($openid): Result
|
||||
{
|
||||
$query = [
|
||||
'access_token' => $this->config->getAccessToken(),
|
||||
'openid' => $openid,
|
||||
'lang' => 'zh_CN'
|
||||
];
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('cgi-bin/user/info', $query);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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 $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param bool $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, bool $asArray = false): object|array
|
||||
{
|
||||
$decode = new Decode();
|
||||
$decode->setSessionKey($sessionKey);
|
||||
$decode->setEncryptedData($encryptedData);
|
||||
$decode->setAppId($this->config->getAppid());
|
||||
$decode->setIv($iv);
|
||||
|
||||
return $decode->decode($asArray);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @return array|mixed|Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createwxaqrcode($path, $width): mixed
|
||||
{
|
||||
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||
$sendBody['path'] = $path;
|
||||
$sendBody['width'] = $width;
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacode($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$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 = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $width
|
||||
* @param bool $is_hyaline
|
||||
* @param bool $auto_color
|
||||
* @param string $line_color
|
||||
* @return Result
|
||||
*/
|
||||
public function getwxacodeunlimit($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
|
||||
{
|
||||
$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 = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
|
||||
$client->close();
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
/**
|
||||
* Class ContentAsyncCheck
|
||||
* @package wchat
|
||||
*/
|
||||
class ContentAsyncCheck
|
||||
{
|
||||
private mixed $_ToUserName = '';
|
||||
private mixed $_FromUserName = '';
|
||||
private mixed $_CreateTime = '';
|
||||
private mixed $_MsgType = '';
|
||||
private mixed $_Event = '';
|
||||
private mixed $_isrisky = '';
|
||||
private mixed $_extra_info_json = '';
|
||||
private mixed $_appid = '';
|
||||
private mixed $_trace_id = '';
|
||||
private mixed $_status_code = '';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getToUserName(): mixed
|
||||
{
|
||||
return $this->_ToUserName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFromUserName(): mixed
|
||||
{
|
||||
return $this->_FromUserName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCreateTime(): mixed
|
||||
{
|
||||
return $this->_CreateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMsgType(): mixed
|
||||
{
|
||||
return $this->_MsgType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEvent(): mixed
|
||||
{
|
||||
return $this->_Event;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIsrisky(): mixed
|
||||
{
|
||||
return $this->_isrisky;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getExtraInfoJson(): mixed
|
||||
{
|
||||
return $this->_extra_info_json;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAppid(): mixed
|
||||
{
|
||||
return $this->_appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTraceId(): mixed
|
||||
{
|
||||
return $this->_trace_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getStatusCode(): mixed
|
||||
{
|
||||
return $this->_status_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return ContentAsyncCheck
|
||||
*/
|
||||
public static function instance(array $params): static
|
||||
{
|
||||
static $class = null;
|
||||
if ($class === null) {
|
||||
$class = new ContentAsyncCheck();
|
||||
}
|
||||
return $class->init($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @return $this
|
||||
*/
|
||||
private function init($params): static
|
||||
{
|
||||
foreach ($params as $item => $param) {
|
||||
$this->{'_' . $item} = $param;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRisky(): bool
|
||||
{
|
||||
return intval($this->_isrisky) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->_status_code === 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
|
||||
/**
|
||||
* Class Message
|
||||
* @package wchat\wx
|
||||
*/
|
||||
class Message extends SmallProgram
|
||||
{
|
||||
|
||||
private array $msgData = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $openid
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->msgData['touser'] = $openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendTextNews($content): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'text';
|
||||
$this->msgData['text'] = ['content' => $content];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendImageNews($media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'image';
|
||||
$this->msgData['image'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendVoiceNews($media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'voice';
|
||||
$this->msgData['voice'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMpNewsNews($media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'mpnews';
|
||||
$this->msgData['mpnews'] = ['media_id' => $media_id];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @param $url
|
||||
* @param $picurl
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendNewsNews($title, $description, $url, $picurl): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'news';
|
||||
$this->msgData['news'] = [
|
||||
'articles' => [
|
||||
[
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'url' => $url,
|
||||
'picurl' => $picurl
|
||||
]
|
||||
]
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendCardNews($title): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'wxcard';
|
||||
$this->msgData['wxcard'] = ['card_id' => $title];
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @param $thumb_media_id
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendVideoNews($media_id, $thumb_media_id, $title, $description): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'video';
|
||||
$this->msgData['video'] = [
|
||||
'media_id' => [
|
||||
'media_id' => $media_id,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
'title' => $title,
|
||||
'description' => $description
|
||||
]
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $musicurl
|
||||
* @param $hqmusicurl
|
||||
* @param $thumb_media_id
|
||||
* @param $title
|
||||
* @param $description
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMusicNews($musicurl, $hqmusicurl, $thumb_media_id, $title, $description): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'music';
|
||||
$this->msgData['music'] = [
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'musicurl' => $musicurl,
|
||||
'hqmusicurl' => $hqmusicurl,
|
||||
'thumb_media_id' => $thumb_media_id
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $head_content
|
||||
* @param $tail_content
|
||||
* @param array $menus
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMenuNews($head_content, $tail_content, array $menus = []): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['msgmenu'] = [
|
||||
'head_content' => $head_content,
|
||||
'tail_content' => $tail_content,
|
||||
];
|
||||
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
||||
throw new Exception('菜单选项必须有2个');
|
||||
}
|
||||
foreach ($menus as $val) {
|
||||
$this->addNewsMenu($val['id'], $val['name']);
|
||||
}
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
private int $index = 0;
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $menuName
|
||||
* @return $this
|
||||
*/
|
||||
public function addNewsMenu($id, $menuName): static
|
||||
{
|
||||
$lists['id'] = $id;
|
||||
$lists['content'] = $menuName;
|
||||
$this->msgData['msgmenu']['list'][$this->index] = $lists;
|
||||
++$this->index;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $appid
|
||||
* @param $pagepath
|
||||
* @param $thumb_media_id
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMiniprogrampageNews($title, $appid, $pagepath, $thumb_media_id): Result
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['miniprogrampage'] = [
|
||||
'title' => $title,
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath,
|
||||
'thumb_media_id' => $thumb_media_id,
|
||||
];
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filePath
|
||||
* @param $type
|
||||
* @param bool $isPermanent
|
||||
* @param string $title
|
||||
* @param string $introduction
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function upload($filePath, $type, bool $isPermanent = false, string $title = '', string $introduction = ''): Result
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception('文件不存在');
|
||||
}
|
||||
|
||||
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
||||
throw new Exception('暂不支持的文件类型');
|
||||
}
|
||||
|
||||
$token = $this->config->getAccessToken();
|
||||
if ($isPermanent) {
|
||||
$url = "/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
||||
} else {
|
||||
$url = "/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
||||
}
|
||||
|
||||
$mime = mime_content_type($filePath);
|
||||
|
||||
$real_path = new \CURLFile(realpath($filePath));
|
||||
|
||||
$data = ["media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime];
|
||||
if ($isPermanent && $mime == 'video/mp3') {
|
||||
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
||||
}
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||
$client->post($url, $data);
|
||||
$client->close();
|
||||
|
||||
$this->msgData = [];
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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 array
|
||||
*/
|
||||
public function getContents(): array
|
||||
{
|
||||
return $this->msgData;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws Exception
|
||||
*/
|
||||
private function sendKefuMsg()
|
||||
{
|
||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $this->msgData);
|
||||
$client->close();
|
||||
|
||||
$this->msgData = [];
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,553 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use wchat\common\Help;
|
||||
|
||||
class Notify extends SmallProgram
|
||||
{
|
||||
public mixed $appid = '';
|
||||
public mixed $mch_id = '';
|
||||
public mixed $device_info = '';
|
||||
public mixed $nonce_str = '';
|
||||
public mixed $sign = '';
|
||||
public mixed $sign_type = '';
|
||||
public mixed $result_code = '';
|
||||
public mixed $err_code = '';
|
||||
public mixed $err_code_des = '';
|
||||
public mixed $openid = '';
|
||||
public mixed $is_subscribe = '';
|
||||
public mixed $trade_type = '';
|
||||
public mixed $bank_type = '';
|
||||
public mixed $total_fee = '';
|
||||
public mixed $settlement_total_fee = '';
|
||||
public mixed $fee_type = '';
|
||||
public mixed $cash_fee = '';
|
||||
public mixed $cash_fee_type = '';
|
||||
public mixed $coupon_fee = '';
|
||||
public mixed $coupon_count = '';
|
||||
public mixed $coupon_type_n = '';
|
||||
public mixed $coupon_id_n = '';
|
||||
public mixed $coupon_fee_n = '';
|
||||
public mixed $transaction_id = '';
|
||||
public mixed $out_trade_no = '';
|
||||
public mixed $attach = '';
|
||||
public mixed $time_end = '';
|
||||
public mixed $return_code = '';
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* 判断是否完成支付
|
||||
*/
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
if ($this->getReturnCode() != "SUCCESS") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->getResultCode() != 'SUCCESS') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
public function setPayNotifyData(array $params): static
|
||||
{
|
||||
if (!$this->validation($params)) {
|
||||
$this->setResultCode('FAIL');
|
||||
$this->setErrCodeDes('签名错误');
|
||||
unset($params['result_code'], $params['err_code_des']);
|
||||
}
|
||||
foreach ($params as $key => $val) {
|
||||
$this->__set($key, $val);
|
||||
}
|
||||
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
|
||||
* @return bool
|
||||
*/
|
||||
public function validation(array $params): bool
|
||||
{
|
||||
$sign = $params['sign'];
|
||||
unset($params['sign']);
|
||||
|
||||
$signType = $this->config->getSignType();
|
||||
$privateKey = $this->config->getKey();
|
||||
$nowSign = Help::sign($params, $privateKey, $signType);
|
||||
if ($sign === $nowSign) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getAppid(): mixed
|
||||
{
|
||||
return $this->appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $appid
|
||||
*/
|
||||
public function setAppid(mixed $appid): void
|
||||
{
|
||||
$this->appid = $appid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getMchId(): mixed
|
||||
{
|
||||
return $this->mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $mch_id
|
||||
*/
|
||||
public function setMchId(mixed $mch_id): void
|
||||
{
|
||||
$this->mch_id = $mch_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getDeviceInfo(): mixed
|
||||
{
|
||||
return $this->device_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $device_info
|
||||
*/
|
||||
public function setDeviceInfo(mixed $device_info): void
|
||||
{
|
||||
$this->device_info = $device_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getNonceStr(): mixed
|
||||
{
|
||||
return $this->nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $nonce_str
|
||||
*/
|
||||
public function setNonceStr(mixed $nonce_str): void
|
||||
{
|
||||
$this->nonce_str = $nonce_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getSign(): mixed
|
||||
{
|
||||
return $this->sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $sign
|
||||
*/
|
||||
public function setSign(mixed $sign): void
|
||||
{
|
||||
$this->sign = $sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getSignType(): mixed
|
||||
{
|
||||
return $this->sign_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $sign_type
|
||||
*/
|
||||
public function setSignType(mixed $sign_type): void
|
||||
{
|
||||
$this->sign_type = $sign_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getResultCode(): mixed
|
||||
{
|
||||
return $this->result_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $result_code
|
||||
*/
|
||||
public function setResultCode(mixed $result_code): void
|
||||
{
|
||||
$this->result_code = $result_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getErrCode(): mixed
|
||||
{
|
||||
return $this->err_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $err_code
|
||||
*/
|
||||
public function setErrCode(mixed $err_code): void
|
||||
{
|
||||
$this->err_code = $err_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getErrCodeDes(): mixed
|
||||
{
|
||||
return $this->err_code_des;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $err_code_des
|
||||
*/
|
||||
public function setErrCodeDes(mixed $err_code_des): void
|
||||
{
|
||||
$this->err_code_des = $err_code_des;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getOpenid(): mixed
|
||||
{
|
||||
return $this->openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $openid
|
||||
*/
|
||||
public function setOpenid(mixed $openid): void
|
||||
{
|
||||
$this->openid = $openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getIsSubscribe(): mixed
|
||||
{
|
||||
return $this->is_subscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $is_subscribe
|
||||
*/
|
||||
public function setIsSubscribe(mixed $is_subscribe): void
|
||||
{
|
||||
$this->is_subscribe = $is_subscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTradeType(): mixed
|
||||
{
|
||||
return $this->trade_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $trade_type
|
||||
*/
|
||||
public function setTradeType(mixed $trade_type): void
|
||||
{
|
||||
$this->trade_type = $trade_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getBankType(): mixed
|
||||
{
|
||||
return $this->bank_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $bank_type
|
||||
*/
|
||||
public function setBankType(mixed $bank_type): void
|
||||
{
|
||||
$this->bank_type = $bank_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTotalFee(): mixed
|
||||
{
|
||||
return $this->total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $total_fee
|
||||
*/
|
||||
public function setTotalFee(mixed $total_fee): void
|
||||
{
|
||||
$this->total_fee = $total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getSettlementTotalFee(): mixed
|
||||
{
|
||||
return $this->settlement_total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $settlement_total_fee
|
||||
*/
|
||||
public function setSettlementTotalFee(mixed $settlement_total_fee): void
|
||||
{
|
||||
$this->settlement_total_fee = $settlement_total_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getFeeType(): mixed
|
||||
{
|
||||
return $this->fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $fee_type
|
||||
*/
|
||||
public function setFeeType(mixed $fee_type): void
|
||||
{
|
||||
$this->fee_type = $fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCashFee(): mixed
|
||||
{
|
||||
return $this->cash_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $cash_fee
|
||||
*/
|
||||
public function setCashFee(mixed $cash_fee): void
|
||||
{
|
||||
$this->cash_fee = $cash_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCashFeeType(): mixed
|
||||
{
|
||||
return $this->cash_fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $cash_fee_type
|
||||
*/
|
||||
public function setCashFeeType(mixed $cash_fee_type): void
|
||||
{
|
||||
$this->cash_fee_type = $cash_fee_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponFee(): mixed
|
||||
{
|
||||
return $this->coupon_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $coupon_fee
|
||||
*/
|
||||
public function setCouponFee(mixed $coupon_fee): void
|
||||
{
|
||||
$this->coupon_fee = $coupon_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponCount(): mixed
|
||||
{
|
||||
return $this->coupon_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $coupon_count
|
||||
*/
|
||||
public function setCouponCount(mixed $coupon_count): void
|
||||
{
|
||||
$this->coupon_count = $coupon_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponTypeN(): mixed
|
||||
{
|
||||
return $this->coupon_type_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $coupon_type_n
|
||||
*/
|
||||
public function setCouponTypeN(mixed $coupon_type_n): void
|
||||
{
|
||||
$this->coupon_type_n = $coupon_type_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponIdN(): mixed
|
||||
{
|
||||
return $this->coupon_id_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $coupon_id_n
|
||||
*/
|
||||
public function setCouponIdN(mixed $coupon_id_n): void
|
||||
{
|
||||
$this->coupon_id_n = $coupon_id_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCouponFeeN(): mixed
|
||||
{
|
||||
return $this->coupon_fee_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $coupon_fee_n
|
||||
*/
|
||||
public function setCouponFeeN(mixed $coupon_fee_n): void
|
||||
{
|
||||
$this->coupon_fee_n = $coupon_fee_n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTransactionId(): mixed
|
||||
{
|
||||
return $this->transaction_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $transaction_id
|
||||
*/
|
||||
public function setTransactionId(mixed $transaction_id): void
|
||||
{
|
||||
$this->transaction_id = $transaction_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getOutTradeNo(): mixed
|
||||
{
|
||||
return $this->out_trade_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $out_trade_no
|
||||
*/
|
||||
public function setOutTradeNo(mixed $out_trade_no): void
|
||||
{
|
||||
$this->out_trade_no = $out_trade_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getAttach(): mixed
|
||||
{
|
||||
return $this->attach;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $attach
|
||||
*/
|
||||
public function setAttach(mixed $attach): void
|
||||
{
|
||||
$this->attach = $attach;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getTimeEnd(): mixed
|
||||
{
|
||||
return $this->time_end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $time_end
|
||||
*/
|
||||
public function setTimeEnd(mixed $time_end): void
|
||||
{
|
||||
$this->time_end = $time_end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getReturnCode(): mixed
|
||||
{
|
||||
return $this->return_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|string $return_code
|
||||
*/
|
||||
public function setReturnCode(mixed $return_code): void
|
||||
{
|
||||
$this->return_code = $return_code;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 9:49
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
class PublicTemplate extends SmallProgram
|
||||
{
|
||||
|
||||
private array $keywords = [];
|
||||
private string $templateId = '';
|
||||
private array $first = [];
|
||||
private array $remark = [];
|
||||
private string $openId = '';
|
||||
private string $defaultUrl = 'http://weixin.qq.com/download';
|
||||
private string $sendUrl = '/cgi-bin/message/template/send';
|
||||
private array $miniprogram = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $templateId
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openId
|
||||
*/
|
||||
public function setOpenId($openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $defaultUrl
|
||||
*/
|
||||
public function setDefaultUrl($defaultUrl)
|
||||
{
|
||||
$this->defaultUrl = $defaultUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($name, $context, string $color = '')
|
||||
{
|
||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param null $color
|
||||
*/
|
||||
public function addKeyword($name, $context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords[$name] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setFirst($context, string $color = '#f00'): void
|
||||
{
|
||||
$this->first = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param string $color
|
||||
* @return void
|
||||
*/
|
||||
public function setRemark($context, string $color = '#000'): void
|
||||
{
|
||||
$this->remark = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $appid
|
||||
* @param string $pagepath
|
||||
* @return void
|
||||
*/
|
||||
public function setMiniprogram($appid, string $pagepath): void
|
||||
{
|
||||
$this->miniprogram = [
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate(): Result
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
||||
|
||||
$keywords = $this->keywords;
|
||||
$keywords['first'] = $this->first;
|
||||
$keywords['remark'] = $this->remark;
|
||||
|
||||
$default = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
];
|
||||
if (!empty($this->miniprogram)) {
|
||||
$default['miniprogram'] = $this->miniprogram;
|
||||
}
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($url, $default);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class SecCheck
|
||||
* @package wchat
|
||||
*/
|
||||
class SecCheck extends SmallProgram
|
||||
{
|
||||
|
||||
private string $_url = '/wxa/img_sec_check?access_token=';
|
||||
|
||||
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
||||
|
||||
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
||||
|
||||
const MEDIA_VIDEO = 1;
|
||||
const MEDIA_IMAGE = 1;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return Result
|
||||
*/
|
||||
public function image(string $path = ''): Result
|
||||
{
|
||||
if (!file_exists($path)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$access_token = $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'multipart/form-data']);
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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 string $url
|
||||
* @param int $type
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
|
||||
{
|
||||
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
||||
throw new \Exception('暂不支持的文件类型');
|
||||
}
|
||||
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: $client->getBody());
|
||||
}
|
||||
$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 $params
|
||||
* @return ContentAsyncCheck|null
|
||||
*/
|
||||
public function readByEvent($params): ?ContentAsyncCheck
|
||||
{
|
||||
return ContentAsyncCheck::instance($params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return Result
|
||||
*/
|
||||
public function text($content): Result
|
||||
{
|
||||
if (empty($content)) {
|
||||
return $this->sendError('文件不存在', 404);
|
||||
}
|
||||
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
|
||||
|
||||
$client = new Client('api.weixin.qq.com', 443, true);
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\common\Multiprogramming;
|
||||
|
||||
class SmallProgram extends Multiprogramming
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use wchat\common\Result;
|
||||
|
||||
/**
|
||||
* Class Subject
|
||||
* @package wchat\wx
|
||||
*/
|
||||
class Subject extends \wchat\base\Subject
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return 'cgi-bin/message/subscribe/send';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return 'api.weixin.qq.com';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 9:49
|
||||
*/
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
/**
|
||||
* Class Template
|
||||
* @package wchat\wx
|
||||
*/
|
||||
class Template extends \wchat\base\Template
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return '/cgi-bin/message/wxopen/template/send';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return 'api.weixin.qq.com';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat\wx;
|
||||
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Result;
|
||||
|
||||
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: $client->getBody());
|
||||
}
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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: $client->getBody());
|
||||
}
|
||||
$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: $client->getBody());
|
||||
}
|
||||
|
||||
$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,50 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Client;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class TransferBatches extends SmallProgram
|
||||
{
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderNo
|
||||
* @param int $total
|
||||
* @param string $openId
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
#[ArrayShape(['code_url' => "string"])]
|
||||
public function request(string $orderNo, int $total, string $openId): array
|
||||
{
|
||||
$body['appid'] = $this->getConfig()->getAppid();
|
||||
$body['out_trade_no'] = $orderNo;
|
||||
$body["batch_name"] = $this->getConfig()->getBody();
|
||||
$body["batch_remark"] = $this->getConfig()->getBody();
|
||||
$body["total_amount"] = $total;
|
||||
$body["total_num"] = 1;
|
||||
$body["transfer_detail_list"] = [
|
||||
[
|
||||
"out_detail_no" => $orderNo,
|
||||
"transfer_amount" => $total,
|
||||
"transfer_remark" => $this->getConfig()->getBody(),
|
||||
"openid" => $openId,
|
||||
]
|
||||
];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/transfer/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')
|
||||
->withAgent('application/json')->withBody($json)
|
||||
->post('/v3/transfer/batches');
|
||||
$client->close();
|
||||
|
||||
return json_decode($client->getBody(), TRUE);
|
||||
}
|
||||
}
|
||||
@@ -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,46 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
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($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,49 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
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($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($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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-205
@@ -1,205 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/19 0019
|
||||
* Time: 16:12
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
class Account extends Miniprogarampage
|
||||
{
|
||||
|
||||
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__ . '/../../../';
|
||||
|
||||
|
||||
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
|
||||
* @return Result
|
||||
*/
|
||||
public function login($code)
|
||||
{
|
||||
$param['appid'] = $this->config->getAppid();
|
||||
$param['secret'] = $this->config->getAppsecret();
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
$this->request->setMethod(WxClient::GET);
|
||||
$this->request->addHeader('Content-Type', 'text/xml');
|
||||
|
||||
return $this->request->get('/sns/jscode2session', $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openid
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getPublicUserInfo($openid)
|
||||
{
|
||||
$query = [
|
||||
'access_token' => $this->getAccessToken(),
|
||||
'openid' => $openid,
|
||||
'lang' => 'zh_CN'
|
||||
];
|
||||
|
||||
$this->request->setMethod(WxClient::GET);
|
||||
$this->request->setIsSSL(true);
|
||||
return $this->request->get($this->publicInfo, $query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
$config = Wx::getMiniProGaRamPage()->getConfig();
|
||||
if (strlen($sessionKey) != 24) {
|
||||
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
||||
}
|
||||
|
||||
$aesKey = base64_decode($sessionKey);
|
||||
if (strlen($iv) != 24) {
|
||||
throw new \Exception('base64解密失败', $this->IllegalIv);
|
||||
}
|
||||
|
||||
$aesIV = base64_decode($iv);
|
||||
$aesCipher = base64_decode($encryptedData);
|
||||
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
||||
if ($result === false) {
|
||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||
}
|
||||
|
||||
$dataObj = json_decode($result);
|
||||
if ($dataObj->watermark->appid != $config->getAppid()) {
|
||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||
}
|
||||
|
||||
if ($asArray) {
|
||||
return get_object_vars($dataObj);
|
||||
}
|
||||
|
||||
return $dataObj;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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(WxClient::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(WxClient::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(WxClient::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;
|
||||
}
|
||||
}
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat;
|
||||
|
||||
|
||||
class Help extends Miniprogarampage
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function toXml(array $data)
|
||||
{
|
||||
$xml = "<xml>";
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($val)) {
|
||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||
} else {
|
||||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
$xml .= "</xml>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @return mixed
|
||||
*/
|
||||
public static function toArray($xml)
|
||||
{
|
||||
if (!is_null($json = json_decode($xml, TRUE))) {
|
||||
return $json;
|
||||
}
|
||||
if (is_array($json)) {
|
||||
return $json;
|
||||
}
|
||||
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
* @return string
|
||||
*
|
||||
* 随机字符串
|
||||
*/
|
||||
public static function random($length = 20)
|
||||
{
|
||||
$res = [];
|
||||
$str = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$str .= strtoupper($str) . '1234567890';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$rand = substr($str, rand(0, strlen($str) - 2), 1);
|
||||
if (empty($rand)) {
|
||||
$rand = substr($str, strlen($str) - 3, 1);
|
||||
}
|
||||
array_push($res, $rand);
|
||||
}
|
||||
|
||||
return implode($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param $key
|
||||
* @param $type
|
||||
* @return string
|
||||
*/
|
||||
public static function sign(array $array, $key, $type)
|
||||
{
|
||||
ksort($array, SORT_STRING);
|
||||
$string = '';
|
||||
foreach ($array as $key => $val) {
|
||||
if (empty($string)) {
|
||||
$string = $key . '=' . $val;
|
||||
} else {
|
||||
$string .= '&' . $key . '=' . $val;
|
||||
}
|
||||
}
|
||||
$string .= '&key=' . $key;
|
||||
|
||||
if ($type == 'MD5') {
|
||||
return strtoupper(md5($string));
|
||||
} else {
|
||||
return hash('sha256', $string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-311
@@ -1,311 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat;
|
||||
|
||||
|
||||
class Message extends Miniprogarampage
|
||||
{
|
||||
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 $msgData = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $openid
|
||||
*/
|
||||
public function setOpenid(string $openid)
|
||||
{
|
||||
$this->openid = $openid;
|
||||
$this->msgData['touser'] = $openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendTextNews(string $content)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'text';
|
||||
$this->msgData['text[content]'] = $content;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendImageNews(string $media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'image';
|
||||
$this->msgData['image[media_id]'] = $media_id;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendVoiceNews(string $media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'voice';
|
||||
$this->msgData['voice[media_id]'] = $media_id;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendMpNewsNews(string $media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'mpnews';
|
||||
$this->msgData['mpnews[media_id]'] = $media_id;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @param string $description
|
||||
* @param string $url
|
||||
* @param string $picurl
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendNewsNews(string $title, string $description, string $url, string $picurl)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'news';
|
||||
$this->msgData['news[articles][0][title]'] = $title;
|
||||
$this->msgData['news[articles][0][description]'] = $description;
|
||||
$this->msgData['news[articles][0][url]'] = $url;
|
||||
$this->msgData['news[articles][0][picurl]'] = $picurl;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendCardNews(string $title)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'wxcard';
|
||||
$this->msgData['wxcard[card_id]'] = $title;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $media_id
|
||||
* @param string $thumb_media_id
|
||||
* @param string $title
|
||||
* @param string $description
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'video';
|
||||
$this->msgData['video[media_id]'] = $media_id;
|
||||
$this->msgData['video[thumb_media_id]'] = $thumb_media_id;
|
||||
$this->msgData['video[title]'] = $title;
|
||||
$this->msgData['video[description]'] = $description;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $musicurl
|
||||
* @param string $hqmusicurl
|
||||
* @param string $thumb_media_id
|
||||
* @param string $title
|
||||
* @param string $description
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'music';
|
||||
$this->msgData['music[title]'] = $title;
|
||||
$this->msgData['music[description]'] = $description;
|
||||
$this->msgData['music[musicurl]'] = $musicurl;
|
||||
$this->msgData['music[hqmusicurl]'] = $hqmusicurl;
|
||||
$this->msgData['music[thumb_media_id]'] = $thumb_media_id;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $head_content
|
||||
* @param string $tail_content
|
||||
* @param array $menus
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendMenuNews(string $head_content, string $tail_content, array $menus = [])
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['msgmenu[head_content]'] = $head_content;
|
||||
$this->msgData['msgmenu[tail_content]'] = $tail_content;
|
||||
|
||||
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
|
||||
throw new \Exception('菜单选项必须有2个');
|
||||
}
|
||||
|
||||
foreach ($menus as $key => $val) {
|
||||
$this->addNewsMenu($val['id'], $val['name']);
|
||||
}
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
private $index = 0;
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $menuName
|
||||
* @return $this
|
||||
*/
|
||||
public function addNewsMenu($id, $menuName)
|
||||
{
|
||||
$this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id;
|
||||
$this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName;
|
||||
|
||||
++$this->index;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $appid
|
||||
* @param $pagepath
|
||||
* @param $thumb_media_id
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id)
|
||||
{
|
||||
$this->msgData['msgtype'] = 'msgmenu';
|
||||
$this->msgData['miniprogrampage[title]'] = $title;
|
||||
$this->msgData['miniprogrampage[appid]'] = $appid;
|
||||
$this->msgData['miniprogrampage[pagepath]'] = $pagepath;
|
||||
$this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id;
|
||||
|
||||
return $this->sendKefuMsg();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filePath
|
||||
* @param string $type
|
||||
* @param bool $isPermanent
|
||||
* @param string $title
|
||||
* @param string $introduction
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '')
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \Exception('文件不存在');
|
||||
}
|
||||
|
||||
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
|
||||
throw new \Exception('暂不支持的文件类型');
|
||||
}
|
||||
|
||||
$token = $this->getAccessToken();
|
||||
if ($isPermanent) {
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
|
||||
} else {
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
|
||||
}
|
||||
|
||||
$mime = mime_content_type($filePath);
|
||||
|
||||
$real_path = new \CURLFile(realpath($filePath));
|
||||
|
||||
$data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[content-type]' => $mime);
|
||||
if ($isPermanent && $mime == 'video/mp3') {
|
||||
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
||||
}
|
||||
|
||||
$this->request->setMethod(WxClient::POST);
|
||||
|
||||
/** @var Result $body */
|
||||
$data = $this->request->post($url, $data);
|
||||
if (!$data->isResultsOK()) {
|
||||
throw new \Exception($data->getMessage());
|
||||
}
|
||||
|
||||
return $data->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mime
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function checkExtinfo($mime)
|
||||
{
|
||||
switch (strtolower($mime)) {
|
||||
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
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function sendKefuMsg()
|
||||
{
|
||||
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
|
||||
$this->request->setMethod(WxClient::POST);
|
||||
|
||||
/** @var Result $body */
|
||||
$body = $this->request->post($url, $data);
|
||||
|
||||
if (!$body->isResultsOK()) {
|
||||
throw new \Exception($body->getMessage());
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:23
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
|
||||
abstract class Miniprogarampage
|
||||
{
|
||||
|
||||
/** @var Config */
|
||||
protected $config;
|
||||
|
||||
/** @var mixed $instance */
|
||||
protected static $instance = null;
|
||||
|
||||
/** @var WxClient */
|
||||
protected $request = null;
|
||||
|
||||
/**
|
||||
* Miniprogarampage constructor.
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
if (!($this->request instanceof WxClient)) {
|
||||
$this->request = new WxClient();
|
||||
}
|
||||
$this->request->setIsSSL(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getInstance(Config $config)
|
||||
{
|
||||
if (static::$instance === null) {
|
||||
static::$instance = new static();
|
||||
}
|
||||
static::$instance->config = $config;
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool|mixed|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getAccessToken()
|
||||
{
|
||||
$access = $this->config->getAccessToken();
|
||||
if (!empty($access)) {
|
||||
return $access;
|
||||
}
|
||||
$this->request->setMethod(WxClient::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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 9:49
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
|
||||
class PublicTemplate extends Miniprogarampage
|
||||
{
|
||||
|
||||
private $keywords = [];
|
||||
private $templateId = '';
|
||||
private $first = '';
|
||||
private $remark = '';
|
||||
private $openId = '';
|
||||
private $defaultUrl = 'http://weixin.qq.com/download';
|
||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
|
||||
private $miniprogram = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templateId
|
||||
*/
|
||||
public function setTemplateId(string $templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $openId
|
||||
*/
|
||||
public function setOpenId(string $openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $defaultUrl
|
||||
*/
|
||||
public function setDefaultUrl(string $defaultUrl)
|
||||
{
|
||||
$this->defaultUrl = $defaultUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function replaceKeyword($name, $context, $color = '')
|
||||
{
|
||||
$this->keywords[$name] = ['value' => $context, 'color' => $color];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $context
|
||||
* @param null $color
|
||||
*/
|
||||
public function addKeyword($name, $context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords[$name] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function setFirst($context, $color = '#f00')
|
||||
{
|
||||
$this->first = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param string $color
|
||||
*/
|
||||
public function setRemark($context, $color = '#000')
|
||||
{
|
||||
$this->remark = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $appid
|
||||
* @param $pagepath
|
||||
*/
|
||||
public function setMiniprogram($appid, $pagepath)
|
||||
{
|
||||
$this->miniprogram = [
|
||||
'appid' => $appid,
|
||||
'pagepath' => $pagepath
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate()
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
|
||||
|
||||
$keywords = $this->keywords;
|
||||
$keywords['first'] = $this->first;
|
||||
$keywords['remark'] = $this->remark;
|
||||
|
||||
$default = [
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"url" => $this->defaultUrl,
|
||||
"data" => $keywords,
|
||||
];
|
||||
|
||||
if (!empty($this->miniprogram)) {
|
||||
$default['miniprogram'] = $this->miniprogram;
|
||||
}
|
||||
|
||||
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->addHeader('content-type', 'application/json');
|
||||
|
||||
$result = $this->request->post($url, $params);
|
||||
$result->append('postBody', $params);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
-184
@@ -1,184 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:22
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
|
||||
class Recharge extends Miniprogarampage
|
||||
{
|
||||
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 $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public function validation(array $params)
|
||||
{
|
||||
$sign = $params['sign'];
|
||||
unset($params['sign']);
|
||||
|
||||
$nowSign = Help::sign($params, $this->config->getKey(), $sign['signType']);
|
||||
if ($sign === $nowSign) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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,
|
||||
'sign_type' => $this->config->getSignType(),
|
||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->config->getNotifyUrl(),
|
||||
'trade_type' => $this->config->getTradeType(),
|
||||
];
|
||||
|
||||
$data = array_merge($data, $this->data);
|
||||
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
|
||||
$data['sign'] = Help::sign($data, $key, $sign_type);
|
||||
return Help::toXml($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['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(WxClient::POST);
|
||||
$this->request->addHeader('Content-Type', 'text/xml');
|
||||
return $this->request->send($url, $data);
|
||||
}
|
||||
}
|
||||
-153
@@ -1,153 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat;
|
||||
|
||||
/**
|
||||
* Class Result
|
||||
*
|
||||
* @package app\components
|
||||
*
|
||||
* @property $code
|
||||
* @property $message
|
||||
* @property $count
|
||||
* @property $data
|
||||
*/
|
||||
class Result
|
||||
{
|
||||
public $code;
|
||||
public $message;
|
||||
public $count = 0;
|
||||
public $data;
|
||||
public $header;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
foreach ($data as $key => $val) {
|
||||
$this->$key = $val;
|
||||
}
|
||||
|
||||
$this->header = $this->reloadResponse($this->header);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $header
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function reloadResponse($header)
|
||||
{
|
||||
$data = [];
|
||||
$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)
|
||||
{
|
||||
$this->$name = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTime()
|
||||
{
|
||||
return [
|
||||
'startTime' => $this->startTime,
|
||||
'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;
|
||||
}
|
||||
|
||||
|
||||
public function isResultsOK()
|
||||
{
|
||||
return $this->code == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* 批量设置返回头
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
foreach ($headers as $key => $val) {
|
||||
$this->setHeader($key, $val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* 设置返回头
|
||||
*/
|
||||
public function setHeader($key, $val)
|
||||
{
|
||||
header($key . ':' . $val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData($name = '')
|
||||
{
|
||||
if (!$this->isResultsOK()) {
|
||||
return '';
|
||||
}
|
||||
if (!empty($name) && isset($this->data[$name])) {
|
||||
return $this->data[$name];
|
||||
}
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $data
|
||||
* @return $this
|
||||
*/
|
||||
public function append($key, $data)
|
||||
{
|
||||
$this->data[$key] = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
}
|
||||
-143
@@ -1,143 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 9:49
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
|
||||
class Template extends Miniprogarampage
|
||||
{
|
||||
|
||||
private $keywords = [];
|
||||
private $templateId = '';
|
||||
private $formId = '';
|
||||
private $openId = '';
|
||||
private $defaultUrl = '';
|
||||
private $page = 'pages/index/index';
|
||||
private $emphasis_keyword = '';
|
||||
|
||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
|
||||
|
||||
|
||||
/**
|
||||
* @param array $keywords
|
||||
*/
|
||||
public function setKeywords(array $keywords)
|
||||
{
|
||||
$this->keywords = $keywords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templateId
|
||||
*/
|
||||
public function setTemplateId(string $templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $formId
|
||||
*/
|
||||
public function setFormId(string $formId)
|
||||
{
|
||||
$this->formId = $formId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $openId
|
||||
*/
|
||||
public function setOpenId(string $openId)
|
||||
{
|
||||
$this->openId = $openId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $defaultUrl
|
||||
*/
|
||||
public function setDefaultUrl(string $defaultUrl)
|
||||
{
|
||||
$this->defaultUrl = $defaultUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $page
|
||||
*/
|
||||
public function setPage(string $page)
|
||||
{
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $emphasis_keyword
|
||||
*/
|
||||
public function setEmphasisKeyword(string $emphasis_keyword)
|
||||
{
|
||||
$this->emphasis_keyword = $emphasis_keyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @param $context
|
||||
* @param $color
|
||||
*/
|
||||
public function replaceKeyword($index, $context, $color = '')
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . $index] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $color
|
||||
* @param $context
|
||||
*/
|
||||
public function addKeyword($context, $color = null)
|
||||
{
|
||||
if (empty($color)) {
|
||||
$color = '#000';
|
||||
}
|
||||
$this->keywords['keyword' . count($this->keywords)] = [
|
||||
'value' => $context,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 奴隶交易通知
|
||||
*/
|
||||
public function sendTemplate()
|
||||
{
|
||||
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
|
||||
|
||||
$params = json_encode([
|
||||
"touser" => $this->openId,
|
||||
"template_id" => $this->templateId,
|
||||
"page" => $this->page,
|
||||
"form_id" => $this->formId,
|
||||
"data" => $this->keywords,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
if (!empty($this->emphasis_keyword)) {
|
||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||
}
|
||||
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->addHeader('content-type', 'application/json');
|
||||
|
||||
$result = $this->request->post($url, $params);
|
||||
$result->append('postBody', $params);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace wchat;
|
||||
|
||||
|
||||
class Wx
|
||||
{
|
||||
|
||||
/** @var static $instance */
|
||||
private static $instance = null;
|
||||
|
||||
/** @var Config $config */
|
||||
private $config = null;
|
||||
|
||||
/**
|
||||
* @return static
|
||||
*/
|
||||
public static function getMiniProGaRamPage()
|
||||
{
|
||||
if (static::$instance === null) {
|
||||
static::$instance = new Wx();
|
||||
}
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Config
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @return $this
|
||||
*/
|
||||
public function setConfig(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Template
|
||||
*/
|
||||
public function getTemplate()
|
||||
{
|
||||
return Template::getInstance($this->config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return PublicTemplate
|
||||
*/
|
||||
public function getPublicTemplate()
|
||||
{
|
||||
return PublicTemplate::getInstance($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount()
|
||||
{
|
||||
return Account::getInstance($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Message
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return Message::getInstance($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Recharge
|
||||
*/
|
||||
public function getRecharge()
|
||||
{
|
||||
return Recharge::getInstance($this->config);
|
||||
}
|
||||
}
|
||||
-396
@@ -1,396 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace wchat;
|
||||
|
||||
use Swoole\Coroutine\Http\Client;
|
||||
|
||||
class WxClient
|
||||
{
|
||||
private $host = 'api.weixin.qq.com';
|
||||
|
||||
private $header = [];
|
||||
|
||||
private $callback = null;
|
||||
private $method = 'get';
|
||||
|
||||
private $url = '';
|
||||
private $isSSL = false;
|
||||
|
||||
const POST = 'post';
|
||||
const GET = 'get';
|
||||
const PUT = 'put';
|
||||
const DELETE = 'delete';
|
||||
const OPTIONS = 'option';
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
*/
|
||||
public function setHost(string $host)
|
||||
{
|
||||
$this->host = $host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $header
|
||||
*/
|
||||
public function setHeader(array $header)
|
||||
{
|
||||
$this->header = $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isSSL
|
||||
*/
|
||||
public function setIsSSL(bool $isSSL)
|
||||
{
|
||||
$this->isSSL = $isSSL;
|
||||
if ($this->isSSL) {
|
||||
$ssl = Wx::getMiniProGaRamPage()->getConfig();
|
||||
$this->header['ssl_cert_file'] = $ssl->getSslCert();
|
||||
$this->header['ssl_key_file'] = $ssl->getSslKey();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function request($url, $data = [])
|
||||
{
|
||||
if (
|
||||
strpos($url, 'http://') === 0 ||
|
||||
strpos($url, 'https://') === 0
|
||||
) {
|
||||
return $this->curl($url, $data);
|
||||
}
|
||||
|
||||
if (function_exists('getIsCli') && getIsCli()) {
|
||||
return $this->coroutine($url, $data);
|
||||
}
|
||||
|
||||
if ($this->isSSL) {
|
||||
return $this->curl('https://' . $this->host . '/' . $url, $data);
|
||||
} else {
|
||||
return $this->curl('http://' . $this->host . '/' . $url, $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
* @throws \Exception
|
||||
*
|
||||
* 使用swoole协程方式请求
|
||||
*/
|
||||
private function coroutine($url, $data = [])
|
||||
{
|
||||
$_data = $this->paramEncode($data);
|
||||
if ($this->method == 'get' && is_array($_data)) {
|
||||
$url .= '?' . http_build_query($_data);
|
||||
}
|
||||
|
||||
$client = $this->getClient($this->getHostPort(), $url, $data);
|
||||
if ($client->statusCode < 0) {
|
||||
throw new \Exception($client->errMsg);
|
||||
}
|
||||
$body = $client->body;
|
||||
$client->close();
|
||||
|
||||
return $this->structure($body, $_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
private function getHostIp()
|
||||
{
|
||||
$host = \Co::getAddrInfo($this->host);
|
||||
return array_shift($host);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
private function getHostPort()
|
||||
{
|
||||
$port = 80;
|
||||
if ($this->isSSL) $port = 443;
|
||||
return $port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $port
|
||||
* @param $url
|
||||
* @param $data
|
||||
* @return Client
|
||||
*/
|
||||
private function getClient($port, $url, $data)
|
||||
{
|
||||
$host = $this->getHostIp();
|
||||
$client = new Client($host, $port, $this->isSSL);
|
||||
if (!empty($this->header)) {
|
||||
$client->setHeaders($this->header);
|
||||
}
|
||||
|
||||
switch (strtolower($this->method)) {
|
||||
case self::GET:
|
||||
$client->get($url);
|
||||
break;
|
||||
default:
|
||||
$client->post($url, $data);
|
||||
}
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return array|mixed|Result
|
||||
*/
|
||||
private function curl($url, $data = [])
|
||||
{
|
||||
$data = $this->paramEncode($data, self::POST);
|
||||
$ch = $this->structureCurlRequest($url, $data);
|
||||
|
||||
if ($this->method != self::GET) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
} else if ($this->method == self::POST) {
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
|
||||
$output = curl_exec($ch);
|
||||
if ($output === FALSE) {
|
||||
return new Result(['code' => 500, 'message' => curl_error($ch)]);
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
return $this->structure($output, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $_data
|
||||
* @return resource
|
||||
*/
|
||||
private function structureCurlRequest($url, $_data)
|
||||
{
|
||||
$ch = curl_init();
|
||||
if ($this->method == self::GET) {
|
||||
$url = $url . '?' . $_data;
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
|
||||
curl_setopt($ch, CURLOPT_HEADER, FALSE);
|
||||
if (!empty($this->header)) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
|
||||
}
|
||||
|
||||
if ($this->isSSL) {
|
||||
curl_setopt($ch, CURLOPT_SSLCERT, $this->header['ssl_cert_file']);
|
||||
curl_setopt($ch, CURLOPT_SSLKEY, $this->header['ssl_key_file']);
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
return $ch;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $body
|
||||
* @param $_data
|
||||
* @return array|mixed|Result
|
||||
* 构建返回体
|
||||
*/
|
||||
private function structure($body, $_data)
|
||||
{
|
||||
$this->setIsSSL(false);
|
||||
$this->setHeaders([]);
|
||||
|
||||
if ($this->callback !== NULL) {
|
||||
$result = call_user_func($this->callback, $body, $_data);
|
||||
} else {
|
||||
$result = $this->formatResponseBody($body);
|
||||
}
|
||||
|
||||
$this->setCallback(null);
|
||||
if (!is_array($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return new Result($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $body
|
||||
* @return array|Result
|
||||
*/
|
||||
private function formatResponseBody($body)
|
||||
{
|
||||
$result = [];
|
||||
if (is_null($results = json_decode($body, TRUE))) {
|
||||
$data = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
$results = json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
if (!is_array($results)) {
|
||||
$result = new Result(['code' => 505, 'message' => '服务器返回体错误!']);
|
||||
} else if (isset($results['errcode'])) {
|
||||
$result['code'] = $results['errcode'];
|
||||
$result['message'] = $results['errmsg'];
|
||||
} else {
|
||||
$result['code'] = 0;
|
||||
$result['message'] = 'system success.';
|
||||
$result['data'] = $results;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $arr
|
||||
* @param string $pushType
|
||||
*
|
||||
* @return array|string
|
||||
* 将请求参数进行编码
|
||||
*/
|
||||
private function paramEncode($arr, $pushType = 'post')
|
||||
{
|
||||
if (!is_array($arr)) {
|
||||
return $arr;
|
||||
}
|
||||
$_tmp = [];
|
||||
foreach ($arr as $Key => $val) {
|
||||
$_tmp[$Key] = $val;
|
||||
}
|
||||
|
||||
return ($pushType == 'post' ? $_tmp : http_build_query($_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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user