2022-09-09 16:42:55 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/3/26 0026
|
|
|
|
|
* Time: 10:23
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace wchat\common;
|
|
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
use Kiri\Client;
|
|
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
abstract class Multiprogramming implements Progaram
|
|
|
|
|
{
|
|
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
/** @var Config */
|
|
|
|
|
protected Config $config;
|
2022-09-09 16:42:55 +08:00
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
protected static ?Multiprogramming $instance = null;
|
2022-09-09 16:42:55 +08:00
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
protected int $errorCode = 0;
|
|
|
|
|
protected string $errorMsg = '';
|
2022-09-09 16:42:55 +08:00
|
|
|
|
|
|
|
|
|
2023-11-13 23:52:41 +08:00
|
|
|
/**
|
2023-11-14 00:06:57 +08:00
|
|
|
* @var AppConfig
|
2023-11-13 23:52:41 +08:00
|
|
|
*/
|
2023-11-14 00:06:57 +08:00
|
|
|
protected AppConfig $payConfig;
|
2023-11-13 23:52:41 +08:00
|
|
|
|
|
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
* @param int $code
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
protected function sendError($message, int $code = 500): Result
|
|
|
|
|
{
|
|
|
|
|
return new Result(code: $code, message: $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $code
|
|
|
|
|
*/
|
|
|
|
|
public function setErrorCode($code)
|
|
|
|
|
{
|
|
|
|
|
$this->errorCode = $code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
*/
|
|
|
|
|
public function setErrorMessage($message)
|
|
|
|
|
{
|
|
|
|
|
$this->errorMsg = $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorCode(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->errorCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorMessage(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->errorMsg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Config $config
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setConfig(Config $config): void
|
|
|
|
|
{
|
|
|
|
|
$this->config = $config;
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
|
2023-11-13 23:52:41 +08:00
|
|
|
/**
|
2023-11-14 00:06:57 +08:00
|
|
|
* @return AppConfig
|
2023-11-13 23:52:41 +08:00
|
|
|
*/
|
2023-11-14 00:06:57 +08:00
|
|
|
public function getPayConfig(): AppConfig
|
2023-11-13 23:52:41 +08:00
|
|
|
{
|
|
|
|
|
return $this->payConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-14 00:06:57 +08:00
|
|
|
* @param AppConfig $payConfig
|
2023-11-13 23:52:41 +08:00
|
|
|
*/
|
2023-11-14 00:06:57 +08:00
|
|
|
public function setPayConfig(AppConfig $payConfig): void
|
2023-11-13 23:52:41 +08:00
|
|
|
{
|
|
|
|
|
$this->payConfig = $payConfig;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
|
2023-11-14 00:45:54 +08:00
|
|
|
/**
|
|
|
|
|
* @return Config
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig(): Config
|
|
|
|
|
{
|
|
|
|
|
return $this->config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $result
|
|
|
|
|
* @return array|bool
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function checkSign($result): array|bool
|
|
|
|
|
{
|
|
|
|
|
$data = Help::toArray($result);
|
|
|
|
|
|
|
|
|
|
if (!isset($data['sign'])) {
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sign = $data['sign'];
|
|
|
|
|
|
|
|
|
|
unset($data['sign']);
|
|
|
|
|
|
|
|
|
|
$key = $this->config->getKey();
|
|
|
|
|
$sign_type = $this->config->getSignType();
|
|
|
|
|
|
|
|
|
|
$_sign = Help::sign($data, $key, $sign_type);
|
|
|
|
|
if ($sign != $_sign) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param mixed $body
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
protected function post(string $requestUrl, mixed $body): Result
|
|
|
|
|
{
|
|
|
|
|
return $this->request('post', $requestUrl, $body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param mixed $body
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
protected function get(string $requestUrl, mixed $body): Result
|
|
|
|
|
{
|
|
|
|
|
return $this->request('get', $requestUrl, $body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param mixed $body
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
protected function upload(string $requestUrl, mixed $body): Result
|
|
|
|
|
{
|
|
|
|
|
return $this->request('upload', $requestUrl, $body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param $body
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
private function request(string $method, string $requestUrl, $body): Result
|
|
|
|
|
{
|
|
|
|
|
$client = new Client('api.weixin.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
|
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
|
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
|
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
|
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
|
|
|
}
|
|
|
|
|
if ($method == 'post') {
|
|
|
|
|
$client->post($requestUrl, $body);
|
|
|
|
|
} else if ($method == 'upload') {
|
|
|
|
|
$client->upload($requestUrl, $body);
|
|
|
|
|
} else {
|
|
|
|
|
$client->get($requestUrl, $body);
|
|
|
|
|
}
|
|
|
|
|
$client->close();
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
|
|
|
return new Result(code: 505, message: $client->getBody());
|
|
|
|
|
}
|
|
|
|
|
$body = json_decode($client->getBody(), true);
|
|
|
|
|
if (!is_null($body)) {
|
|
|
|
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
|
|
|
|
} else {
|
|
|
|
|
return new Result(code: 0, data: $client->getBody());
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
|
|
|
|
|
}
|