Compare commits
297 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
+102
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use wchat\common\Config;
|
||||||
|
use wchat\common\Progaram;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Container
|
||||||
|
*/
|
||||||
|
class Container
|
||||||
|
{
|
||||||
|
|
||||||
|
/** @var Container */
|
||||||
|
private static $_instance;
|
||||||
|
|
||||||
|
/** @var \common\Config */
|
||||||
|
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, 'initConfig')) {
|
||||||
|
$newInstance->initConfig($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, 'initConfig')) {
|
||||||
|
$newInstance->initConfig($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
|
```php
|
||||||
|
|
||||||
$config = new \wchat\Config();
|
$config = new \common\Config();
|
||||||
$config->setAppid('');
|
$config->setAppid('');
|
||||||
$config->setAppsecret('');
|
$config->setAppsecret('');
|
||||||
$config->setMchId('');
|
$config->setMchId('');
|
||||||
|
|||||||
+5
-3
@@ -13,9 +13,11 @@
|
|||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"wchat\\": "wx",
|
"wchat\\": "wchat"
|
||||||
"qq\\": "qq"
|
},
|
||||||
}
|
"files": [
|
||||||
|
"Container.php"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">= 7.0"
|
"php": ">= 7.0"
|
||||||
|
|||||||
-215
@@ -1,215 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/19 0019
|
|
||||||
* Time: 16:12
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace qq;
|
|
||||||
|
|
||||||
use wchat\Result;
|
|
||||||
use wchat\WxClient;
|
|
||||||
use wchat\Miniprogarampage;
|
|
||||||
|
|
||||||
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';
|
|
||||||
|
|
||||||
if (empty($code)) {
|
|
||||||
return new Result(['code' => 404, 'message' => '临时登录凭证不能为空.']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->request->setHost('api.q.qq.com');
|
|
||||||
$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 = QqSDK::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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/8 0008
|
|
||||||
* Time: 9:49
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace qq;
|
|
||||||
|
|
||||||
use wchat\Miniprogarampage;
|
|
||||||
use wchat\Result;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-116
@@ -1,116 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace qq;
|
|
||||||
|
|
||||||
use wchat\WxClient;
|
|
||||||
use wchat\Config;
|
|
||||||
|
|
||||||
class QqSDK
|
|
||||||
{
|
|
||||||
|
|
||||||
/** @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 QqSDK();
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function getNotify()
|
|
||||||
{
|
|
||||||
return Notify::getInstance($this->config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return WxClient
|
|
||||||
*/
|
|
||||||
public function getClient()
|
|
||||||
{
|
|
||||||
$client = WxClient::getInstance();
|
|
||||||
$client->setAgent($this->config->getAgent());
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Token
|
|
||||||
*/
|
|
||||||
public function getAccessToken()
|
|
||||||
{
|
|
||||||
return Token::getInstance($this->config);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-155
@@ -1,155 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/8 0008
|
|
||||||
* Time: 9:49
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace qq;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\Miniprogarampage;
|
|
||||||
use wchat\Result;
|
|
||||||
use wchat\Help;
|
|
||||||
use wchat\WxClient;
|
|
||||||
|
|
||||||
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.q.qq.com/api/json/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) + 1)] = [
|
|
||||||
'value' => $context,
|
|
||||||
'color' => $color
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Result
|
|
||||||
* @throws \Exception
|
|
||||||
*
|
|
||||||
* 奴隶交易通知
|
|
||||||
*/
|
|
||||||
public function sendTemplate()
|
|
||||||
{
|
|
||||||
$url = $this->sendUrl . '?access_token=' . $this->config->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);
|
|
||||||
|
|
||||||
$this->openId = '';
|
|
||||||
$this->keywords = [];
|
|
||||||
$this->formId = '';
|
|
||||||
$this->templateId = '';
|
|
||||||
$this->page = '';
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace qq;
|
|
||||||
|
|
||||||
|
|
||||||
use wchat\Miniprogarampage;
|
|
||||||
|
|
||||||
class Token extends Miniprogarampage
|
|
||||||
{
|
|
||||||
|
|
||||||
private $url = 'https://api.q.qq.com/api/getToken';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function generateAccess_token()
|
|
||||||
{
|
|
||||||
$query = [
|
|
||||||
'grant_type' => 'client_credential',
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'secret' => $this->config->getAppsecret()
|
|
||||||
];
|
|
||||||
$param = $this->request->get($this->url, $query);
|
|
||||||
if (!$param->isResultsOK()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return $param->getData('access_token');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,39 +5,37 @@
|
|||||||
* Date: 2018/7/19 0019
|
* Date: 2018/7/19 0019
|
||||||
* Time: 18:38
|
* 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();
|
require_once __DIR__ . '/vendor/autoload.php';
|
||||||
$config->setAppid('');
|
|
||||||
$config->setAppsecret('');
|
|
||||||
$config->setMchId('');
|
|
||||||
$config->setKey('');
|
|
||||||
$config->setRemoteAddr('');
|
|
||||||
$config->setDeviceInfo('');
|
|
||||||
$config->setAccessToken('');
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
|
||||||
$instance = \wchat\Wx::getMiniProGaRamPage();
|
\Swoole\Coroutine::create(function () {
|
||||||
$instance->setConfig($config);
|
$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();
|
var_dump(PHP_LIBDIR, PHP_CONFIG_FILE_SCAN_DIR);
|
||||||
$recharge->cashWithdrawal(1, 'xxx', 'ooo');
|
|
||||||
$recharge->recharge(1, '', '');
|
|
||||||
|
|
||||||
$account = $instance->getAccount();
|
$config = new \wchat\common\Config();
|
||||||
$account->setSavePath('');
|
$config->setAppid('1110444685');
|
||||||
$account->login('');
|
$config->setAppsecret('XTK81p7WS0PqWjiF');
|
||||||
$account->createwxaqrcode('pages/index/index',200);
|
$config->setUsrSwoole(true);
|
||||||
$account->getwxacode('pages/index/index',150,true);
|
$config->setAccessToken('g3JqSiACJQuYuE7Hz683mz5oAi8aYuUktCc2C3a92IeB0_Kx9E92tJTtRQVUBAbucCSt');
|
||||||
$account->getwxacodeunlimit('pages/index/index',150,true);
|
|
||||||
|
|
||||||
|
/** @var \wchat\qq\SecCheck $model */
|
||||||
|
$model = \wchat\qq\SecCheck::getInstance($config);
|
||||||
|
var_dump($model->text('德生科技还是看得见粉红色的空间'));
|
||||||
|
|
||||||
$message = $instance->getMessage();
|
// $json = json_decode($data, true);
|
||||||
$message->setOpenid('');
|
//
|
||||||
$message->sendCardNews('');
|
// var_dump($json);
|
||||||
|
|
||||||
$template = $instance->getTemplate();
|
});
|
||||||
$template->setOpenId('');
|
|
||||||
$template->setFormId('');
|
|
||||||
$template->setTemplateId('');
|
|
||||||
$template->setPage('');
|
|
||||||
$template->sendTemplate();
|
|
||||||
|
|||||||
@@ -1,23 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\base;
|
||||||
|
|
||||||
|
|
||||||
class Subject extends Miniprogarampage
|
use wchat\common\Miniprogarampage;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Subject
|
||||||
|
* @package wchat\base
|
||||||
|
*/
|
||||||
|
abstract class Subject extends Miniprogarampage
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private $keywords = [];
|
private $keywords = [];
|
||||||
private $templateId = '';
|
private $templateId = '';
|
||||||
private $openId = '';
|
private $openId = '';
|
||||||
private $defaultUrl = '';
|
|
||||||
private $page = 'pages/index/index';
|
private $page = 'pages/index/index';
|
||||||
private $emphasis_keyword = '';
|
private $emphasis_keyword = '';
|
||||||
|
|
||||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $keywords
|
* @param array $keywords
|
||||||
*/
|
*/
|
||||||
@@ -42,14 +45,6 @@ class Subject extends Miniprogarampage
|
|||||||
$this->openId = $openId;
|
$this->openId = $openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $defaultUrl
|
|
||||||
*/
|
|
||||||
public function setDefaultUrl(string $defaultUrl)
|
|
||||||
{
|
|
||||||
$this->defaultUrl = $defaultUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $page
|
* @param string $page
|
||||||
*/
|
*/
|
||||||
@@ -98,32 +93,45 @@ class Subject extends Miniprogarampage
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract public function getUrl(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*
|
|
||||||
* 奴隶交易通知
|
|
||||||
*/
|
*/
|
||||||
public function sendTemplate()
|
public function sendTemplate()
|
||||||
{
|
{
|
||||||
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
$url = $this->getUrl() . '?access_token=' . $this->config->getAccessToken();
|
||||||
$params = json_encode([
|
$params = [
|
||||||
"touser" => $this->openId,
|
"touser" => $this->openId,
|
||||||
"template_id" => $this->templateId,
|
"template_id" => $this->templateId,
|
||||||
"page" => $this->page,
|
"page" => $this->page,
|
||||||
"data" => $this->keywords,
|
"data" => $this->keywords,
|
||||||
], JSON_UNESCAPED_UNICODE);
|
];
|
||||||
|
|
||||||
if (!empty($this->emphasis_keyword)) {
|
if (!empty($this->emphasis_keyword)) {
|
||||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->request->setIsSSL(true);
|
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
|
||||||
$this->request->addHeader('content-type', 'application/json');
|
|
||||||
|
|
||||||
|
$this->request->setIsSSL(true);
|
||||||
|
$this->request->addHeader('Content-Type', 'application/json');
|
||||||
|
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
$result = $this->request->post($url, $params);
|
$result = $this->request->post($url, $params);
|
||||||
$result->append('postBody', $params);
|
$result->append('postBody', $params);
|
||||||
|
|
||||||
|
return $this->reset($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $result
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function reset($result)
|
||||||
|
{
|
||||||
$this->openId = '';
|
$this->openId = '';
|
||||||
$this->keywords = [];
|
$this->keywords = [];
|
||||||
$this->templateId = '';
|
$this->templateId = '';
|
||||||
@@ -131,4 +139,5 @@ class Subject extends Miniprogarampage
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\base;
|
||||||
|
|
||||||
|
use wchat\common\Miniprogarampage;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Class Template
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/8 0008
|
|
||||||
* Time: 9:49
|
|
||||||
*/
|
*/
|
||||||
|
abstract class Template extends Miniprogarampage
|
||||||
namespace wchat;
|
|
||||||
|
|
||||||
class Template extends Miniprogarampage
|
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private $keywords = [];
|
private $keywords = [];
|
||||||
private $templateId = '';
|
private $templateId = '';
|
||||||
private $formId = '';
|
private $formId = '';
|
||||||
private $openId = '';
|
private $openId = '';
|
||||||
private $defaultUrl = '';
|
|
||||||
private $page = 'pages/index/index';
|
private $page = 'pages/index/index';
|
||||||
private $emphasis_keyword = '';
|
private $emphasis_keyword = '';
|
||||||
|
|
||||||
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $keywords
|
* @param array $keywords
|
||||||
*/
|
*/
|
||||||
@@ -54,14 +51,6 @@ class Template extends Miniprogarampage
|
|||||||
$this->openId = $openId;
|
$this->openId = $openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $defaultUrl
|
|
||||||
*/
|
|
||||||
public function setDefaultUrl(string $defaultUrl)
|
|
||||||
{
|
|
||||||
$this->defaultUrl = $defaultUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $page
|
* @param string $page
|
||||||
*/
|
*/
|
||||||
@@ -110,6 +99,8 @@ class Template extends Miniprogarampage
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract public function getUrl(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Result
|
* @return Result
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
@@ -118,25 +109,40 @@ class Template extends Miniprogarampage
|
|||||||
*/
|
*/
|
||||||
public function sendTemplate()
|
public function sendTemplate()
|
||||||
{
|
{
|
||||||
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
|
$access_token = $this->config->getAccessToken();
|
||||||
$params = json_encode([
|
if (empty($access_token)) {
|
||||||
|
throw new \Exception('request access_token con\'t null.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = $this->getUrl() . '?access_token=' . $access_token;
|
||||||
|
$params = [
|
||||||
"touser" => $this->openId,
|
"touser" => $this->openId,
|
||||||
"template_id" => $this->templateId,
|
"template_id" => $this->templateId,
|
||||||
"page" => $this->page,
|
"page" => $this->page,
|
||||||
"form_id" => $this->formId,
|
"form_id" => $this->formId,
|
||||||
"data" => $this->keywords,
|
"data" => $this->keywords,
|
||||||
], JSON_UNESCAPED_UNICODE);
|
];
|
||||||
|
|
||||||
if (!empty($this->emphasis_keyword)) {
|
if (!empty($this->emphasis_keyword)) {
|
||||||
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
$params['emphasis_keyword'] = $this->emphasis_keyword;
|
||||||
}
|
}
|
||||||
|
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
|
||||||
$this->request->setIsSSL(true);
|
$this->request->setIsSSL(true);
|
||||||
$this->request->addHeader('content-type', 'application/json');
|
$this->request->addHeader('Content-Type', 'application/json');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
$result = $this->request->post($url, $params);
|
$result = $this->request->post($url, $params);
|
||||||
$result->append('postBody', $params);
|
$result->append('postBody', $params);
|
||||||
|
|
||||||
|
return $this->reset($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $result
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function reset($result)
|
||||||
|
{
|
||||||
$this->openId = '';
|
$this->openId = '';
|
||||||
$this->keywords = [];
|
$this->keywords = [];
|
||||||
$this->formId = '';
|
$this->formId = '';
|
||||||
@@ -145,4 +151,5 @@ class Template extends Miniprogarampage
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\common;
|
||||||
|
|
||||||
|
|
||||||
class Config
|
class Config
|
||||||
@@ -29,6 +29,9 @@ class Config
|
|||||||
*/
|
*/
|
||||||
private $device_info = 'WEB';
|
private $device_info = 'WEB';
|
||||||
|
|
||||||
|
private $token;
|
||||||
|
private $encodingAesKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*
|
*
|
||||||
@@ -101,6 +104,8 @@ class Config
|
|||||||
|
|
||||||
private $ssl_cert = '';
|
private $ssl_cert = '';
|
||||||
private $ssl_key = '';
|
private $ssl_key = '';
|
||||||
|
private $ssl_ca = '';
|
||||||
|
private $port = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@@ -108,6 +113,61 @@ class Config
|
|||||||
private $key = '';
|
private $key = '';
|
||||||
private $access_token = '';
|
private $access_token = '';
|
||||||
private $agent = '';
|
private $agent = '';
|
||||||
|
private $usrSwoole = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslCa(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_ca;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_ca
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function setSslCa(string $ssl_ca): Config
|
||||||
|
{
|
||||||
|
$this->ssl_ca = $ssl_ca;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPort(): string
|
||||||
|
{
|
||||||
|
return $this->port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $port
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function setPort(string $port): Config
|
||||||
|
{
|
||||||
|
$this->port = $port;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isUsrSwoole(): bool
|
||||||
|
{
|
||||||
|
return $this->usrSwoole;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $usrSwoole
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function setUsrSwoole(bool $usrSwoole): Config
|
||||||
|
{
|
||||||
|
$this->usrSwoole = $usrSwoole;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
@@ -127,7 +187,41 @@ class Config
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getToken()
|
||||||
|
{
|
||||||
|
return $this->token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $token
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function setToken($token)
|
||||||
|
{
|
||||||
|
$this->token = $token;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getEncodingAesKey()
|
||||||
|
{
|
||||||
|
return $this->encodingAesKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $encodingAesKey
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function setEncodingAesKey($encodingAesKey)
|
||||||
|
{
|
||||||
|
$this->encodingAesKey = $encodingAesKey;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\common;
|
||||||
|
|
||||||
|
|
||||||
|
class Decode
|
||||||
|
{
|
||||||
|
|
||||||
|
private $sessionKey;
|
||||||
|
private $iv;
|
||||||
|
private $encryptedData;
|
||||||
|
private $appId;
|
||||||
|
|
||||||
|
|
||||||
|
private $OK = 0;
|
||||||
|
private $IllegalAesKey = -41001;
|
||||||
|
private $IllegalIv = -41002;
|
||||||
|
private $IllegalBuffer = -41003;
|
||||||
|
private $DecodeBase64Error = -41004;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $sessionKey
|
||||||
|
* @return Decode
|
||||||
|
*/
|
||||||
|
public function setSessionKey($sessionKey)
|
||||||
|
{
|
||||||
|
$this->sessionKey = $sessionKey;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $iv
|
||||||
|
* @return Decode
|
||||||
|
*/
|
||||||
|
public function setIv($iv)
|
||||||
|
{
|
||||||
|
$this->iv = $iv;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $encryptedData
|
||||||
|
* @return Decode
|
||||||
|
*/
|
||||||
|
public function setEncryptedData($encryptedData)
|
||||||
|
{
|
||||||
|
$this->encryptedData = $encryptedData;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $appId
|
||||||
|
* @return Decode
|
||||||
|
*/
|
||||||
|
public function setAppId($appId)
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $asArray
|
||||||
|
* @return array|mixed
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function decode($asArray)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($asArray) {
|
||||||
|
return get_object_vars($dataObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dataObj;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\common;
|
||||||
|
|
||||||
|
|
||||||
class Help extends Miniprogarampage
|
class Help extends Miniprogarampage
|
||||||
@@ -32,16 +32,78 @@ class Help extends Miniprogarampage
|
|||||||
*/
|
*/
|
||||||
public static function toArray($xml)
|
public static function toArray($xml)
|
||||||
{
|
{
|
||||||
|
if (is_array($xml)) {
|
||||||
|
return $xml;
|
||||||
|
}
|
||||||
|
$matchQoute = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
|
||||||
|
if (!preg_match($matchQoute, $xml)) {
|
||||||
|
return self::jsonToArray($xml);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||||
|
if ($data !== false) {
|
||||||
|
$data = json_decode(json_encode($data), TRUE);
|
||||||
|
} else {
|
||||||
|
$data = $xml;
|
||||||
|
}
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
$data = $xml;
|
||||||
|
} finally {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function jsonToArray($xml)
|
||||||
|
{
|
||||||
|
$_xml = json_decode($xml, true);
|
||||||
|
if (is_null($_xml)) {
|
||||||
|
return $xml;
|
||||||
|
}
|
||||||
|
return $_xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $xml
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function xmlToArray($xml)
|
||||||
|
{
|
||||||
|
if (is_array($xml)) {
|
||||||
|
return $xml;
|
||||||
|
}
|
||||||
|
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
|
||||||
|
return json_decode(json_encode($data), TRUE);
|
||||||
|
}
|
||||||
if (!is_null($json = json_decode($xml, TRUE))) {
|
if (!is_null($json = json_decode($xml, TRUE))) {
|
||||||
return $json;
|
return $json;
|
||||||
}
|
}
|
||||||
if (is_array($json)) {
|
return $xml;
|
||||||
return $json;
|
|
||||||
}
|
|
||||||
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
|
||||||
return json_decode(json_encode($data), TRUE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $json
|
||||||
|
* @return false|mixed|string
|
||||||
|
*/
|
||||||
|
public static function toJson($json)
|
||||||
|
{
|
||||||
|
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
|
* @param int $length
|
||||||
@@ -0,0 +1,950 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\common;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
use Swoole\Coroutine\Http\Client as SClient;
|
||||||
|
use Swoole\Coroutine\System;
|
||||||
|
use Swoole\Coroutine\Client;
|
||||||
|
|
||||||
|
class HttpClient
|
||||||
|
{
|
||||||
|
|
||||||
|
private $host = '';
|
||||||
|
|
||||||
|
private $header = [];
|
||||||
|
|
||||||
|
private $callback = null;
|
||||||
|
private $method = 'get';
|
||||||
|
|
||||||
|
private $url = '';
|
||||||
|
private $isSSL = false;
|
||||||
|
private $agent = '';
|
||||||
|
private $isFileStream = false;
|
||||||
|
private $errorCodeField = '';
|
||||||
|
private $errorMsgField = '';
|
||||||
|
private $use_swoole = false;
|
||||||
|
|
||||||
|
private $ssl_cert_file = '';
|
||||||
|
private $ssl_key_file = '';
|
||||||
|
private $ca = '';
|
||||||
|
private $port = '';
|
||||||
|
|
||||||
|
/** @var string $_message 错误信息 */
|
||||||
|
private $_message = '';
|
||||||
|
private $_data = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCa(): string
|
||||||
|
{
|
||||||
|
return $this->ca;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ca
|
||||||
|
*/
|
||||||
|
public function setCa(string $ca): void
|
||||||
|
{
|
||||||
|
$this->ca = $ca;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPort(): string
|
||||||
|
{
|
||||||
|
return $this->port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $port
|
||||||
|
*/
|
||||||
|
public function setPort(string $port): void
|
||||||
|
{
|
||||||
|
$this->port = $port;
|
||||||
|
}
|
||||||
|
|
||||||
|
const POST = 'post';
|
||||||
|
const GET = 'get';
|
||||||
|
const PUT = 'put';
|
||||||
|
const DELETE = 'delete';
|
||||||
|
const OPTIONS = 'option';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HttpClient constructor.
|
||||||
|
*/
|
||||||
|
private function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
*/
|
||||||
|
public function setData($data)
|
||||||
|
{
|
||||||
|
$this->_data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslCertFile(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_cert_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_cert_file
|
||||||
|
*/
|
||||||
|
public function setSslCertFile(string $ssl_cert_file)
|
||||||
|
{
|
||||||
|
$this->ssl_cert_file = $ssl_cert_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSslKeyFile(): string
|
||||||
|
{
|
||||||
|
return $this->ssl_key_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $ssl_key_file
|
||||||
|
*/
|
||||||
|
public function setSslKeyFile(string $ssl_key_file)
|
||||||
|
{
|
||||||
|
$this->ssl_key_file = $ssl_key_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public static function NewRequest()
|
||||||
|
{
|
||||||
|
static $client = null;
|
||||||
|
if (!($client instanceof HttpClient)) {
|
||||||
|
$client = new HttpClient();
|
||||||
|
}
|
||||||
|
return $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setErrorField(string $name)
|
||||||
|
{
|
||||||
|
$this->errorCodeField = $name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $bool
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setUseSwoole($bool)
|
||||||
|
{
|
||||||
|
$this->use_swoole = $bool;
|
||||||
|
if ($this->use_swoole) {
|
||||||
|
function_exists('setCli') && setCli(true);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setErrorMsgField(string $name)
|
||||||
|
{
|
||||||
|
$this->errorMsgField = $name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $host
|
||||||
|
*/
|
||||||
|
public function setHost(string $host)
|
||||||
|
{
|
||||||
|
$this->host = $this->replaceHost($host);
|
||||||
|
$match_quest = '/^[a-zA-Z\-]+(\.[a-zA-Z\-])+/';
|
||||||
|
if (preg_match($match_quest, $this->host)) {
|
||||||
|
$this->addHeader('Host', $this->host);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param array $data
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
public function grpc($path, array $data)
|
||||||
|
{
|
||||||
|
$client = new Client(SWOOLE_TCP);
|
||||||
|
if (empty($this->host) || empty($this->port)) {
|
||||||
|
return new Result(['code' => 500, 'message' => 'Host and port is null']);
|
||||||
|
}
|
||||||
|
if (!$client->connect($this->host, $this->port)) {
|
||||||
|
return new Result(['code' => 500, 'message' => $client->errMsg]);
|
||||||
|
}
|
||||||
|
$path = $this->port . '/' . ltrim($path, '/');
|
||||||
|
|
||||||
|
$params['body'] = $data;
|
||||||
|
$params['header']['request_uri'] = $path;
|
||||||
|
$params['header']['request_method'] = 'grpc';
|
||||||
|
|
||||||
|
if ($client->send($params)) {
|
||||||
|
$recv = $client->recv();
|
||||||
|
$param = ['code' => 0, 'message' => Help::toArray($recv)];
|
||||||
|
} else {
|
||||||
|
$param = ['code' => 500, 'message' => $client->errMsg];
|
||||||
|
}
|
||||||
|
$client->close();
|
||||||
|
return new Result($param);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $key
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function setHeader($key, $value)
|
||||||
|
{
|
||||||
|
$this->header[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $key
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function addHeader($key, $value)
|
||||||
|
{
|
||||||
|
$this->header[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param null $callback
|
||||||
|
*/
|
||||||
|
public function setCallback($callback)
|
||||||
|
{
|
||||||
|
$this->callback = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $method
|
||||||
|
*/
|
||||||
|
public function setMethod(string $method)
|
||||||
|
{
|
||||||
|
$this->method = $method;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
*/
|
||||||
|
public function setUrl(string $url)
|
||||||
|
{
|
||||||
|
$this->url = $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAgent(string $agent)
|
||||||
|
{
|
||||||
|
$this->agent = $agent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $isSSL
|
||||||
|
*/
|
||||||
|
public function setIsSSL(bool $isSSL)
|
||||||
|
{
|
||||||
|
$this->isSSL = $isSSL;
|
||||||
|
if ($this->isSSL) {
|
||||||
|
$this->port = 443;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function getIsSSL()
|
||||||
|
{
|
||||||
|
return $this->isSSL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $isFIle
|
||||||
|
* 设置返回类型
|
||||||
|
*/
|
||||||
|
public function asFileStream($isFIle = true)
|
||||||
|
{
|
||||||
|
$this->isFileStream = $isFIle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function request($url, $data = [])
|
||||||
|
{
|
||||||
|
$data = $this->paramEncode($data);
|
||||||
|
if ($this->use_swoole) {
|
||||||
|
return $this->coroutine($this->matchHost($url), $data);
|
||||||
|
} else {
|
||||||
|
return $this->useCurl($url, $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isCli()
|
||||||
|
{
|
||||||
|
return function_exists('getIsCli') && getIsCli();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $string
|
||||||
|
* @return bool|string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function matchHost($string = '')
|
||||||
|
{
|
||||||
|
if (empty($string)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->isHttp($string)) {
|
||||||
|
$string = str_replace('http://', '', $string);
|
||||||
|
$hostAndUrls = explode('/', $string);
|
||||||
|
|
||||||
|
$this->host = array_shift($hostAndUrls);
|
||||||
|
$string = implode('/', $hostAndUrls);
|
||||||
|
} else if ($this->isHttps($string)) {
|
||||||
|
$string = str_replace('https://', '', $string);
|
||||||
|
$this->setIsSSL(true);
|
||||||
|
|
||||||
|
$hostAndUrls = explode('/', $string);
|
||||||
|
|
||||||
|
$this->host = array_shift($hostAndUrls);
|
||||||
|
$string = implode('/', $hostAndUrls);
|
||||||
|
} else if (empty($this->host)) {
|
||||||
|
$hostAndUrls = explode('/', $string);
|
||||||
|
$this->host = array_shift($hostAndUrls);
|
||||||
|
|
||||||
|
$string = implode('/', $hostAndUrls);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($this->host, ':') !== false) {
|
||||||
|
[$this->host, $this->port] = explode(':', $this->host);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) {
|
||||||
|
$this->host = System::gethostbyname($this->host);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->checkIsIp($this->host) && !$this->isDomainName($this->host)) {
|
||||||
|
throw new Exception('Client Host error.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return bool|mixed
|
||||||
|
*/
|
||||||
|
private function isDomainName($name)
|
||||||
|
{
|
||||||
|
if (!preg_match('/^[a-zA-Z\-0-9]+(\.[a-zA-Z\-0-9]+)+[^\/]?/', $name, $out)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $out[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return array|Result|mixed
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function useCurl($url, $data)
|
||||||
|
{
|
||||||
|
if ($this->isHttp($url) || $this->isHttps($url)) {
|
||||||
|
return $this->curl($url, $data);
|
||||||
|
}
|
||||||
|
$url = $this->matchHost(ltrim($url, '/'));
|
||||||
|
if (!empty($this->port)) {
|
||||||
|
$this->host .= ':' . $this->port;
|
||||||
|
}
|
||||||
|
if ($this->isSSL) {
|
||||||
|
return $this->curl('https://' . $this->host . '/' . $url, $data);
|
||||||
|
} else {
|
||||||
|
return $this->curl('http://' . $this->host . '/' . $url, $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $host
|
||||||
|
* @return string|string[]
|
||||||
|
*/
|
||||||
|
private function replaceHost($host)
|
||||||
|
{
|
||||||
|
if ($this->isHttp($host)) {
|
||||||
|
return str_replace('http://', '', $host);
|
||||||
|
}
|
||||||
|
if ($this->isHttps($host)) {
|
||||||
|
return str_replace('https://', '', $host);
|
||||||
|
}
|
||||||
|
return $host;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @return false|int
|
||||||
|
*/
|
||||||
|
private function checkIsIp($url)
|
||||||
|
{
|
||||||
|
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isHttp($url)
|
||||||
|
{
|
||||||
|
return strpos($url, 'http://') === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isHttps($url)
|
||||||
|
{
|
||||||
|
return strpos($url, 'https://') === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws Exception
|
||||||
|
* 使用swoole协程方式请求
|
||||||
|
*/
|
||||||
|
private function coroutine($url, $data = [])
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$client = $this->generate_client($this->host, $url, $data);
|
||||||
|
if ($client->statusCode < 0) {
|
||||||
|
throw new Exception($client->errMsg);
|
||||||
|
}
|
||||||
|
unset($this->_data);
|
||||||
|
|
||||||
|
$body = $this->resolve($client->getHeaders(), $client->body);
|
||||||
|
if ($client->getStatusCode() != 200) {
|
||||||
|
if (is_string($body)) {
|
||||||
|
$message = 'Request error code ' . $client->getStatusCode();
|
||||||
|
} else {
|
||||||
|
$message = $this->searchMessageByData($body);
|
||||||
|
}
|
||||||
|
$response['code'] = $client->getStatusCode();
|
||||||
|
$response['message'] = $message;
|
||||||
|
$response['data'] = $body;
|
||||||
|
} else {
|
||||||
|
$response = $this->structure($body, $data, $client->getHeaders());
|
||||||
|
}
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
$response['code'] = 500;
|
||||||
|
$response['message'] = $exception->getMessage();
|
||||||
|
$response['data'] = array_slice($exception->getTrace(), 0, 6);
|
||||||
|
$response['header'] = [];
|
||||||
|
}
|
||||||
|
if (!($response instanceof Result)) {
|
||||||
|
$response = new Result($response);
|
||||||
|
}
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private function getHostPort()
|
||||||
|
{
|
||||||
|
if (!empty($this->port)) {
|
||||||
|
return $this->port;
|
||||||
|
}
|
||||||
|
$port = 80;
|
||||||
|
if ($this->isSSL) $port = 443;
|
||||||
|
return $port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $host
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return SClient
|
||||||
|
*/
|
||||||
|
private function generate_client($host, $url, $data = [])
|
||||||
|
{
|
||||||
|
$client = new SClient($host, $this->getHostPort(), $this->isSSL);
|
||||||
|
if (strpos($url, '/') !== 0) {
|
||||||
|
$url = '/' . $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client->set($this->settings());
|
||||||
|
if (!empty($this->agent)) {
|
||||||
|
$this->header['User-Agent'] = $this->agent;
|
||||||
|
}
|
||||||
|
if (!empty($this->header)) {
|
||||||
|
$client->setHeaders($this->header);
|
||||||
|
}
|
||||||
|
$client->setMethod(strtoupper($this->method));
|
||||||
|
if (strtolower($this->method) == self::GET && !empty($data)) {
|
||||||
|
$url .= '?' . $data;
|
||||||
|
} else {
|
||||||
|
$this->_data = $this->mergeParams($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->_data)) {
|
||||||
|
$client->setData($this->_data);
|
||||||
|
}
|
||||||
|
$client->execute($url);
|
||||||
|
$client->close();
|
||||||
|
return $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $newData
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function mergeParams($newData)
|
||||||
|
{
|
||||||
|
if (empty($this->_data)) {
|
||||||
|
return $this->toRequest($newData);
|
||||||
|
} else if (empty($newData)) {
|
||||||
|
return $this->toRequest($this->_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
$newData = Help::toArray($newData);
|
||||||
|
$array = Help::toArray($this->_data);
|
||||||
|
|
||||||
|
$params = array_merge($array, $newData);
|
||||||
|
|
||||||
|
return $this->toRequest($params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @return false|mixed|string
|
||||||
|
*/
|
||||||
|
private function toRequest($data)
|
||||||
|
{
|
||||||
|
if (is_string($data)) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$contentType = 'application/x-www-form-urlencoded';
|
||||||
|
if (isset($this->header['Content-Type'])) {
|
||||||
|
$contentType = $this->header['Content-Type'];
|
||||||
|
} else if (isset($this->header['content-type'])) {
|
||||||
|
$contentType = $this->header['content-type'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($contentType, 'json') !== false) {
|
||||||
|
return Help::toJson($data);
|
||||||
|
} else if (strpos($contentType, 'xml') !== false) {
|
||||||
|
return Help::toXml($data);
|
||||||
|
} else {
|
||||||
|
return http_build_query($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function settings()
|
||||||
|
{
|
||||||
|
$sslCert = $this->getSslCertFile();
|
||||||
|
$sslKey = $this->getSslKeyFile();
|
||||||
|
$sslCa = $this->getCa();
|
||||||
|
|
||||||
|
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$params['ssl_host_name'] = $this->host;
|
||||||
|
$params['ssl_cert_file'] = $this->getSslCertFile();
|
||||||
|
$params['ssl_key_file'] = $this->getSslKeyFile();
|
||||||
|
$params['ssl_verify_peer'] = true;
|
||||||
|
$params['ssl_cafile'] = $sslCa;
|
||||||
|
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
*/
|
||||||
|
private function curl($url, $data = [])
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$output = $this->curlParse($url, $this->mergeParams($data));
|
||||||
|
if ($output === FALSE) {
|
||||||
|
return new Result(['code' => 500, 'message' => $output]);
|
||||||
|
}
|
||||||
|
[$header, $body, $status] = $this->explode($output);
|
||||||
|
if ($status != 200 && $status != 201) {
|
||||||
|
$data = new Result(['code' => $status, 'message' => $body, 'header' => $header]);
|
||||||
|
} else {
|
||||||
|
$data = $this->structure($body, $data, $header);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
$response['code'] = 500;
|
||||||
|
$response['message'] = $exception->getMessage();
|
||||||
|
$response['data'] = array_slice($exception->getTrace(), 0, 6);
|
||||||
|
$response['header'] = [];
|
||||||
|
return new Result($response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return bool|string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function curlParse($url, $data)
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data));
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
|
||||||
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||||
|
|
||||||
|
if ($headers = $this->parseHeaderMat()) {
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->agent)) {
|
||||||
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
|
||||||
|
curl_setopt($ch, CURLOPT_SSLCERT, $this->getSslCertFile());
|
||||||
|
curl_setopt($ch, CURLOPT_SSLKEY, $this->getSslKeyFile());
|
||||||
|
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
|
||||||
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
|
||||||
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
|
||||||
|
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
|
||||||
|
|
||||||
|
if ($this->method == self::POST) {
|
||||||
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->method != self::GET) {
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method));
|
||||||
|
$output = curl_exec($ch);
|
||||||
|
if ($output === false) {
|
||||||
|
throw new Exception(curl_error($ch));
|
||||||
|
}
|
||||||
|
curl_close($ch);
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $output
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function explode($output)
|
||||||
|
{
|
||||||
|
[$header, $body] = explode("\r\n\r\n", $output, 2);
|
||||||
|
if ($header == 'HTTP/1.1 100 Continue') {
|
||||||
|
[$header, $body] = explode("\r\n\r\n", $body, 2);
|
||||||
|
} else if (strpos($body, "\r\n\r\n") !== false) {
|
||||||
|
[$header, $body] = explode("\r\n\r\n", $body, 2);
|
||||||
|
}
|
||||||
|
$header = explode("\r\n", $header);
|
||||||
|
|
||||||
|
unset($output);
|
||||||
|
|
||||||
|
$status = (int)explode(' ', trim($header[0]))[1];
|
||||||
|
$header = $this->headerFormat($header);
|
||||||
|
|
||||||
|
return [$header, $this->resolve($header, $body), $status];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function createRequestUrl($url, $data)
|
||||||
|
{
|
||||||
|
if ($this->isGet()) {
|
||||||
|
return $url . '?' . $data;
|
||||||
|
}
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @param $body
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function resolve($data, $body)
|
||||||
|
{
|
||||||
|
if (is_array($body)) {
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
$type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html';
|
||||||
|
if (strpos($type, 'text/html') !== false) {
|
||||||
|
return $body;
|
||||||
|
} else if (strpos($type, 'json') !== false) {
|
||||||
|
return json_decode($body, true);
|
||||||
|
} else if (strpos($type, 'xml') !== false) {
|
||||||
|
return Help::xmlToArray($body);
|
||||||
|
} else if (strpos($type, 'plain') !== false) {
|
||||||
|
return Help::toArray($body);
|
||||||
|
}
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $headers
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function headerFormat($headers)
|
||||||
|
{
|
||||||
|
$_tmp = [];
|
||||||
|
foreach ($headers as $key => $val) {
|
||||||
|
$trim = explode(': ', trim($val));
|
||||||
|
|
||||||
|
$_tmp[strtolower($trim[0])] = $trim[1] ?? '';
|
||||||
|
}
|
||||||
|
return $_tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $body
|
||||||
|
* @param $_data
|
||||||
|
* @param $header
|
||||||
|
* @param $statusCode
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* 构建返回体
|
||||||
|
*/
|
||||||
|
private function structure($body, $_data, $header = [], $statusCode = 200)
|
||||||
|
{
|
||||||
|
$this->setIsSSL(false);
|
||||||
|
$this->setHeaders([]);
|
||||||
|
|
||||||
|
if ($this->callback !== NULL) {
|
||||||
|
$result = call_user_func($this->callback, $body, $_data, $header);
|
||||||
|
$this->setCallback(null);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
if (is_string($body)) {
|
||||||
|
$result['code'] = 0;
|
||||||
|
$result['message'] = '';
|
||||||
|
} else {
|
||||||
|
$result['code'] = $body[$this->errorCodeField] ?? 0;
|
||||||
|
$result['message'] = $this->searchMessageByData($body);
|
||||||
|
}
|
||||||
|
$result['data'] = $body;
|
||||||
|
$result['header'] = $header;
|
||||||
|
$result['httpStatus'] = $statusCode;
|
||||||
|
|
||||||
|
|
||||||
|
return new Result($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $body
|
||||||
|
* @return array|mixed|string
|
||||||
|
*/
|
||||||
|
private function searchMessageByData($body)
|
||||||
|
{
|
||||||
|
$parent = [];
|
||||||
|
if (empty($this->errorMsgField)) {
|
||||||
|
return 'system success.';
|
||||||
|
}
|
||||||
|
$explode = explode('.', $this->errorMsgField);
|
||||||
|
if (!isset($body[$explode[0]])) {
|
||||||
|
return 'system success.';
|
||||||
|
}
|
||||||
|
foreach ($explode as $item) {
|
||||||
|
if (empty($item)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (empty($parent)) {
|
||||||
|
$parent = $body[$item];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (is_string($parent) || !isset($parent[$item])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$parent = $parent[$item];
|
||||||
|
}
|
||||||
|
return !empty($parent) ? $parent : 'system success.';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* check isPost Request
|
||||||
|
*/
|
||||||
|
public function isPost()
|
||||||
|
{
|
||||||
|
return strtolower($this->method) === self::POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* check isGet Request
|
||||||
|
*/
|
||||||
|
public function isGet()
|
||||||
|
{
|
||||||
|
return strtolower($this->method) === self::GET;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $arr
|
||||||
|
*
|
||||||
|
* @return array|string
|
||||||
|
* 将请求参数进行编码
|
||||||
|
*/
|
||||||
|
private function paramEncode($arr)
|
||||||
|
{
|
||||||
|
if (!is_array($arr)) {
|
||||||
|
return $arr;
|
||||||
|
}
|
||||||
|
$_tmp = [];
|
||||||
|
foreach ($arr as $Key => $val) {
|
||||||
|
$_tmp[$Key] = $val;
|
||||||
|
}
|
||||||
|
if ($this->isGet()) {
|
||||||
|
return http_build_query($_tmp);
|
||||||
|
}
|
||||||
|
return $_tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function post($url, $data = [])
|
||||||
|
{
|
||||||
|
$this->setMethod(self::POST);
|
||||||
|
return $this->request($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function put($url, $data = [])
|
||||||
|
{
|
||||||
|
$this->setMethod(self::PUT);
|
||||||
|
return $this->request($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function get($url, $data = [])
|
||||||
|
{
|
||||||
|
$this->setMethod(self::GET);
|
||||||
|
return $this->request($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function option($url, $data = [])
|
||||||
|
{
|
||||||
|
$this->setMethod(self::OPTIONS);
|
||||||
|
return $this->request($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function delete($url, $data = [])
|
||||||
|
{
|
||||||
|
$this->setMethod(self::DELETE);
|
||||||
|
return $this->request($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param array $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function send($url, $data = [])
|
||||||
|
{
|
||||||
|
return $this->request($url, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function parseHeaderMat()
|
||||||
|
{
|
||||||
|
if ($this->use_swoole) {
|
||||||
|
return $this->header;
|
||||||
|
}
|
||||||
|
$headers = [];
|
||||||
|
foreach ($this->header as $key => $val) {
|
||||||
|
$header = $key . ':' . $val;
|
||||||
|
if (in_array($header, $headers)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$headers[] = $header;
|
||||||
|
}
|
||||||
|
$this->header = [];
|
||||||
|
return $headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $headers
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function setHeaders(array $headers)
|
||||||
|
{
|
||||||
|
if (empty($headers)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
foreach ($headers as $key => $val) {
|
||||||
|
$this->header[$key] = $val;
|
||||||
|
}
|
||||||
|
return $this->header;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,9 +6,11 @@
|
|||||||
* Time: 10:23
|
* Time: 10:23
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\common;
|
||||||
|
|
||||||
abstract class Miniprogarampage
|
use wchat\common\Config;
|
||||||
|
|
||||||
|
abstract class Miniprogarampage implements Progaram
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var Config */
|
/** @var Config */
|
||||||
@@ -17,16 +19,60 @@ abstract class Miniprogarampage
|
|||||||
/** @var Miniprogarampage $instance */
|
/** @var Miniprogarampage $instance */
|
||||||
protected static $instance = null;
|
protected static $instance = null;
|
||||||
|
|
||||||
/** @var WxClient */
|
/** @var HttpClient */
|
||||||
protected $request = null;
|
protected $request = null;
|
||||||
|
|
||||||
|
protected $errorCode = 0;
|
||||||
|
protected $errorMsg = '';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $message
|
||||||
|
* @param int $code
|
||||||
|
* @return Result
|
||||||
|
*/
|
||||||
|
protected function sendError($message, $code = 500)
|
||||||
|
{
|
||||||
|
return new Result(['code' => $code, 'message' => $message]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $code
|
||||||
|
*/
|
||||||
|
public function setErrorCode($code)
|
||||||
|
{
|
||||||
|
$this->errorCode = $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $message
|
||||||
|
*/
|
||||||
|
public function setErrorMessage($message)
|
||||||
|
{
|
||||||
|
$this->errorMsg = $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getErrorCode()
|
||||||
|
{
|
||||||
|
return $this->errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getErrorMessage()
|
||||||
|
{
|
||||||
|
return $this->errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Miniprogarampage constructor.
|
* Miniprogarampage constructor.
|
||||||
*/
|
*/
|
||||||
private function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->request = WxClient::getInstance();
|
|
||||||
$this->request->setIsSSL(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +85,9 @@ abstract class Miniprogarampage
|
|||||||
static::$instance = new static();
|
static::$instance = new static();
|
||||||
}
|
}
|
||||||
static::$instance->config = $config;
|
static::$instance->config = $config;
|
||||||
|
static::$instance->request = HttpClient::NewRequest();
|
||||||
static::$instance->request->setAgent($config->getAgent());
|
static::$instance->request->setAgent($config->getAgent());
|
||||||
|
static::$instance->request->setUseSwoole($config->isUsrSwoole());
|
||||||
|
|
||||||
$request = static::$instance->request;
|
$request = static::$instance->request;
|
||||||
if ($request->getIsSSL()) {
|
if ($request->getIsSSL()) {
|
||||||
@@ -49,6 +97,39 @@ abstract class Miniprogarampage
|
|||||||
return static::$instance;
|
return static::$instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function initConfig($config)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$this->request = HttpClient::NewRequest();
|
||||||
|
$this->request->setIsSSL(true);
|
||||||
|
$this->request->setData(null);
|
||||||
|
$this->request->setUseSwoole($config->isUsrSwoole());
|
||||||
|
if (!empty($config->getAgent())) {
|
||||||
|
$this->request->setAgent($config->getAgent());
|
||||||
|
}
|
||||||
|
if (!empty($config->getSslCert())) {
|
||||||
|
$this->request->setSslCertFile($config->getSslCert());
|
||||||
|
}
|
||||||
|
if (!empty($config->getSslKey())) {
|
||||||
|
$this->request->setSslKeyFile($config->getSslKey());
|
||||||
|
}
|
||||||
|
if (!empty($config->getSslCa())) {
|
||||||
|
$this->request->setCa($config->getSslCa());
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \wchat\common\Config
|
||||||
|
*/
|
||||||
|
public function getConfig()
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool|mixed|string
|
* @return bool|mixed|string
|
||||||
@@ -60,7 +141,7 @@ abstract class Miniprogarampage
|
|||||||
if (!empty($access)) {
|
if (!empty($access)) {
|
||||||
return $access;
|
return $access;
|
||||||
}
|
}
|
||||||
$this->request->setMethod(WxClient::GET);
|
$this->request->setMethod(HttpClient::GET);
|
||||||
$data = $this->request->get('/cgi-bin/token', [
|
$data = $this->request->get('/cgi-bin/token', [
|
||||||
'grant_type' => 'client_credential',
|
'grant_type' => 'client_credential',
|
||||||
'appid' => $this->config->getAppid(),
|
'appid' => $this->config->getAppid(),
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\common;
|
||||||
|
|
||||||
|
|
||||||
|
interface Progaram
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\common;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Result
|
* Class Result
|
||||||
@@ -19,6 +19,9 @@ class Result
|
|||||||
public $count = 0;
|
public $count = 0;
|
||||||
public $data;
|
public $data;
|
||||||
public $header;
|
public $header;
|
||||||
|
private $startTime;
|
||||||
|
private $requestTime;
|
||||||
|
private $runTime;
|
||||||
|
|
||||||
public function __construct(array $data)
|
public function __construct(array $data)
|
||||||
{
|
{
|
||||||
@@ -38,6 +41,9 @@ class Result
|
|||||||
private function reloadResponse($header)
|
private function reloadResponse($header)
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
|
if (is_array($header)) {
|
||||||
|
return $header;
|
||||||
|
}
|
||||||
$load = explode("\n", $header);
|
$load = explode("\n", $header);
|
||||||
if (!empty($load) && is_array($load)) {
|
if (!empty($load) && is_array($load)) {
|
||||||
foreach ($load as $key => $val) {
|
foreach ($load as $key => $val) {
|
||||||
@@ -64,12 +70,15 @@ class Result
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getTime()
|
public function getTime()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'startTime' => $this->startTime,
|
'startTime' => $this->startTime,
|
||||||
'requestTime' => $this->requestTime,
|
'requestTime' => $this->requestTime,
|
||||||
'runTime' => $this->runTime,
|
'runTime' => $this->runTime,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +97,9 @@ class Result
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function isResultsOK()
|
public function isResultsOK()
|
||||||
{
|
{
|
||||||
if (!is_numeric($this->code)) {
|
if (!is_numeric($this->code)) {
|
||||||
@@ -125,7 +136,7 @@ class Result
|
|||||||
public function getData($name = '')
|
public function getData($name = '')
|
||||||
{
|
{
|
||||||
if (!$this->isResultsOK()) {
|
if (!$this->isResultsOK()) {
|
||||||
return '';
|
return $this->data;
|
||||||
}
|
}
|
||||||
if (!empty($name) && isset($this->data[$name])) {
|
if (!empty($name) && isset($this->data[$name])) {
|
||||||
return $this->data[$name];
|
return $this->data[$name];
|
||||||
@@ -140,15 +151,24 @@ class Result
|
|||||||
*/
|
*/
|
||||||
public function append($key, $data)
|
public function append($key, $data)
|
||||||
{
|
{
|
||||||
|
if (!is_array($this->data)) {
|
||||||
|
$this->data = ['origin' => $this->data];
|
||||||
|
}
|
||||||
$this->data[$key] = $data;
|
$this->data[$key] = $data;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function getMessage()
|
public function getMessage()
|
||||||
{
|
{
|
||||||
return $this->message;
|
return $this->message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function getCode()
|
public function getCode()
|
||||||
{
|
{
|
||||||
return $this->code;
|
return $this->code;
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
|
||||||
|
class AccessToken extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Miniprogarampage;
|
||||||
|
|
||||||
|
abstract class AfficialAccount extends Miniprogarampage
|
||||||
|
{
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
|
private $url = 'https://api.weixin.qq.com/cgi-bin/token?';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function generateAccess_token()
|
||||||
|
{
|
||||||
|
if (!empty($this->config->getToken())) {
|
||||||
|
return $this->config->getToken();
|
||||||
|
}
|
||||||
|
$requestParam['grant_type'] = 'client_credential';
|
||||||
|
$requestParam['appid'] = $this->config->getAppid();
|
||||||
|
$requestParam['secret'] = $this->config->getAppsecret();
|
||||||
|
|
||||||
|
$result = HttpClient::NewRequest()->get($this->url, $requestParam);
|
||||||
|
if (!$result->isResultsOK()) {
|
||||||
|
throw new \Exception($result->getMessage(), $result->getCode());
|
||||||
|
}
|
||||||
|
return $result->getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\officialaccount\dcaler\SnsInfo;
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\officialaccount\dcaler\Authorization as DAuth;
|
||||||
|
|
||||||
|
class Authorization extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
private $url = 'https://api.weixin.qq.com/cgi-bin/user/info';
|
||||||
|
|
||||||
|
private $turn = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token=';
|
||||||
|
|
||||||
|
private $oauth2 = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
||||||
|
private $snsUserInfo = 'https://api.weixin.qq.com/sns/userinfo';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $code
|
||||||
|
* @return array|mixed|\wchat\Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function authorization_user_info($code)
|
||||||
|
{
|
||||||
|
$param = $this->GetAccessTokenByCode($code);
|
||||||
|
if (!$param->isResultsOK()) {
|
||||||
|
throw new \Exception($param->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$requestParam['access_token'] = $param->getData('access_token');
|
||||||
|
$requestParam['openid'] = $param->getData('openid');
|
||||||
|
$requestParam['lang'] = 'zh_CN';
|
||||||
|
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
$result = $client->get($this->snsUserInfo, $requestParam);
|
||||||
|
if ($result->isResultsOK()) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
$userInfo = SnsInfo::instance($result->getData());
|
||||||
|
$result->append('instance', $userInfo);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $code
|
||||||
|
* @return array|mixed|\wchat\Result
|
||||||
|
*/
|
||||||
|
private function GetAccessTokenByCode($code)
|
||||||
|
{
|
||||||
|
$requestParam['appid'] = $this->config->getAppid();
|
||||||
|
$requestParam['secret'] = $this->config->getAppsecret();
|
||||||
|
$requestParam['code'] = $code;
|
||||||
|
$requestParam['grant_type'] = 'authorization_code';
|
||||||
|
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
return $client->get($this->oauth2, $requestParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $openid
|
||||||
|
* @return DAuth
|
||||||
|
* @throws \Exception
|
||||||
|
* 获取用户信息
|
||||||
|
*/
|
||||||
|
public function To_grant_authorization($openid)
|
||||||
|
{
|
||||||
|
$requestParam['access_token'] = $this->config->getAccessToken();
|
||||||
|
$requestParam['openid'] = $openid;
|
||||||
|
$requestParam['lang'] = 'zh_CN';
|
||||||
|
|
||||||
|
$authorization = $this->request->get($this->url, $requestParam);
|
||||||
|
if (!$authorization) {
|
||||||
|
throw new \Exception($authorization->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return DAuth::instance($authorization->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $long_url
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function Turn_to_short_chain($long_url)
|
||||||
|
{
|
||||||
|
$token = $this->config->getAccessToken();
|
||||||
|
$requestParam['action'] = 'long2short';
|
||||||
|
$requestParam['long_url'] = $long_url;
|
||||||
|
|
||||||
|
$result = $this->request->post($this->turn . $token, $requestParam);
|
||||||
|
if (!$result->isResultsOK()) {
|
||||||
|
throw new \Exception($result->getMessage());
|
||||||
|
}
|
||||||
|
return $result->getData('short_url');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\officialaccount\dcaler\Prpcrypt;
|
||||||
|
use wchat\officialaccount\dcaler\XMLParse;
|
||||||
|
|
||||||
|
class NewsManager extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
const OK = 0;
|
||||||
|
const ValidateSignatureError = -40001;
|
||||||
|
const ParseXmlError = -40002;
|
||||||
|
const ComputeSignatureError = -40003;
|
||||||
|
const IllegalAesKey = -40004;
|
||||||
|
const ValidateAppidError = -40005;
|
||||||
|
const EncryptAESError = -40006;
|
||||||
|
const DecryptAESError = -40007;
|
||||||
|
const IllegalBuffer = -40008;
|
||||||
|
const EncodeBase64Error = -40009;
|
||||||
|
const DecodeBase64Error = -40010;
|
||||||
|
const GenReturnXmlError = -40011;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $code
|
||||||
|
* @return mixed|string
|
||||||
|
*/
|
||||||
|
public function toError($code)
|
||||||
|
{
|
||||||
|
$messages = [
|
||||||
|
self::ValidateSignatureError => '签名验证错误',
|
||||||
|
self::ParseXmlError => 'xml解析失败',
|
||||||
|
self::ComputeSignatureError => 'sha加密生成签名失败',
|
||||||
|
self::IllegalAesKey => 'encodingAesKey 非法',
|
||||||
|
self::ValidateAppidError => 'appid 校验错误',
|
||||||
|
self::EncryptAESError => 'aes 加密失败',
|
||||||
|
self::DecryptAESError => 'aes 解密失败',
|
||||||
|
self::IllegalBuffer => '解密后得到的buffer非法',
|
||||||
|
self::EncodeBase64Error => 'base64加密失败',
|
||||||
|
self::DecodeBase64Error => 'base64解密失败',
|
||||||
|
self::GenReturnXmlError => '生成xml失败',
|
||||||
|
];
|
||||||
|
return $messages[$code] ?? 'OK';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将公众平台回复用户的消息加密打包.
|
||||||
|
* <ol>
|
||||||
|
* <li>对要发送的消息进行AES-CBC加密</li>
|
||||||
|
* <li>生成安全签名</li>
|
||||||
|
* <li>将消息密文和安全签名打包成xml格式</li>
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* @param $replyMsg string 公众平台待回复用户的消息,xml格式的字符串
|
||||||
|
* @param $timeStamp string 时间戳,可以自己生成,也可以用URL参数的timestamp
|
||||||
|
* @param $nonce string 随机串,可以自己生成,也可以用URL参数的nonce
|
||||||
|
* @param &$encryptMsg string 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串,
|
||||||
|
* 当return返回0时有效
|
||||||
|
*
|
||||||
|
* @return int 成功0,失败返回对应的错误码
|
||||||
|
*/
|
||||||
|
public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg)
|
||||||
|
{
|
||||||
|
$pc = new Prpcrypt($this->config->getEncodingAesKey());
|
||||||
|
|
||||||
|
//加密
|
||||||
|
$array = $pc->encrypt($replyMsg, $this->config->getAppid());
|
||||||
|
$ret = $array[0];
|
||||||
|
if ($ret != 0) {
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($timeStamp == null) {
|
||||||
|
$timeStamp = time();
|
||||||
|
}
|
||||||
|
$encrypt = $array[1];
|
||||||
|
|
||||||
|
//生成安全签名
|
||||||
|
$array = $this->getSHA1($this->config->getToken(), $timeStamp, $nonce, $encrypt);
|
||||||
|
$ret = $array[0];
|
||||||
|
if ($ret != 0) {
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
$signature = $array[1];
|
||||||
|
|
||||||
|
//生成发送的xml
|
||||||
|
$xmlparse = new XMLParse;
|
||||||
|
$encryptMsg = $xmlparse->generate($encrypt, $signature, $timeStamp, $nonce);
|
||||||
|
return self::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检验消息的真实性,并且获取解密后的明文.
|
||||||
|
* <ol>
|
||||||
|
* <li>利用收到的密文生成安全签名,进行签名验证</li>
|
||||||
|
* <li>若验证通过,则提取xml中的加密消息</li>
|
||||||
|
* <li>对消息进行解密</li>
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* @param $msgSignature string 签名串,对应URL参数的msg_signature
|
||||||
|
* @param $timestamp string 时间戳 对应URL参数的timestamp
|
||||||
|
* @param $nonce string 随机串,对应URL参数的nonce
|
||||||
|
* @param $postData string 密文,对应POST请求的数据
|
||||||
|
* @param &$msg string 解密后的原文,当return返回0时有效
|
||||||
|
*
|
||||||
|
* @return int 成功0,失败返回对应的错误码
|
||||||
|
*/
|
||||||
|
public function decryptMsg($msgSignature, $timestamp, $nonce, $postData, &$msg)
|
||||||
|
{
|
||||||
|
if (strlen($this->config->getEncodingAesKey()) != 43) {
|
||||||
|
return self::IllegalAesKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pc = new Prpcrypt($this->config->getEncodingAesKey());
|
||||||
|
|
||||||
|
//提取密文
|
||||||
|
$xmlparse = new XMLParse();
|
||||||
|
$array = $xmlparse->extract($postData);
|
||||||
|
$ret = $array[0];
|
||||||
|
|
||||||
|
if ($ret != 0) {
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($timestamp == null) {
|
||||||
|
$timestamp = time();
|
||||||
|
}
|
||||||
|
|
||||||
|
$encrypt = $array[1];
|
||||||
|
$touser_name = $array[2];
|
||||||
|
|
||||||
|
//验证安全签名
|
||||||
|
$array = $this->getSHA1($this->config->getToken(), $timestamp, $nonce, $encrypt);
|
||||||
|
$ret = $array[0];
|
||||||
|
|
||||||
|
if ($ret != 0) {
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
$signature = $array[1];
|
||||||
|
if ($signature != $msgSignature) {
|
||||||
|
return self::ValidateSignatureError;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $pc->decrypt($encrypt, $this->config->getAppid());
|
||||||
|
if ($result[0] != 0) {
|
||||||
|
return $result[0];
|
||||||
|
}
|
||||||
|
$msg = $result[1];
|
||||||
|
|
||||||
|
return self::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $token
|
||||||
|
* @param $timestamp
|
||||||
|
* @param $nonce
|
||||||
|
* @param $encrypt_msg
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getSHA1($token, $timestamp, $nonce, $encrypt_msg)
|
||||||
|
{
|
||||||
|
//排序
|
||||||
|
try {
|
||||||
|
$array = array($encrypt_msg, $token, $timestamp, $nonce);
|
||||||
|
sort($array, SORT_STRING);
|
||||||
|
$str = implode($array);
|
||||||
|
return array(self::OK, sha1($str));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
//print $e . "\n";
|
||||||
|
return array(self::ComputeSignatureError, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class QrCode extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
private $tick_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
||||||
|
private $temporary_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
|
||||||
|
private $permanent_qr_code_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $scene_id
|
||||||
|
* @return mixed
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function temporaryQrCode($scene_id)
|
||||||
|
{
|
||||||
|
$requestParam['expire_seconds'] = 24 * 3600 * 30;
|
||||||
|
$requestParam['action_name'] = 'QR_SCENE';
|
||||||
|
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
||||||
|
|
||||||
|
$tick = $this->get_tick($requestParam);
|
||||||
|
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
return $client->get($this->temporary_url, ['ticket' => $tick]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $scene_id
|
||||||
|
* @return array|mixed|\common\Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function permanent_qr_code($scene_id)
|
||||||
|
{
|
||||||
|
$requestParam['action_name'] = 'QR_LIMIT_SCENE';
|
||||||
|
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
||||||
|
|
||||||
|
$tick = $this->get_tick($requestParam);
|
||||||
|
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
return $client->get($this->permanent_qr_code_url, ['ticket' => $tick]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $requestParam
|
||||||
|
* @return mixed
|
||||||
|
* @throws \Exception
|
||||||
|
* tick
|
||||||
|
*/
|
||||||
|
private function get_tick($requestParam)
|
||||||
|
{
|
||||||
|
$request = HttpClient::NewRequest();
|
||||||
|
$request->setCallback(function ($body) {
|
||||||
|
if (is_array($body)) {
|
||||||
|
$param = ['code' => $body['errcode'], 'message' => $body['errmsg'], 'data' => $body];
|
||||||
|
} else {
|
||||||
|
$param = ['code' => 0, 'data' => json_decode($body, true)];
|
||||||
|
}
|
||||||
|
return new Result($param);
|
||||||
|
});
|
||||||
|
$data = $request->post($this->tick_url . $this->config->getAccessToken(), json_encode($requestParam));
|
||||||
|
if (!$data->isResultsOK()) {
|
||||||
|
throw new \Exception($data->getMessage());
|
||||||
|
}
|
||||||
|
return $data->getData('ticket');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class SourceMaterial extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
private $uploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/upload?';
|
||||||
|
private $getUploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/get';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $file
|
||||||
|
* @return bool|mixed|Result
|
||||||
|
*/
|
||||||
|
public function ImageUpload($file)
|
||||||
|
{
|
||||||
|
if (!file_exists($file)) {
|
||||||
|
return $this->sendError('文件不存在.', 404);
|
||||||
|
}
|
||||||
|
return $this->upload($file, 'image');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $file
|
||||||
|
* @return bool|mixed|Result
|
||||||
|
*/
|
||||||
|
public function VoiceUpload($file)
|
||||||
|
{
|
||||||
|
if (!file_exists($file)) {
|
||||||
|
return $this->sendError('文件不存在.', 404);
|
||||||
|
}
|
||||||
|
return $this->upload($file, 'voice');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $file
|
||||||
|
* @return bool|mixed|Result
|
||||||
|
*/
|
||||||
|
public function VideoUpload($file)
|
||||||
|
{
|
||||||
|
if (!file_exists($file)) {
|
||||||
|
return $this->sendError('文件不存在.', 404);
|
||||||
|
}
|
||||||
|
return $this->upload($file, 'video');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $file
|
||||||
|
* @return bool|mixed|Result
|
||||||
|
*/
|
||||||
|
public function ThumbUpload($file)
|
||||||
|
{
|
||||||
|
if (!file_exists($file)) {
|
||||||
|
return $this->sendError('文件不存在.', 404);
|
||||||
|
}
|
||||||
|
return $this->upload($file, 'thumb');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $media_id
|
||||||
|
* @return array|mixed|Result
|
||||||
|
*/
|
||||||
|
public function MediaGet($media_id)
|
||||||
|
{
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
|
||||||
|
$accessToken = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
return $client->get($this->getUploadUrl, ['access_token' => $accessToken, 'media_id' => $media_id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $file
|
||||||
|
* @param $type
|
||||||
|
* @return bool|mixed
|
||||||
|
*/
|
||||||
|
private function upload($file, $type)
|
||||||
|
{
|
||||||
|
$uploadInfo['media'] = new \CURLFile($file);
|
||||||
|
$uploadInfo['form-data[filename]'] = $uploadInfo['media']->getFilename();
|
||||||
|
$uploadInfo['form-data[Content-Type]'] = $uploadInfo['media']->getMimeType();
|
||||||
|
|
||||||
|
$accessToken = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
$url = $this->uploadUrl . 'access_token=' . $accessToken . '&type=' . $type;
|
||||||
|
$result = $this->request->post($url, $uploadInfo);
|
||||||
|
if (!$result->isResultsOK()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $result->getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
|
||||||
|
class Subscribe extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
private $openid = '';
|
||||||
|
private $template_id = '';
|
||||||
|
private $url = '';
|
||||||
|
private $miniprogram = [];
|
||||||
|
private $scene = '';
|
||||||
|
private $title = '';
|
||||||
|
private $keywords = [];
|
||||||
|
|
||||||
|
private $pushUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $openid
|
||||||
|
* @return Subscribe
|
||||||
|
*/
|
||||||
|
public function setOpenid(string $openid): Subscribe
|
||||||
|
{
|
||||||
|
$this->openid = $openid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $template_id
|
||||||
|
* @return Subscribe
|
||||||
|
*/
|
||||||
|
public function setTemplateId(string $template_id): Subscribe
|
||||||
|
{
|
||||||
|
$this->template_id = $template_id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @return Subscribe
|
||||||
|
*/
|
||||||
|
public function setUrl(string $url): Subscribe
|
||||||
|
{
|
||||||
|
$this->url = $url;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $appid
|
||||||
|
* @param string $path
|
||||||
|
* @return Subscribe
|
||||||
|
*/
|
||||||
|
public function setMiniprogram(string $appid, string $path): Subscribe
|
||||||
|
{
|
||||||
|
$this->miniprogram['appid'] = $appid;
|
||||||
|
$this->miniprogram['pagepath'] = $path;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $scene
|
||||||
|
* @return Subscribe
|
||||||
|
*/
|
||||||
|
public function setScene(string $scene): Subscribe
|
||||||
|
{
|
||||||
|
$this->scene = $scene;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $title
|
||||||
|
* @return Subscribe
|
||||||
|
*/
|
||||||
|
public function setTitle(string $title): Subscribe
|
||||||
|
{
|
||||||
|
$this->title = $title;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $keyword
|
||||||
|
* @param $content
|
||||||
|
* @param null $color
|
||||||
|
*/
|
||||||
|
public function addContent($keyword, $content, $color = null)
|
||||||
|
{
|
||||||
|
$param['value'] = $content;
|
||||||
|
if (!empty($color)) {
|
||||||
|
$param['color'] = $color;
|
||||||
|
}
|
||||||
|
$this->keywords[$keyword] = $param;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|mixed|\wchat\common\Result
|
||||||
|
*/
|
||||||
|
public function sendSubscribeMessage()
|
||||||
|
{
|
||||||
|
$requestParam['touser'] = $this->openid;
|
||||||
|
$requestParam['template_id'] = $this->template_id;
|
||||||
|
$requestParam['url'] = $this->url;
|
||||||
|
$requestParam['miniprogram'] = $this->miniprogram;
|
||||||
|
$requestParam['scene'] = $this->scene;
|
||||||
|
$requestParam['title'] = $this->title;
|
||||||
|
$requestParam['data'] = $this->keywords;
|
||||||
|
|
||||||
|
$requestUrl = $this->pushUrl . $this->config->getAccessToken();
|
||||||
|
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
return $client->post($requestUrl, $requestParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,157 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class Tag extends AfficialAccount
|
||||||
|
{
|
||||||
|
|
||||||
|
private $generate_url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=';
|
||||||
|
private $tag_lists = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token=';
|
||||||
|
private $tag_edit = 'https://api.weixin.qq.com/cgi-bin/tags/update?access_token=';
|
||||||
|
private $tag_delete = 'https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=';
|
||||||
|
private $tag_fans_list = 'https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=';
|
||||||
|
private $user_batch_add_tag = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=';
|
||||||
|
private $user_batch_remove_tag = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=';
|
||||||
|
private $user_tag = 'https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $tagName
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function generate($tagName)
|
||||||
|
{
|
||||||
|
$params['tag'] = ['name' => $tagName];
|
||||||
|
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
|
||||||
|
$this->request->setData(json_encode($params, JSON_UNESCAPED_UNICODE));
|
||||||
|
return $this->request->post($this->generate_url . $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param $tagName
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function tag_edit($id, $tagName)
|
||||||
|
{
|
||||||
|
$url['tag[id]'] = $id;
|
||||||
|
$url['tag[name]'] = $tagName;
|
||||||
|
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
|
||||||
|
return $this->request->post($this->tag_edit . $config, $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function tag_delete($id)
|
||||||
|
{
|
||||||
|
$url['tag[id]'] = $id;
|
||||||
|
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->post($this->tag_delete . $config, $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param string[] $openid_list
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function user_batch_add_tag($id, array $openid_list)
|
||||||
|
{
|
||||||
|
$url['tagid'] = $id;
|
||||||
|
$url['openid_list'] = $openid_list;
|
||||||
|
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
$this->request->setData(json_encode($url, JSON_UNESCAPED_UNICODE));
|
||||||
|
return $this->request->post($this->user_batch_add_tag . $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param string[] $openid_list
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function user_batch_remove_tag($id, array $openid_list)
|
||||||
|
{
|
||||||
|
$url['tagid'] = $id;
|
||||||
|
$url['openid_list'] = $openid_list;
|
||||||
|
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->post($this->user_batch_remove_tag . $config, $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function tagList()
|
||||||
|
{
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->get($this->tag_lists . $config, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function tag_fans_list()
|
||||||
|
{
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->get($this->tag_fans_list . $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $openid
|
||||||
|
* @return Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function user_tag($openid)
|
||||||
|
{
|
||||||
|
$config = $this->config->getAccessToken();
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->post($this->user_tag . $config, [
|
||||||
|
'openid' => $openid
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Config;
|
||||||
|
|
||||||
|
class WxSDK
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function authorization(Config $config)
|
||||||
|
{
|
||||||
|
return Authorization::getInstance($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function qrCode(Config $config)
|
||||||
|
{
|
||||||
|
return QrCode::getInstance($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function news(Config $config)
|
||||||
|
{
|
||||||
|
return NewsManager::getInstance($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,360 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace wchat\officialaccount\dcaler;
|
||||||
|
|
||||||
|
|
||||||
|
class Authorization
|
||||||
|
{
|
||||||
|
private $subscribe;
|
||||||
|
private $openid;
|
||||||
|
private $nickname;
|
||||||
|
private $sex;
|
||||||
|
private $language;
|
||||||
|
private $city;
|
||||||
|
private $province;
|
||||||
|
private $country;
|
||||||
|
private $headimgurl;
|
||||||
|
private $subscribe_time;
|
||||||
|
private $unionid;
|
||||||
|
private $remark;
|
||||||
|
private $groupid;
|
||||||
|
private $tagid_list;
|
||||||
|
private $subscribe_scene;
|
||||||
|
private $qr_scene;
|
||||||
|
private $qr_scene_str;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $object
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public static function instance($object)
|
||||||
|
{
|
||||||
|
$class = new Authorization();
|
||||||
|
$class->setSubscribe($object['subscribe']);
|
||||||
|
$class->setOpenid($object['openid']);
|
||||||
|
$class->setNickname($object['nickname']);
|
||||||
|
$class->setSex($object['sex']);
|
||||||
|
$class->setLanguage($object['language']);
|
||||||
|
$class->setCity($object['city']);
|
||||||
|
$class->setProvince($object['province']);
|
||||||
|
$class->setCountry($object['country']);
|
||||||
|
$class->setHeadimgurl($object['headimgurl']);
|
||||||
|
$class->setSubscribeTime($object['subscribe_time']);
|
||||||
|
$class->setUnionid($object['unionid']);
|
||||||
|
$class->setRemark($object['remark']);
|
||||||
|
$class->setGroupid($object['groupid']);
|
||||||
|
$class->setTagidList($object['tagid_list']);
|
||||||
|
$class->setSubscribeScene($object['subscribe_scene']);
|
||||||
|
$class->setQrScene($object['qr_scene']);
|
||||||
|
$class->setQrSceneStr($object['qr_scene_str']);
|
||||||
|
return $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getSubscribe()
|
||||||
|
{
|
||||||
|
return $this->subscribe;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $subscribe
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setSubscribe($subscribe)
|
||||||
|
{
|
||||||
|
$this->subscribe = $subscribe;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getOpenid()
|
||||||
|
{
|
||||||
|
return $this->openid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $openid
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setOpenid($openid)
|
||||||
|
{
|
||||||
|
$this->openid = $openid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getNickname()
|
||||||
|
{
|
||||||
|
return $this->nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $nickname
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setNickname($nickname)
|
||||||
|
{
|
||||||
|
$this->nickname = $nickname;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getSex()
|
||||||
|
{
|
||||||
|
return $this->sex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $sex
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setSex($sex)
|
||||||
|
{
|
||||||
|
$this->sex = $sex;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getLanguage()
|
||||||
|
{
|
||||||
|
return $this->language;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $language
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setLanguage($language)
|
||||||
|
{
|
||||||
|
$this->language = $language;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getCity()
|
||||||
|
{
|
||||||
|
return $this->city;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $city
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setCity($city)
|
||||||
|
{
|
||||||
|
$this->city = $city;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getProvince()
|
||||||
|
{
|
||||||
|
return $this->province;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $province
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setProvince($province)
|
||||||
|
{
|
||||||
|
$this->province = $province;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getCountry()
|
||||||
|
{
|
||||||
|
return $this->country;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $country
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setCountry($country)
|
||||||
|
{
|
||||||
|
$this->country = $country;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getHeadimgurl()
|
||||||
|
{
|
||||||
|
return $this->headimgurl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $headimgurl
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setHeadimgurl($headimgurl)
|
||||||
|
{
|
||||||
|
$this->headimgurl = $headimgurl;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getSubscribeTime()
|
||||||
|
{
|
||||||
|
return $this->subscribe_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $subscribe_time
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setSubscribeTime($subscribe_time)
|
||||||
|
{
|
||||||
|
$this->subscribe_time = $subscribe_time;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getUnionid()
|
||||||
|
{
|
||||||
|
return $this->unionid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $unionid
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setUnionid($unionid)
|
||||||
|
{
|
||||||
|
$this->unionid = $unionid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getRemark()
|
||||||
|
{
|
||||||
|
return $this->remark;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $remark
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setRemark($remark)
|
||||||
|
{
|
||||||
|
$this->remark = $remark;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getGroupid()
|
||||||
|
{
|
||||||
|
return $this->groupid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $groupid
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setGroupid($groupid)
|
||||||
|
{
|
||||||
|
$this->groupid = $groupid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getTagidList()
|
||||||
|
{
|
||||||
|
return $this->tagid_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $tagid_list
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setTagidList($tagid_list)
|
||||||
|
{
|
||||||
|
$this->tagid_list = $tagid_list;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getSubscribeScene()
|
||||||
|
{
|
||||||
|
return $this->subscribe_scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $subscribe_scene
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setSubscribeScene($subscribe_scene)
|
||||||
|
{
|
||||||
|
$this->subscribe_scene = $subscribe_scene;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getQrScene()
|
||||||
|
{
|
||||||
|
return $this->qr_scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $qr_scene
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setQrScene($qr_scene)
|
||||||
|
{
|
||||||
|
$this->qr_scene = $qr_scene;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getQrSceneStr()
|
||||||
|
{
|
||||||
|
return $this->qr_scene_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $qr_scene_str
|
||||||
|
* @return Authorization
|
||||||
|
*/
|
||||||
|
public function setQrSceneStr($qr_scene_str)
|
||||||
|
{
|
||||||
|
$this->qr_scene_str = $qr_scene_str;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount\dcaler;
|
||||||
|
|
||||||
|
|
||||||
|
class PKCS7Encoder
|
||||||
|
{
|
||||||
|
public static $block_size = 32;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $text
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function encode($text)
|
||||||
|
{
|
||||||
|
$block_size = PKCS7Encoder::$block_size;
|
||||||
|
$text_length = strlen($text);
|
||||||
|
//计算需要填充的位数
|
||||||
|
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
|
||||||
|
if ($amount_to_pad == 0) {
|
||||||
|
$amount_to_pad = PKCS7Encoder::$block_size;
|
||||||
|
}
|
||||||
|
//获得补位所用的字符
|
||||||
|
$pad_chr = chr($amount_to_pad);
|
||||||
|
$tmp = "";
|
||||||
|
for ($index = 0; $index < $amount_to_pad; $index++) {
|
||||||
|
$tmp .= $pad_chr;
|
||||||
|
}
|
||||||
|
return $text . $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $text
|
||||||
|
* @return false|string
|
||||||
|
*/
|
||||||
|
function decode($text)
|
||||||
|
{
|
||||||
|
|
||||||
|
$pad = ord(substr($text, -1));
|
||||||
|
if ($pad < 1 || $pad > 32) {
|
||||||
|
$pad = 0;
|
||||||
|
}
|
||||||
|
return substr($text, 0, (strlen($text) - $pad));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount\dcaler;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\officialaccount\NewsManager;
|
||||||
|
|
||||||
|
class Prpcrypt
|
||||||
|
{
|
||||||
|
public $key;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prpcrypt constructor.
|
||||||
|
* @param $k
|
||||||
|
*/
|
||||||
|
public function __construct($k)
|
||||||
|
{
|
||||||
|
$this->key = base64_decode($k . "=");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $text
|
||||||
|
* @param $appid
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function encrypt($text, $appid)
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
//获得16位随机字符串,填充到明文之前
|
||||||
|
$random = $this->getRandomStr();
|
||||||
|
$text = $random . pack("N", strlen($text)) . $text . $appid;
|
||||||
|
// 网络字节序
|
||||||
|
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
||||||
|
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||||
|
$iv = substr($this->key, 0, 16);
|
||||||
|
//使用自定义的填充方式对明文进行补位填充
|
||||||
|
$pkc_encoder = new PKCS7Encoder;
|
||||||
|
$text = $pkc_encoder->encode($text);
|
||||||
|
mcrypt_generic_init($module, $this->key, $iv);
|
||||||
|
//加密
|
||||||
|
$encrypted = mcrypt_generic($module, $text);
|
||||||
|
mcrypt_generic_deinit($module);
|
||||||
|
mcrypt_module_close($module);
|
||||||
|
|
||||||
|
//print(base64_encode($encrypted));
|
||||||
|
//使用BASE64对加密后的字符串进行编码
|
||||||
|
return array(NewsManager::OK, base64_encode($encrypted));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
//print $e;
|
||||||
|
return array(NewsManager::EncryptAESError, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $encrypted
|
||||||
|
* @param $appid
|
||||||
|
* @return array|string
|
||||||
|
*/
|
||||||
|
public function decrypt($encrypted, $appid)
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
//使用BASE64对需要解密的字符串进行解码
|
||||||
|
$ciphertext_dec = base64_decode($encrypted);
|
||||||
|
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||||
|
$iv = substr($this->key, 0, 16);
|
||||||
|
mcrypt_generic_init($module, $this->key, $iv);
|
||||||
|
|
||||||
|
//解密
|
||||||
|
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
||||||
|
mcrypt_generic_deinit($module);
|
||||||
|
mcrypt_module_close($module);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return array(NewsManager::DecryptAESError, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
//去除补位字符
|
||||||
|
$pkc_encoder = new PKCS7Encoder;
|
||||||
|
$result = $pkc_encoder->decode($decrypted);
|
||||||
|
//去除16位随机字符串,网络字节序和AppId
|
||||||
|
if (strlen($result) < 16)
|
||||||
|
return "";
|
||||||
|
$content = substr($result, 16, strlen($result));
|
||||||
|
$len_list = unpack("N", substr($content, 0, 4));
|
||||||
|
$xml_len = $len_list[1];
|
||||||
|
$xml_content = substr($content, 4, $xml_len);
|
||||||
|
$from_appid = substr($content, $xml_len + 4);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
//print $e;
|
||||||
|
return array(NewsManager::IllegalBuffer, null);
|
||||||
|
}
|
||||||
|
if ($from_appid != $appid)
|
||||||
|
return array(NewsManager::ValidateAppidError, null);
|
||||||
|
return array(0, $xml_content);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 随机生成16位字符串
|
||||||
|
* @return string 生成的字符串
|
||||||
|
*/
|
||||||
|
function getRandomStr()
|
||||||
|
{
|
||||||
|
|
||||||
|
$str = "";
|
||||||
|
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
|
$max = strlen($str_pol) - 1;
|
||||||
|
for ($i = 0; $i < 16; $i++) {
|
||||||
|
$str .= $str_pol[mt_rand(0, $max)];
|
||||||
|
}
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount\dcaler;
|
||||||
|
|
||||||
|
|
||||||
|
class SnsInfo
|
||||||
|
{
|
||||||
|
|
||||||
|
private $openid;
|
||||||
|
private $nickname;
|
||||||
|
private $sex;
|
||||||
|
private $province;
|
||||||
|
private $city;
|
||||||
|
private $country;
|
||||||
|
private $headimgurl;
|
||||||
|
private $privilege;
|
||||||
|
private $unionid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getOpenid()
|
||||||
|
{
|
||||||
|
return $this->openid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $openid
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setOpenid($openid)
|
||||||
|
{
|
||||||
|
$this->openid = $openid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getNickname()
|
||||||
|
{
|
||||||
|
return $this->nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $nickname
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setNickname($nickname)
|
||||||
|
{
|
||||||
|
$this->nickname = $nickname;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getSex()
|
||||||
|
{
|
||||||
|
return $this->sex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $sex
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setSex($sex)
|
||||||
|
{
|
||||||
|
$this->sex = $sex;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getProvince()
|
||||||
|
{
|
||||||
|
return $this->province;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $province
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setProvince($province)
|
||||||
|
{
|
||||||
|
$this->province = $province;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getCity()
|
||||||
|
{
|
||||||
|
return $this->city;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $city
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setCity($city)
|
||||||
|
{
|
||||||
|
$this->city = $city;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getCountry()
|
||||||
|
{
|
||||||
|
return $this->country;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $country
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setCountry($country)
|
||||||
|
{
|
||||||
|
$this->country = $country;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getHeadimgurl()
|
||||||
|
{
|
||||||
|
return $this->headimgurl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $headimgurl
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setHeadimgurl($headimgurl)
|
||||||
|
{
|
||||||
|
$this->headimgurl = $headimgurl;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getPrivilege()
|
||||||
|
{
|
||||||
|
return $this->privilege;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $privilege
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setPrivilege($privilege)
|
||||||
|
{
|
||||||
|
$this->privilege = $privilege;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getUnionid()
|
||||||
|
{
|
||||||
|
return $this->unionid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $unionid
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public function setUnionid($unionid)
|
||||||
|
{
|
||||||
|
$this->unionid = $unionid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $object
|
||||||
|
* @return SnsInfo
|
||||||
|
*/
|
||||||
|
public static function instance($object)
|
||||||
|
{
|
||||||
|
$class = new SnsInfo();
|
||||||
|
$class->setOpenid($object['openid']);
|
||||||
|
$class->setNickname($object['nickname']);
|
||||||
|
$class->setSex($object['sex']);
|
||||||
|
$class->setCity($object['city']);
|
||||||
|
$class->setProvince($object['province']);
|
||||||
|
$class->setCountry($object['country']);
|
||||||
|
$class->setHeadimgurl($object['headimgurl']);
|
||||||
|
$class->setUnionid($object['unionid']);
|
||||||
|
$class->setPrivilege($object['privilege']);
|
||||||
|
return $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\officialaccount\dcaler;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\officialaccount\NewsManager;
|
||||||
|
|
||||||
|
class XMLParse
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 提取出xml数据包中的加密消息
|
||||||
|
* @param string $xmltext 待提取的xml字符串
|
||||||
|
* @return array 提取出的加密消息字符串
|
||||||
|
*/
|
||||||
|
public function extract($xmltext)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$xml = new \DOMDocument();
|
||||||
|
$xml->loadXML($xmltext);
|
||||||
|
$array_e = $xml->getElementsByTagName('Encrypt');
|
||||||
|
$array_a = $xml->getElementsByTagName('ToUserName');
|
||||||
|
$encrypt = $array_e->item(0)->nodeValue;
|
||||||
|
$tousername = $array_a->item(0)->nodeValue;
|
||||||
|
return [0, $encrypt, $tousername];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return [NewsManager::ParseXmlError, null, null];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $encrypt
|
||||||
|
* @param $signature
|
||||||
|
* @param $timestamp
|
||||||
|
* @param $nonce
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function generate($encrypt, $signature, $timestamp, $nonce)
|
||||||
|
{
|
||||||
|
$format = "<xml>
|
||||||
|
<Encrypt><![CDATA[%s]]></Encrypt>
|
||||||
|
<MsgSignature><![CDATA[%s]]></MsgSignature>
|
||||||
|
<TimeStamp>%s</TimeStamp>
|
||||||
|
<Nonce><![CDATA[%s]]></Nonce>
|
||||||
|
</xml>";
|
||||||
|
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/19 0019
|
||||||
|
* Time: 16:12
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
use wchat\common\Decode;
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class Account extends SmallProgram
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @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';
|
||||||
|
|
||||||
|
if (empty($code)) {
|
||||||
|
return new Result(['code' => 404, 'message' => '临时登录凭证不能为空.']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->request->setHost('api.q.qq.com');
|
||||||
|
$this->request->setMethod(HttpClient::GET);
|
||||||
|
$this->request->setIsSSL(true);
|
||||||
|
$this->request->setUseSwoole($this->config->isUsrSwoole());
|
||||||
|
|
||||||
|
return $this->request->get('sns/jscode2session', $param);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $encryptedData
|
||||||
|
* @param $iv
|
||||||
|
* @param $sessionKey
|
||||||
|
* @param $asArray
|
||||||
|
* @return object|array
|
||||||
|
* @throws
|
||||||
|
*
|
||||||
|
* * <li>-41001: encodingAesKey 非法</li>
|
||||||
|
* <li>-41003: aes 解密失败</li>
|
||||||
|
* <li>-41004: 解密后得到的buffer非法</li>
|
||||||
|
* <li>-41005: base64加密失败</li>
|
||||||
|
* <li>-41016: base64解密失败</li>
|
||||||
|
*/
|
||||||
|
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
||||||
|
{
|
||||||
|
$decode = new Decode();
|
||||||
|
$decode->setAppId($this->config->getAppid());
|
||||||
|
$decode->setIv($iv);
|
||||||
|
$decode->setEncryptedData($encryptedData);
|
||||||
|
$decode->setSessionKey($sessionKey);
|
||||||
|
return $decode->decode($asArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
class GamePrePay extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
private $url = 'https://api.q.qq.com/api/json/openApiPay/GamePrePay?access_token=';
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace qq;
|
namespace wchat\qq;
|
||||||
|
|
||||||
use wchat\Miniprogarampage;
|
use wchat\common\Help;
|
||||||
use wchat\Help;
|
|
||||||
|
|
||||||
class Notify extends Miniprogarampage
|
class Notify extends SmallProgram
|
||||||
{
|
{
|
||||||
public $appid = '';
|
public $appid = '';
|
||||||
public $mch_id = '';
|
public $mch_id = '';
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class QrCode extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||||
|
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
|
||||||
|
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
||||||
|
|
||||||
|
private $savePath = __DIR__ . '/../../Users/';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
*/
|
||||||
|
public function setSavePath($path)
|
||||||
|
{
|
||||||
|
$this->savePath = $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param $width
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function createwxaqrcode($path, $width)
|
||||||
|
{
|
||||||
|
$url = $this->wxaqr . $this->getAccessToken();
|
||||||
|
|
||||||
|
$sendBody['path'] = $path;
|
||||||
|
$sendBody['width'] = $width;
|
||||||
|
|
||||||
|
$this->request->setMethod(HttpClient::POST);
|
||||||
|
$this->request->setCallback([$this, 'saveByPath']);
|
||||||
|
|
||||||
|
return $this->request->post($url, $sendBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param $width
|
||||||
|
* @param bool $is_hyaline
|
||||||
|
* @param bool $auto_color
|
||||||
|
* @param string $line_color
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function getwxacode($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
||||||
|
{
|
||||||
|
$sendBody['path'] = $path;
|
||||||
|
$sendBody['width'] = $width;
|
||||||
|
$sendBody['auto_color'] = $auto_color;
|
||||||
|
$sendBody['is_hyaline'] = $is_hyaline;
|
||||||
|
if ($auto_color) {
|
||||||
|
$sendBody['line_color'] = $line_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = $this->getwxacode . $this->getAccessToken();
|
||||||
|
|
||||||
|
$this->request->setMethod(HttpClient::POST);
|
||||||
|
$this->request->setCallback([$this, 'saveByPath']);
|
||||||
|
return $this->request->post($url, $sendBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param $width
|
||||||
|
* @param bool $is_hyaline
|
||||||
|
* @param bool $auto_color
|
||||||
|
* @param string $line_color
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function getwxacodeunlimit($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
|
||||||
|
{
|
||||||
|
$sendBody['path'] = $path;
|
||||||
|
$sendBody['width'] = $width;
|
||||||
|
$sendBody['auto_color'] = $auto_color;
|
||||||
|
$sendBody['is_hyaline'] = $is_hyaline;
|
||||||
|
if ($auto_color) {
|
||||||
|
$sendBody['line_color'] = $line_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = $this->getwxacodeunlimit . $this->getAccessToken();
|
||||||
|
|
||||||
|
$this->request->setMethod(HttpClient::POST);
|
||||||
|
$this->request->setCallback([$this, 'saveByPath']);
|
||||||
|
return $this->request->post($url, $sendBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $body
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function saveByPath($body)
|
||||||
|
{
|
||||||
|
if (!is_null($json = json_decode($body))) {
|
||||||
|
throw new \Exception($json['errmsg'], $json['errcode']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$push = md5_file($body) . '.png';
|
||||||
|
file_put_contents($this->savePath . $push, $this->savePath);
|
||||||
|
return $this->savePath . $push;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/3/26 0026
|
||||||
|
* Time: 10:22
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Result;
|
||||||
|
use wchat\common\Help;
|
||||||
|
|
||||||
|
class Recharge extends SmallProgram
|
||||||
|
{
|
||||||
|
private $money = 0;
|
||||||
|
|
||||||
|
private $orderNo;
|
||||||
|
|
||||||
|
private $data = [];
|
||||||
|
|
||||||
|
private $spill_create_ip = '';
|
||||||
|
|
||||||
|
private $uniformer = 'https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setSpillCreateIp(string $value)
|
||||||
|
{
|
||||||
|
$this->spill_create_ip = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
|
||||||
|
$this->request->setCallback(function ($result, $body) {
|
||||||
|
$data = Help::toArray($result);
|
||||||
|
if (isset($data['sign'])) {
|
||||||
|
$sign = $data['sign'];
|
||||||
|
unset($data['sign']);
|
||||||
|
}
|
||||||
|
if (!isset($sign)) {
|
||||||
|
$return['code'] = -1;
|
||||||
|
$return['message'] = '返回数据签名验证失败';
|
||||||
|
} else {
|
||||||
|
$return['code'] = 0;
|
||||||
|
$return['data'] = $data;
|
||||||
|
$return['data']['postBody'] = $body;
|
||||||
|
if ($data['return_code'] == 'FAIL') {
|
||||||
|
$return['code'] = -1;
|
||||||
|
$return['message'] = $data['return_msg'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Result($return);
|
||||||
|
});
|
||||||
|
return $this->send($this->uniformer, $this->builder());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function builder()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'mch_id' => $this->config->getMchId(),
|
||||||
|
'nonce_str' => Help::random(32),
|
||||||
|
'body' => $this->config->getBody(),
|
||||||
|
'out_trade_no' => $this->orderNo,
|
||||||
|
'total_fee' => $this->money,
|
||||||
|
'spbill_create_ip' => $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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return array|mixed|Result
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function send($url, $data)
|
||||||
|
{
|
||||||
|
$this->request->setIsSSL(true);
|
||||||
|
$this->request->setMethod(HttpClient::POST);
|
||||||
|
$this->request->addHeader('Content-Type', 'text/xml');
|
||||||
|
return $this->request->send($url, $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,500 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
use wchat\common\Help;
|
||||||
|
use wchat\common\Result;
|
||||||
|
use qq\result\RedHatResult;
|
||||||
|
|
||||||
|
class Redhat extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
private $charset = 'UTF8';
|
||||||
|
private $nonce_str = '';
|
||||||
|
private $sign = '';
|
||||||
|
private $mch_billno = '';
|
||||||
|
private $mch_id = '';
|
||||||
|
private $mch_name = '';
|
||||||
|
private $qqappid = '';
|
||||||
|
private $re_openid = '';
|
||||||
|
private $total_amount = '';
|
||||||
|
private $total_num = '';
|
||||||
|
private $wishing = '';
|
||||||
|
private $act_name = '';
|
||||||
|
private $icon_id = '';
|
||||||
|
private $banner_id = '';
|
||||||
|
private $notify_url = '';
|
||||||
|
private $not_send_msg = '';
|
||||||
|
private $min_value = '';
|
||||||
|
private $max_value = '';
|
||||||
|
private $key = '';
|
||||||
|
private $signType = '';
|
||||||
|
|
||||||
|
|
||||||
|
private $sendUrl = 'https://api.qpay.qq.com/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||||
|
private $searchUrl = 'https://qpay.qq.com/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getKey(): string
|
||||||
|
{
|
||||||
|
return $this->key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setKey(string $key): Redhat
|
||||||
|
{
|
||||||
|
$this->key = $key;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSignType(): string
|
||||||
|
{
|
||||||
|
return $this->signType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $signType
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setSignType(string $signType): Redhat
|
||||||
|
{
|
||||||
|
$this->signType = $signType;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCharset(): string
|
||||||
|
{
|
||||||
|
return $this->charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $charset
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setCharset(string $charset): Redhat
|
||||||
|
{
|
||||||
|
$this->charset = $charset;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNonceStr(): string
|
||||||
|
{
|
||||||
|
return $this->nonce_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $nonce_str
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setNonceStr(string $nonce_str): Redhat
|
||||||
|
{
|
||||||
|
$this->nonce_str = $nonce_str;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSign(): string
|
||||||
|
{
|
||||||
|
return $this->sign;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sign
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setSign(string $sign): Redhat
|
||||||
|
{
|
||||||
|
$this->sign = $sign;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMchBillno(): string
|
||||||
|
{
|
||||||
|
if (empty($this->mch_billno)) {
|
||||||
|
return '1559179381' . date('Ymd') . mt_rand(11, 99) . date('His') . mt_rand(11, 99);
|
||||||
|
}
|
||||||
|
return $this->mch_billno;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $mch_billno
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setMchBillno(string $mch_billno): Redhat
|
||||||
|
{
|
||||||
|
$this->mch_billno = $mch_billno;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMchId(): string
|
||||||
|
{
|
||||||
|
return $this->mch_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $mch_id
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setMchId(string $mch_id): Redhat
|
||||||
|
{
|
||||||
|
$this->mch_id = $mch_id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMchName(): string
|
||||||
|
{
|
||||||
|
return $this->mch_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $mch_name
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setMchName(string $mch_name): Redhat
|
||||||
|
{
|
||||||
|
$this->mch_name = $mch_name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getQqappid(): string
|
||||||
|
{
|
||||||
|
return $this->qqappid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $qqappid
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setQqappid(string $qqappid): Redhat
|
||||||
|
{
|
||||||
|
$this->qqappid = $qqappid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getReOpenid(): string
|
||||||
|
{
|
||||||
|
return $this->re_openid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $re_openid
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setReOpenid(string $re_openid): Redhat
|
||||||
|
{
|
||||||
|
$this->re_openid = $re_openid;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTotalAmount(): string
|
||||||
|
{
|
||||||
|
return $this->total_amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $total_amount
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setTotalAmount(string $total_amount): Redhat
|
||||||
|
{
|
||||||
|
$this->total_amount = $total_amount;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTotalNum(): string
|
||||||
|
{
|
||||||
|
return $this->total_num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $total_num
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setTotalNum(string $total_num): Redhat
|
||||||
|
{
|
||||||
|
$this->total_num = $total_num;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getWishing(): string
|
||||||
|
{
|
||||||
|
return $this->wishing;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $wishing
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setWishing(string $wishing): Redhat
|
||||||
|
{
|
||||||
|
$this->wishing = $wishing;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getActName(): string
|
||||||
|
{
|
||||||
|
return $this->act_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $act_name
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setActName(string $act_name): Redhat
|
||||||
|
{
|
||||||
|
$this->act_name = $act_name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getIconId(): string
|
||||||
|
{
|
||||||
|
return $this->icon_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $icon_id
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setIconId(string $icon_id): Redhat
|
||||||
|
{
|
||||||
|
$this->icon_id = $icon_id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBannerId(): string
|
||||||
|
{
|
||||||
|
return $this->banner_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $banner_id
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setBannerId(string $banner_id): Redhat
|
||||||
|
{
|
||||||
|
$this->banner_id = $banner_id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNotifyUrl(): string
|
||||||
|
{
|
||||||
|
return $this->notify_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $notify_url
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setNotifyUrl(string $notify_url): Redhat
|
||||||
|
{
|
||||||
|
$this->notify_url = $notify_url;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNotSendMsg(): string
|
||||||
|
{
|
||||||
|
return $this->not_send_msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $not_send_msg
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setNotSendMsg(string $not_send_msg): Redhat
|
||||||
|
{
|
||||||
|
$this->not_send_msg = $not_send_msg;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMinValue(): string
|
||||||
|
{
|
||||||
|
return $this->min_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $min_value
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setMinValue(string $min_value): Redhat
|
||||||
|
{
|
||||||
|
$this->min_value = $min_value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMaxValue(): string
|
||||||
|
{
|
||||||
|
return $this->max_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $max_value
|
||||||
|
* @return Redhat
|
||||||
|
*/
|
||||||
|
public function setMaxValue(string $max_value): Redhat
|
||||||
|
{
|
||||||
|
$this->max_value = $max_value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
* 构建请求参数
|
||||||
|
*/
|
||||||
|
private function generate()
|
||||||
|
{
|
||||||
|
$requestParam['charset'] = $this->getCharset();
|
||||||
|
$requestParam['nonce_str'] = $this->getNonceStr();
|
||||||
|
$requestParam['mch_billno'] = $this->getMchBillno();
|
||||||
|
$requestParam['mch_id'] = $this->getMchId();
|
||||||
|
$requestParam['mch_name'] = $this->getMchName();
|
||||||
|
$requestParam['qqappid'] = $this->getQqappid();
|
||||||
|
$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->getNotifyUrl();
|
||||||
|
|
||||||
|
$requestParam['sign'] = $this->builderSign($requestParam);
|
||||||
|
|
||||||
|
return $requestParam;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $requestParam
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function builderSign($requestParam)
|
||||||
|
{
|
||||||
|
return Help::sign($requestParam, $this->getKey(), $this->getSignType());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws
|
||||||
|
* 发红包
|
||||||
|
*/
|
||||||
|
public function redEnvelopes()
|
||||||
|
{
|
||||||
|
$callback = function ($response) {
|
||||||
|
$json = json_decode($response, true);
|
||||||
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||||
|
$message = $json['return_msg'] ?? $json['retmsg'];
|
||||||
|
$param = ['code' => 500, 'message' => $message];
|
||||||
|
} else if ($json['retcode'] != 0) {
|
||||||
|
$param = ['code' => $json['retcode'], 'message' => $json['retmsg']];
|
||||||
|
} else {
|
||||||
|
$param = ['code' => 0, 'message' => $json['retmsg'], 'data' => ['listid' => $json['listid']]];
|
||||||
|
}
|
||||||
|
return new Result($param);
|
||||||
|
};
|
||||||
|
$this->request->setCallback($callback);
|
||||||
|
$response = $this->request->post($this->sendUrl, $this->generate());
|
||||||
|
if (!$response->isResultsOK()) {
|
||||||
|
throw new \Exception($response->getMessage());
|
||||||
|
}
|
||||||
|
return $response->getData('listid');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $listid
|
||||||
|
* @param null $orderNo
|
||||||
|
* @return RedHatResult
|
||||||
|
* @throws \Exception
|
||||||
|
*
|
||||||
|
* 查询红包状态
|
||||||
|
*/
|
||||||
|
public function searchByOrderNo($listid, $orderNo = null)
|
||||||
|
{
|
||||||
|
$requestParam['nonce_str'] = $this->getNonceStr();
|
||||||
|
$requestParam['mch_id'] = $this->getMchId();
|
||||||
|
$requestParam['listid'] = $listid;
|
||||||
|
if (!empty($orderNo)) {
|
||||||
|
$requestParam['mch_billno'] = $orderNo;
|
||||||
|
}
|
||||||
|
$requestParam['sign'] = $this->builderSign($requestParam);
|
||||||
|
$this->request->setCallback(function ($response) {
|
||||||
|
$response = json_decode($response, true);
|
||||||
|
if (!isset($response['result']) || $response['result'] != 0) {
|
||||||
|
$param = ['code' => 500, 'message' => $response['res_info'] ?? '订单查询失败!', 'data' => $response];
|
||||||
|
} else {
|
||||||
|
$param = ['code' => 0, 'message' => 'system success!', 'data' => $response];
|
||||||
|
}
|
||||||
|
return new Result($param);
|
||||||
|
});
|
||||||
|
$response = $this->request->get($this->searchUrl, $requestParam);
|
||||||
|
if (!$response->isResultsOK()) {
|
||||||
|
throw new \Exception($response->getMessage());
|
||||||
|
}
|
||||||
|
return new RedHatResult($response->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SecCheck
|
||||||
|
* @package qq
|
||||||
|
*/
|
||||||
|
class SecCheck extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
private $_url = 'https://api.q.qq.com/api/json/security/ImgSecCheck?access_token=';
|
||||||
|
|
||||||
|
private $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return array|Result|mixed
|
||||||
|
*/
|
||||||
|
public function image($path = '')
|
||||||
|
{
|
||||||
|
if (!file_exists($path)) {
|
||||||
|
return $this->sendError('文件不存在', 404);
|
||||||
|
}
|
||||||
|
$this->request->setUseSwoole(false);
|
||||||
|
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
||||||
|
$this->request->setErrorField('errCode');
|
||||||
|
$this->request->setErrorMsgField('errMsg');
|
||||||
|
return $this->request->post($this->_url . $this->config->getAccessToken(), [
|
||||||
|
'appid' => $this->config->getAppid(), 'media' => new \CURLFile($path)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $content
|
||||||
|
* @return array|Result|mixed
|
||||||
|
*/
|
||||||
|
public function text($content)
|
||||||
|
{
|
||||||
|
if (empty($content)) {
|
||||||
|
return $this->sendError('文件不存在', 404);
|
||||||
|
}
|
||||||
|
$this->request->setUseSwoole(true);
|
||||||
|
$this->request->addHeader('Content-Type', 'application/json');
|
||||||
|
$this->request->setErrorField('errCode');
|
||||||
|
$this->request->setErrorMsgField('errMsg');
|
||||||
|
$this->request->setData([
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'content' => $content
|
||||||
|
]);
|
||||||
|
$this->request->setHost('api.q.qq.com');
|
||||||
|
$this->request->setIsSSL(true);
|
||||||
|
|
||||||
|
$url = '/' . ltrim($this->_msgUrl, '/') . $this->config->getAccessToken();
|
||||||
|
return $this->request->post($url);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Miniprogarampage;
|
||||||
|
|
||||||
|
class SmallProgram extends Miniprogarampage
|
||||||
|
{
|
||||||
|
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
|
private $url = 'https://api.q.qq.com/api/getToken';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $get_token
|
||||||
|
* @return mixed|null
|
||||||
|
*/
|
||||||
|
public function generateAccess_token($get_token = false)
|
||||||
|
{
|
||||||
|
if (!empty($this->config->getToken())) {
|
||||||
|
return $this->config->getToken();
|
||||||
|
}
|
||||||
|
$query = [
|
||||||
|
'grant_type' => 'client_credential',
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'secret' => $this->config->getAppsecret()
|
||||||
|
];
|
||||||
|
$param = $this->request->get($this->url, $query);
|
||||||
|
if (!$param->isResultsOK()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ($get_token) {
|
||||||
|
return $param->getData('access_token');
|
||||||
|
}
|
||||||
|
return $param->getData();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Subject
|
||||||
|
* @package wchat\qq
|
||||||
|
*/
|
||||||
|
class Subject extends \wchat\base\Subject
|
||||||
|
{
|
||||||
|
|
||||||
|
private $sendUrl = 'https://api.q.qq.com/api/json/subscribe/SendSubscriptionMessage';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUrl(): string
|
||||||
|
{
|
||||||
|
return $this->getUrl();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/8 0008
|
||||||
|
* Time: 9:49
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Template
|
||||||
|
* @package wchat\qq
|
||||||
|
*/
|
||||||
|
class Template extends \wchat\base\Template
|
||||||
|
{
|
||||||
|
|
||||||
|
private $sendUrl = 'https://api.q.qq.com/api/json/template/send';
|
||||||
|
|
||||||
|
public function getUrl(): string
|
||||||
|
{
|
||||||
|
return $this->sendUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Token extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq\pay;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
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 $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
||||||
|
|
||||||
|
private $_errors = [
|
||||||
|
'66228701' => '红包个数超出限制',
|
||||||
|
'66228705' => '总金额超出限制',
|
||||||
|
'66228706' => '总金额不足以按最小金额领取每个红包',
|
||||||
|
'66228707' => '商户签名校验失败',
|
||||||
|
'66228708' => '重入??',
|
||||||
|
'66228709' => 'openid转换uin失败',
|
||||||
|
'66228711' => '商户订单中的商户号有误',
|
||||||
|
'66228712' => '商户订单中的日期超过范围',
|
||||||
|
'66228713' => '余额不足',
|
||||||
|
'66228715' => '用户未关注公众号,发送AIO消息失败',
|
||||||
|
'66229716' => '用户禁用公众号,发AIO消息失败'
|
||||||
|
];
|
||||||
|
|
||||||
|
private $_requestParams = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setMchName(string $value)
|
||||||
|
{
|
||||||
|
$this->_requestParams['mch_name'] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setActName(string $value)
|
||||||
|
{
|
||||||
|
$this->_requestParams['act_name'] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setWishing(string $value)
|
||||||
|
{
|
||||||
|
$this->_requestParams['wishing'] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setIconId($value)
|
||||||
|
{
|
||||||
|
$this->_requestParams['icon_id'] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $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)
|
||||||
|
{
|
||||||
|
$client = HttpClient::NewRequest();
|
||||||
|
$client->setIsSSL(true);
|
||||||
|
$client->setSslCertFile($this->getConfig()->getSslCert());
|
||||||
|
$client->setSslKeyFile($this->getConfig()->getSslKey());
|
||||||
|
$client->setCa($this->getConfig()->getSslCa());
|
||||||
|
$client->setUseSwoole($this->getConfig()->isUsrSwoole());
|
||||||
|
$client->setHost('api.qpay.qq.com');
|
||||||
|
$client->setCallback(function ($response, $requestParam) {
|
||||||
|
$json = json_decode($response, true);
|
||||||
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||||
|
$array['code'] = 500;
|
||||||
|
$array['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']];
|
||||||
|
}
|
||||||
|
$array['code'] = $json['retcode'];
|
||||||
|
$array['message'] = $json['retmsg'];
|
||||||
|
} else {
|
||||||
|
$array['code'] = 0;
|
||||||
|
$array['message'] = $json['retmsg'];
|
||||||
|
$array['data'] = ['listid' => $json['listid']];
|
||||||
|
}
|
||||||
|
$array['data']['request'] = $requestParam;
|
||||||
|
return new Result($array);
|
||||||
|
});
|
||||||
|
$response = $client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||||
|
if (!$response->isResultsOK()) {
|
||||||
|
throw new Exception($response->getMessage());
|
||||||
|
}
|
||||||
|
return $response->getData('listid');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $mch_billno
|
||||||
|
* @param $openId
|
||||||
|
* @param $price
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function orderConfig($mch_billno, $openId, $price)
|
||||||
|
{
|
||||||
|
$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) && 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()
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
$notifySign = $requestParams['sign'];
|
||||||
|
unset($requestParams['sign']);
|
||||||
|
$sign = Help::sign($requestParams, $this->getConfig()->getKey(), 'MD5');
|
||||||
|
|
||||||
|
if ($sign !== $notifySign) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq\pay;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
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 $_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 string $value
|
||||||
|
*/
|
||||||
|
public function setOpUserId(string $value)
|
||||||
|
{
|
||||||
|
$this->_requestParams['op_user_id'] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setOpUserPassword(string $value)
|
||||||
|
{
|
||||||
|
$this->_requestParams['op_user_passwd'] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function setSpbillCreateIp(string $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 = HttpClient::NewRequest();
|
||||||
|
$client->setIsSSL(true);
|
||||||
|
$client->setSslCertFile($this->getConfig()->getSslCert());
|
||||||
|
$client->setSslKeyFile($this->getConfig()->getSslKey());
|
||||||
|
$client->setCa($this->getConfig()->getSslCa());
|
||||||
|
$client->setUseSwoole($this->getConfig()->isUsrSwoole());
|
||||||
|
$client->setHost('api.qpay.qq.com');
|
||||||
|
$client->setCallback(function ($response, $requestParam) {
|
||||||
|
$json = json_decode($response, true);
|
||||||
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
||||||
|
$array['code'] = 500;
|
||||||
|
$array['message'] = $json['return_msg'] ?? $json['retmsg'];
|
||||||
|
} else if ($json['result_code'] != 'SUCCESS') {
|
||||||
|
$array['code'] = 500;
|
||||||
|
$array['message'] = $this->_errors[$json['err_code']] ?? $json['err_code_desc'];
|
||||||
|
} else {
|
||||||
|
$array['code'] = 0;
|
||||||
|
$array['message'] = 'system success';
|
||||||
|
$array['data'] = $json;
|
||||||
|
}
|
||||||
|
$array['data']['request'] = $requestParam;
|
||||||
|
return new Result($array);
|
||||||
|
});
|
||||||
|
$response = $client->post($this->_cash, $this->orderConfig($mch_billno, $openId, $price));
|
||||||
|
if (!$response->isResultsOK()) {
|
||||||
|
throw new Exception($response->getMessage());
|
||||||
|
}
|
||||||
|
return $response->getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $mch_billno
|
||||||
|
* @param $openId
|
||||||
|
* @param $price
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function orderConfig($mch_billno, $openId, $price)
|
||||||
|
{
|
||||||
|
$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()
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
$notifySign = $requestParams['sign'];
|
||||||
|
unset($requestParams['sign']);
|
||||||
|
$sign = Help::sign($requestParams, $this->getConfig()->getKey(), 'MD5');
|
||||||
|
|
||||||
|
if ($sign !== $notifySign) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\qq\result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RedHatResult
|
||||||
|
* @package qq\result
|
||||||
|
*/
|
||||||
|
class RedHatResult
|
||||||
|
{
|
||||||
|
|
||||||
|
public $result;
|
||||||
|
public $res_info;
|
||||||
|
|
||||||
|
public $listid;
|
||||||
|
public $state;
|
||||||
|
public $total_num;
|
||||||
|
public $recv_num;
|
||||||
|
public $total_amount;
|
||||||
|
public $recv_amount;
|
||||||
|
public $recv_details;
|
||||||
|
public $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 $this
|
||||||
|
*/
|
||||||
|
private function init($array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => $value) {
|
||||||
|
if (!property_exists($this, $key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->{$key} = $value;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $field
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function get($field)
|
||||||
|
{
|
||||||
|
return $this->$field;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* 是否已完成支付
|
||||||
|
*/
|
||||||
|
public function isPaid()
|
||||||
|
{
|
||||||
|
return $this->state == self::RED_HAT_STATUS_PAID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* 是否已抢完
|
||||||
|
*/
|
||||||
|
public function isSnatched()
|
||||||
|
{
|
||||||
|
return $this->state == self::RED_HAT_STATUS_SNATCHED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isExpired()
|
||||||
|
{
|
||||||
|
return $this->state == self::RED_HAT_STATUS_EXPIRED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isRefunded()
|
||||||
|
{
|
||||||
|
return $this->state == self::RED_HAT_STATUS_REFUNDED;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,8 +6,13 @@
|
|||||||
* Time: 16:12
|
* Time: 16:12
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\wx;
|
||||||
class Account extends Miniprogarampage
|
|
||||||
|
use wchat\common\Decode;
|
||||||
|
use wchat\common\HttpClient;
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class Account extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
||||||
@@ -15,7 +20,7 @@ class Account extends Miniprogarampage
|
|||||||
private $publicInfo = 'https://api.weixin.qq.com/cgi-bin/user/info?';
|
private $publicInfo = 'https://api.weixin.qq.com/cgi-bin/user/info?';
|
||||||
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
||||||
|
|
||||||
private $savePath = __DIR__ . '/../../../';
|
private $savePath = __DIR__ . '/../../Users/';
|
||||||
|
|
||||||
|
|
||||||
private $OK = 0;
|
private $OK = 0;
|
||||||
@@ -43,8 +48,10 @@ class Account extends Miniprogarampage
|
|||||||
$param['js_code'] = $code;
|
$param['js_code'] = $code;
|
||||||
$param['grant_type'] = 'authorization_code';
|
$param['grant_type'] = 'authorization_code';
|
||||||
|
|
||||||
$this->request->setMethod(WxClient::GET);
|
$this->request->setMethod(HttpClient::GET);
|
||||||
$this->request->addHeader('Content-Type', 'text/xml');
|
$this->request->addHeader('Content-Type', 'text/xml');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
|
||||||
return $this->request->get('sns/jscode2session', $param);
|
return $this->request->get('sns/jscode2session', $param);
|
||||||
}
|
}
|
||||||
@@ -58,12 +65,14 @@ class Account extends Miniprogarampage
|
|||||||
{
|
{
|
||||||
$query = [
|
$query = [
|
||||||
'access_token' => $this->getAccessToken(),
|
'access_token' => $this->getAccessToken(),
|
||||||
'openid' => $openid,
|
'openid' => $openid,
|
||||||
'lang' => 'zh_CN'
|
'lang' => 'zh_CN'
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->request->setMethod(WxClient::GET);
|
$this->request->setMethod(HttpClient::GET);
|
||||||
$this->request->setIsSSL(true);
|
$this->request->setIsSSL(true);
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
return $this->request->get($this->publicInfo, $query);
|
return $this->request->get($this->publicInfo, $query);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,33 +93,13 @@ class Account extends Miniprogarampage
|
|||||||
*/
|
*/
|
||||||
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
||||||
{
|
{
|
||||||
$config = Wx::getMiniProGaRamPage()->getConfig();
|
$decode = new Decode();
|
||||||
if (strlen($sessionKey) != 24) {
|
$decode->setSessionKey($sessionKey);
|
||||||
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
$decode->setEncryptedData($encryptedData);
|
||||||
}
|
$decode->setAppId($this->config->getAppid());
|
||||||
|
$decode->setIv($iv);
|
||||||
|
|
||||||
$aesKey = base64_decode($sessionKey);
|
return $decode->decode($asArray);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -127,8 +116,10 @@ class Account extends Miniprogarampage
|
|||||||
$sendBody['path'] = $path;
|
$sendBody['path'] = $path;
|
||||||
$sendBody['width'] = $width;
|
$sendBody['width'] = $width;
|
||||||
|
|
||||||
$this->request->setMethod(WxClient::POST);
|
$this->request->setMethod(HttpClient::POST);
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
$this->request->setCallback([$this, 'saveByPath']);
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
|
||||||
return $this->request->post($url, $sendBody);
|
return $this->request->post($url, $sendBody);
|
||||||
}
|
}
|
||||||
@@ -155,8 +146,10 @@ class Account extends Miniprogarampage
|
|||||||
|
|
||||||
$url = $this->getwxacode . $this->getAccessToken();
|
$url = $this->getwxacode . $this->getAccessToken();
|
||||||
|
|
||||||
$this->request->setMethod(WxClient::POST);
|
$this->request->setMethod(HttpClient::POST);
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
$this->request->setCallback([$this, 'saveByPath']);
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
return $this->request->post($url, $sendBody);
|
return $this->request->post($url, $sendBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,8 +175,10 @@ class Account extends Miniprogarampage
|
|||||||
|
|
||||||
$url = $this->getwxacodeunlimit . $this->getAccessToken();
|
$url = $this->getwxacodeunlimit . $this->getAccessToken();
|
||||||
|
|
||||||
$this->request->setMethod(WxClient::POST);
|
$this->request->setMethod(HttpClient::POST);
|
||||||
$this->request->setCallback([$this, 'saveByPath']);
|
$this->request->setCallback([$this, 'saveByPath']);
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
return $this->request->post($url, $sendBody);
|
return $this->request->post($url, $sendBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ContentAsyncCheck
|
||||||
|
* @package wchat
|
||||||
|
*/
|
||||||
|
class ContentAsyncCheck
|
||||||
|
{
|
||||||
|
private $_ToUserName = '';
|
||||||
|
private $_FromUserName = '';
|
||||||
|
private $_CreateTime = '';
|
||||||
|
private $_MsgType = '';
|
||||||
|
private $_Event = '';
|
||||||
|
private $_isrisky = '';
|
||||||
|
private $_extra_info_json = '';
|
||||||
|
private $_appid = '';
|
||||||
|
private $_trace_id = '';
|
||||||
|
private $_status_code = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getToUserName(): string
|
||||||
|
{
|
||||||
|
return $this->_ToUserName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFromUserName(): string
|
||||||
|
{
|
||||||
|
return $this->_FromUserName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCreateTime(): string
|
||||||
|
{
|
||||||
|
return $this->_CreateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMsgType(): string
|
||||||
|
{
|
||||||
|
return $this->_MsgType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getEvent(): string
|
||||||
|
{
|
||||||
|
return $this->_Event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getIsrisky(): string
|
||||||
|
{
|
||||||
|
return $this->_isrisky;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getExtraInfoJson(): string
|
||||||
|
{
|
||||||
|
return $this->_extra_info_json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAppid(): string
|
||||||
|
{
|
||||||
|
return $this->_appid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTraceId(): string
|
||||||
|
{
|
||||||
|
return $this->_trace_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getStatusCode(): string
|
||||||
|
{
|
||||||
|
return $this->_status_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $params
|
||||||
|
* @return ContentAsyncCheck|null
|
||||||
|
*/
|
||||||
|
public static function instance(array $params)
|
||||||
|
{
|
||||||
|
static $class = null;
|
||||||
|
if ($class === null) {
|
||||||
|
$class = new ContentAsyncCheck();
|
||||||
|
}
|
||||||
|
return $class->init($params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $params
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
private function init($params)
|
||||||
|
{
|
||||||
|
foreach ($params as $item => $param) {
|
||||||
|
$this->{'_' . $item} = $param;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isRisky()
|
||||||
|
{
|
||||||
|
return intval($this->_isrisky) === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isSuccess()
|
||||||
|
{
|
||||||
|
return $this->_status_code === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace qq;
|
namespace wchat\wx;
|
||||||
|
|
||||||
use wchat\Miniprogarampage;
|
use wchat\common\HttpClient;
|
||||||
use wchat\Result;
|
use wchat\common\Result;
|
||||||
use wchat\WxClient;
|
class Message extends SmallProgram
|
||||||
|
|
||||||
class Message extends Miniprogarampage
|
|
||||||
{
|
{
|
||||||
const TEXT = 0;
|
const TEXT = 0;
|
||||||
const IMAGE = 1;
|
const IMAGE = 1;
|
||||||
@@ -251,12 +249,12 @@ class Message extends Miniprogarampage
|
|||||||
|
|
||||||
$real_path = new \CURLFile(realpath($filePath));
|
$real_path = new \CURLFile(realpath($filePath));
|
||||||
|
|
||||||
$data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[content-type]' => $mime);
|
$data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime);
|
||||||
if ($isPermanent && $mime == 'video/mp3') {
|
if ($isPermanent && $mime == 'video/mp3') {
|
||||||
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->request->setMethod(WxClient::POST);
|
$this->request->setMethod(HttpClient::POST);
|
||||||
|
|
||||||
/** @var Result $body */
|
/** @var Result $body */
|
||||||
$data = $this->request->post($url, $data);
|
$data = $this->request->post($url, $data);
|
||||||
@@ -301,7 +299,9 @@ class Message extends Miniprogarampage
|
|||||||
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
|
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
|
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
|
||||||
$this->request->setMethod(WxClient::POST);
|
$this->request->setMethod(HttpClient::POST);
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
|
||||||
/** @var Result $body */
|
/** @var Result $body */
|
||||||
$body = $this->request->post($url, $data);
|
$body = $this->request->post($url, $data);
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
class Notify extends Miniprogarampage
|
class Notify extends SmallProgram
|
||||||
{
|
{
|
||||||
public $appid = '';
|
public $appid = '';
|
||||||
public $mch_id = '';
|
public $mch_id = '';
|
||||||
@@ -6,9 +6,11 @@
|
|||||||
* Time: 9:49
|
* Time: 9:49
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace wchat;
|
namespace wchat\wx;
|
||||||
|
|
||||||
class PublicTemplate extends Miniprogarampage
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class PublicTemplate extends SmallProgram
|
||||||
{
|
{
|
||||||
|
|
||||||
private $keywords = [];
|
private $keywords = [];
|
||||||
@@ -144,10 +146,11 @@ class PublicTemplate extends Miniprogarampage
|
|||||||
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
$this->request->setIsSSL(true);
|
$this->request->setIsSSL(true);
|
||||||
$this->request->addHeader('content-type', 'application/json');
|
$this->request->addHeader('Content-Type', 'application/json');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
$result = $this->request->post($url, $params);
|
$result = $this->request->post($url, $params);
|
||||||
$result->append('postBody', $params);
|
$result->append('postBody', $default);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -6,14 +6,13 @@
|
|||||||
* Time: 10:22
|
* Time: 10:22
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace qq;
|
namespace wchat\wx;
|
||||||
|
|
||||||
use wchat\Miniprogarampage;
|
use wchat\common\HttpClient;
|
||||||
use wchat\Result;
|
use wchat\common\Result;
|
||||||
use wchat\Help;
|
use wchat\common\Help;
|
||||||
use wchat\WxClient;
|
|
||||||
|
|
||||||
class Recharge extends Miniprogarampage
|
class Recharge extends SmallProgram
|
||||||
{
|
{
|
||||||
private $money = 0;
|
private $money = 0;
|
||||||
|
|
||||||
@@ -22,7 +21,7 @@ class Recharge extends Miniprogarampage
|
|||||||
private $data = [];
|
private $data = [];
|
||||||
|
|
||||||
private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
|
private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
|
||||||
private $unifiedorder = 'https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi';
|
private $unifiedorder = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $money
|
* @param int $money
|
||||||
@@ -61,7 +60,7 @@ class Recharge extends Miniprogarampage
|
|||||||
$return['message'] = $data['return_msg'];
|
$return['message'] = $data['return_msg'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $return;
|
return new Result($return);
|
||||||
});
|
});
|
||||||
return $this->send($this->unifiedorder, $this->builder());
|
return $this->send($this->unifiedorder, $this->builder());
|
||||||
}
|
}
|
||||||
@@ -87,7 +86,6 @@ class Recharge extends Miniprogarampage
|
|||||||
$key = $this->config->getKey();
|
$key = $this->config->getKey();
|
||||||
$sign_type = $this->config->getSignType();
|
$sign_type = $this->config->getSignType();
|
||||||
$array['paySign'] = Help::sign($array, $key, $sign_type);
|
$array['paySign'] = Help::sign($array, $key, $sign_type);
|
||||||
$array['appId'] = $this->config->getAppid();
|
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +154,9 @@ class Recharge extends Miniprogarampage
|
|||||||
|
|
||||||
$this->request->setCallback(function ($data) {
|
$this->request->setCallback(function ($data) {
|
||||||
$array = Help::toArray($data);
|
$array = Help::toArray($data);
|
||||||
if ($array['result_code'] != 'SUCCESS') {
|
if ($array['return_code'] != 'SUCCESS') {
|
||||||
|
$data = ['code' => $array['return_code'], 'message' => $array['return_msg']];
|
||||||
|
} else if ($array['result_code'] != 'SUCCESS') {
|
||||||
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
|
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
|
||||||
} else {
|
} else {
|
||||||
$data = ['code' => 0, 'message' => '支付成功'];
|
$data = ['code' => 0, 'message' => '支付成功'];
|
||||||
@@ -175,8 +175,10 @@ class Recharge extends Miniprogarampage
|
|||||||
private function send($url, $data)
|
private function send($url, $data)
|
||||||
{
|
{
|
||||||
$this->request->setIsSSL(true);
|
$this->request->setIsSSL(true);
|
||||||
$this->request->setMethod(WxClient::POST);
|
$this->request->setMethod(HttpClient::POST);
|
||||||
$this->request->addHeader('Content-Type', 'text/xml');
|
$this->request->addHeader('Content-Type', 'text/xml');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
return $this->request->send($url, $data);
|
return $this->request->send($url, $data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SecCheck
|
||||||
|
* @package wchat
|
||||||
|
*/
|
||||||
|
class SecCheck extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
private $_url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=';
|
||||||
|
|
||||||
|
private $_msgUrl = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=';
|
||||||
|
|
||||||
|
private $_mediaCheckAsync = 'https://api.weixin.qq.com/wxa/media_check_async?access_token=';
|
||||||
|
|
||||||
|
const MEDIA_VIDEO = 1;
|
||||||
|
const MEDIA_IMAGE = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return array|Result|mixed
|
||||||
|
*/
|
||||||
|
public function image($path = '')
|
||||||
|
{
|
||||||
|
if (!file_exists($path)) {
|
||||||
|
return $this->sendError('文件不存在', 404);
|
||||||
|
}
|
||||||
|
$this->request->setUseSwoole(false);
|
||||||
|
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->post($this->_url . $this->config->getAccessToken(), ['media' => new \CURLFile($path)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @param int $type
|
||||||
|
* @return mixed
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function mediaAsync(string $url, $type = SecCheck::MEDIA_IMAGE)
|
||||||
|
{
|
||||||
|
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
||||||
|
throw new \Exception('暂不支持的文件类型');
|
||||||
|
}
|
||||||
|
$this->request->setHeader('Content-Type', 'application/json');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
$response = $this->request->post($this->_mediaCheckAsync . $this->config->getAccessToken(), [
|
||||||
|
'media_url' => $url,
|
||||||
|
'media_type' => $type
|
||||||
|
]);
|
||||||
|
if (!$response->isResultsOK()) {
|
||||||
|
throw new \Exception($response->getMessage());
|
||||||
|
}
|
||||||
|
return $response->getData('trace_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $params
|
||||||
|
* @return ContentAsyncCheck|null
|
||||||
|
*/
|
||||||
|
public function readByEvent($params)
|
||||||
|
{
|
||||||
|
return ContentAsyncCheck::instance($params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $content
|
||||||
|
* @return array|Result|mixed
|
||||||
|
*/
|
||||||
|
public function text($content)
|
||||||
|
{
|
||||||
|
if (empty($content)) {
|
||||||
|
return $this->sendError('文件不存在', 404);
|
||||||
|
}
|
||||||
|
$this->request->setHeader('Content-Type', 'application/json');
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
return $this->request->post($this->_msgUrl . $this->config->getAccessToken(), ['content' => $content]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Miniprogarampage;
|
||||||
|
|
||||||
|
class SmallProgram extends Miniprogarampage
|
||||||
|
{
|
||||||
|
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
|
private $url = 'https://api.weixin.qq.com/cgi-bin/token';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function generateAccess_token()
|
||||||
|
{
|
||||||
|
if (!empty($this->config->getToken())) {
|
||||||
|
return $this->config->getToken();
|
||||||
|
}
|
||||||
|
$query = [
|
||||||
|
'grant_type' => 'client_credential',
|
||||||
|
'appid' => $this->config->getAppid(),
|
||||||
|
'secret' => $this->config->getAppsecret()
|
||||||
|
];
|
||||||
|
$this->request->setErrorField('errcode');
|
||||||
|
$this->request->setErrorMsgField('errmsg');
|
||||||
|
$param = $this->request->get($this->url, $query);
|
||||||
|
if (!$param->isResultsOK()) {
|
||||||
|
throw new \Exception($param->getMessage());
|
||||||
|
}
|
||||||
|
return $param->getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
use wchat\common\Result;
|
||||||
|
|
||||||
|
class Subject extends \wchat\base\Subject
|
||||||
|
{
|
||||||
|
|
||||||
|
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUrl(): string
|
||||||
|
{
|
||||||
|
return $this->sendUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?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
|
||||||
|
{
|
||||||
|
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
|
||||||
|
|
||||||
|
public function getUrl(): string
|
||||||
|
{
|
||||||
|
return $this->sendUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace wchat\wx;
|
||||||
|
|
||||||
|
|
||||||
|
class Token extends SmallProgram
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
-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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-176
@@ -1,176 +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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function builder()
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'mch_id' => $this->config->getMchId(),
|
|
||||||
'nonce_str' => Help::random(32),
|
|
||||||
'body' => $this->config->getBody(),
|
|
||||||
'out_trade_no' => $this->orderNo,
|
|
||||||
'total_fee' => $this->money,
|
|
||||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
|
||||||
'notify_url' => $this->config->getNotifyUrl(),
|
|
||||||
'trade_type' => $this->config->getTradeType(),
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->data = array_merge($data, $this->data);
|
|
||||||
|
|
||||||
$key = $this->config->getKey();
|
|
||||||
$sign_type = $this->config->getSignType();
|
|
||||||
|
|
||||||
$this->data['sign_type'] = $this->config->getSignType();
|
|
||||||
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
|
|
||||||
|
|
||||||
return Help::toXml($this->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSignData()
|
|
||||||
{
|
|
||||||
$data = $this->data;
|
|
||||||
$data['key'] = $this->config->getKey();
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $money
|
|
||||||
* @param $openid
|
|
||||||
* @param $order
|
|
||||||
* @param $desc
|
|
||||||
* @return Result
|
|
||||||
* @throws
|
|
||||||
*
|
|
||||||
* 提现
|
|
||||||
*/
|
|
||||||
public function cashWithdrawal($money, $openid, $order, $desc = '零钱提现')
|
|
||||||
{
|
|
||||||
$array = [
|
|
||||||
'nonce_str' => Help::random(32),
|
|
||||||
'partner_trade_no' => $order,
|
|
||||||
'mchid' => $this->config->getMchId(),
|
|
||||||
'mch_appid' => $this->config->getAppid(),
|
|
||||||
'openid' => $openid,
|
|
||||||
'check_name' => 'NO_CHECK',
|
|
||||||
'amount' => $money * 100,
|
|
||||||
'spbill_create_ip' => $this->config->getRemoteAddr(),
|
|
||||||
'desc' => $desc,
|
|
||||||
];
|
|
||||||
|
|
||||||
$key = $this->config->getKey();
|
|
||||||
$sign_type = $this->config->getSignType();
|
|
||||||
$array['sign'] = Help::sign($array, $key, $sign_type);
|
|
||||||
|
|
||||||
$this->request->setCallback(function ($data) {
|
|
||||||
$array = Help::toArray($data);
|
|
||||||
if ($array['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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace wchat;
|
|
||||||
|
|
||||||
|
|
||||||
class Token extends Miniprogarampage
|
|
||||||
{
|
|
||||||
|
|
||||||
private $url = 'https://api.q.qq.com/cgi-bin/token';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function generateAccess_token()
|
|
||||||
{
|
|
||||||
$query = [
|
|
||||||
'grant_type' => 'client_credential',
|
|
||||||
'appid' => $this->config->getAppid(),
|
|
||||||
'secret' => $this->config->getAppsecret()
|
|
||||||
];
|
|
||||||
$param = $this->request->get($this->url, $query);
|
|
||||||
if (!$param->isResultsOK()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return $param->getData('access_token');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,123 +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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Notify
|
|
||||||
*/
|
|
||||||
public function getNotify()
|
|
||||||
{
|
|
||||||
return Notify::getInstance($this->config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Token
|
|
||||||
*/
|
|
||||||
public function getToken()
|
|
||||||
{
|
|
||||||
return Token::getInstance($this->config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return WxClient
|
|
||||||
*/
|
|
||||||
public function getClient()
|
|
||||||
{
|
|
||||||
$client = WxClient::getInstance();
|
|
||||||
$client->setAgent($this->config->getAgent());
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Subject
|
|
||||||
*/
|
|
||||||
public function getSubject()
|
|
||||||
{
|
|
||||||
return Subject::getInstance($this->config);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-428
@@ -1,428 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace wchat;
|
|
||||||
|
|
||||||
use Swoole\Coroutine\Http\Client;
|
|
||||||
use Swoole\Coroutine\System;
|
|
||||||
|
|
||||||
class WxClient
|
|
||||||
{
|
|
||||||
private $host = 'api.weixin.qq.com';
|
|
||||||
|
|
||||||
private $header = [];
|
|
||||||
|
|
||||||
private $callback = null;
|
|
||||||
private $method = 'get';
|
|
||||||
|
|
||||||
private $url = '';
|
|
||||||
private $isSSL = false;
|
|
||||||
private $agent = '';
|
|
||||||
|
|
||||||
const POST = 'post';
|
|
||||||
const GET = 'get';
|
|
||||||
const PUT = 'put';
|
|
||||||
const DELETE = 'delete';
|
|
||||||
const OPTIONS = 'option';
|
|
||||||
|
|
||||||
private function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return WxClient
|
|
||||||
*/
|
|
||||||
public static function getInstance()
|
|
||||||
{
|
|
||||||
static $client = null;
|
|
||||||
if (!($client instanceof WxClient)) {
|
|
||||||
$client = new WxClient();
|
|
||||||
}
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setAgent(string $agent)
|
|
||||||
{
|
|
||||||
$this->agent = $agent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $isSSL
|
|
||||||
*/
|
|
||||||
public function setIsSSL(bool $isSSL)
|
|
||||||
{
|
|
||||||
$this->isSSL = $isSSL;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getIsSSL()
|
|
||||||
{
|
|
||||||
return $this->isSSL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
private function request($url, $data = [])
|
|
||||||
{
|
|
||||||
if (function_exists('getIsCli') && getIsCli()) {
|
|
||||||
if (strpos($url, 'http://') !== FALSE) {
|
|
||||||
$url = str_replace('http://', '', $url);
|
|
||||||
} else if (strpos($url, 'https://') !== FALSE) {
|
|
||||||
$url = str_replace('https://', '', $url);
|
|
||||||
}
|
|
||||||
$explode = explode('/', $url);
|
|
||||||
$ip = System::gethostbyname(array_shift($explode));
|
|
||||||
return $this->coroutine($ip, $url, $data);
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
strpos($url, 'http://') === 0 ||
|
|
||||||
strpos($url, 'https://') === 0
|
|
||||||
) {
|
|
||||||
return $this->curl($url, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->isSSL) {
|
|
||||||
return $this->curl('https://' . $this->host . '/' . $url, $data);
|
|
||||||
} else {
|
|
||||||
return $this->curl('http://' . $this->host . '/' . $url, $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $ip
|
|
||||||
* @param $url
|
|
||||||
* @param array $data
|
|
||||||
* @return array|mixed|Result
|
|
||||||
* @throws \Exception
|
|
||||||
* 使用swoole协程方式请求
|
|
||||||
*/
|
|
||||||
private function coroutine($ip, $url, $data = [])
|
|
||||||
{
|
|
||||||
$_data = $this->paramEncode($data);
|
|
||||||
if ($this->method == 'get' && is_array($_data)) {
|
|
||||||
$url .= '?' . http_build_query($_data);
|
|
||||||
}
|
|
||||||
$client = $this->getClient($ip, $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 $host
|
|
||||||
* @param $port
|
|
||||||
* @param $url
|
|
||||||
* @param $data
|
|
||||||
* @return Client
|
|
||||||
*/
|
|
||||||
private function getClient($host, $port, $url, $data)
|
|
||||||
{
|
|
||||||
$client = new Client($host, $port, $this->isSSL);
|
|
||||||
if (!empty($this->agent)) {
|
|
||||||
$this->header['User-Agent'] = $this->agent;
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
if (is_array($_data)) {
|
|
||||||
$_data = http_build_query($_data);
|
|
||||||
}
|
|
||||||
$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 (!empty($this->agent)) {
|
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
|
|
||||||
}
|
|
||||||
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