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
|
|
|
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
|
|
|
|
|
*/
|
2023-12-12 15:35:37 +08:00
|
|
|
public function setErrorCode($code): void
|
2023-11-14 00:45:54 +08:00
|
|
|
{
|
|
|
|
|
$this->errorCode = $code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
*/
|
2023-12-12 15:35:37 +08:00
|
|
|
public function setErrorMessage($message): void
|
2023-11-14 00:45:54 +08:00
|
|
|
{
|
|
|
|
|
$this->errorMsg = $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorCode(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->errorCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorMessage(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->errorMsg;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/**
|
2023-11-14 12:39:28 +08:00
|
|
|
* @param string $host
|
2023-11-14 00:45:54 +08:00
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param mixed $body
|
2023-11-14 01:08:06 +08:00
|
|
|
* @param string $contentType
|
2023-11-14 00:45:54 +08:00
|
|
|
* @return Result
|
|
|
|
|
*/
|
2023-11-14 12:39:28 +08:00
|
|
|
protected function post(string $host, string $requestUrl, mixed $body, string $contentType = 'application/application'): Result
|
2023-11-14 00:45:54 +08:00
|
|
|
{
|
2023-11-14 12:39:28 +08:00
|
|
|
return $this->request($host, 'post', $requestUrl, $body, $contentType);
|
2023-11-14 00:45:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-14 12:39:28 +08:00
|
|
|
* @param string $host
|
2023-11-14 00:45:54 +08:00
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param mixed $body
|
2023-11-14 12:39:28 +08:00
|
|
|
* @param string $contentType
|
2023-11-14 00:45:54 +08:00
|
|
|
* @return Result
|
|
|
|
|
*/
|
2023-11-14 12:39:28 +08:00
|
|
|
protected function get(string $host, string $requestUrl, mixed $body, string $contentType = 'application/application'): Result
|
2023-11-14 00:45:54 +08:00
|
|
|
{
|
2023-11-14 12:39:28 +08:00
|
|
|
return $this->request($host, 'get', $requestUrl, $body, $contentType);
|
2023-11-14 00:45:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-14 12:39:28 +08:00
|
|
|
* @param string $host
|
2023-11-14 00:45:54 +08:00
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param mixed $body
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
2023-11-14 12:39:28 +08:00
|
|
|
protected function upload(string $host, string $requestUrl, mixed $body): Result
|
2023-11-14 00:45:54 +08:00
|
|
|
{
|
2023-11-14 12:39:28 +08:00
|
|
|
return $this->request($host, 'upload', $requestUrl, $body);
|
2023-11-14 00:45:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-14 12:39:28 +08:00
|
|
|
* @param string $host
|
2023-11-14 00:45:54 +08:00
|
|
|
* @param string $method
|
|
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param $body
|
2023-11-14 01:08:06 +08:00
|
|
|
* @param string $contentType
|
2023-11-14 00:45:54 +08:00
|
|
|
* @return Result
|
|
|
|
|
*/
|
2023-11-14 12:39:28 +08:00
|
|
|
private function request(string $host, string $method, string $requestUrl, $body, string $contentType = 'application/application'): Result
|
2023-11-14 00:45:54 +08:00
|
|
|
{
|
2023-11-14 12:39:28 +08:00
|
|
|
$client = new Client($host, 80, true);
|
2023-11-14 01:12:03 +08:00
|
|
|
$proxyHost = $this->payConfig->getProxyHost();
|
|
|
|
|
$proxyPort = $this->payConfig->getProxyPort();
|
2023-11-14 00:45:54 +08:00
|
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
|
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
|
|
|
}
|
|
|
|
|
if ($method == 'post') {
|
2023-11-14 01:08:06 +08:00
|
|
|
$client->withHeader(['Content-Type' => $contentType]);
|
2023-11-14 00:45:54 +08:00
|
|
|
$client->post($requestUrl, $body);
|
|
|
|
|
} else if ($method == 'upload') {
|
|
|
|
|
$client->upload($requestUrl, $body);
|
|
|
|
|
} else {
|
2023-11-14 01:08:06 +08:00
|
|
|
$client->withHeader(['Content-Type' => $contentType]);
|
2023-11-14 00:45:54 +08:00
|
|
|
$client->get($requestUrl, $body);
|
|
|
|
|
}
|
|
|
|
|
$client->close();
|
2026-06-12 23:57:22 +08:00
|
|
|
if (!in_array($client->statusCode, [101, 200, 201])) {
|
|
|
|
|
return new Result(code: 505, message: $client->body);
|
2023-11-14 00:45:54 +08:00
|
|
|
}
|
2026-06-12 23:57:22 +08:00
|
|
|
$body = json_decode($client->body, true);
|
2023-11-23 13:54:13 +08:00
|
|
|
if (is_null($body) || (isset($body['errcode']) && $body['errcode'] != 0)) {
|
2023-11-14 00:45:54 +08:00
|
|
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
|
|
|
|
} else {
|
2023-11-23 13:54:13 +08:00
|
|
|
return new Result(code: 0, data: $body);
|
2023-11-14 00:45:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
|
|
|
|
|
}
|