This commit is contained in:
xl
2023-11-14 00:45:54 +08:00
parent e721fe4f36
commit 0d8ddfef54
12 changed files with 763 additions and 1300 deletions
+57 -4
View File
@@ -29,10 +29,13 @@ class AppConfig
private string $proxyHost = '';
private int $proxyPort = 0;
private string $notifyUrl = '';
private string $body = '';
private string $currency = 'CNY';
private string $remoteIp = '';
private string $notifyUrl = '';
private string $body = '';
private string $currency = 'CNY';
private string $remoteIp = '';
private string $accessToken = '';
private string $tradeType = 'JSAPI';
private string $signType = 'MD5';
/**
* @var int
@@ -165,4 +168,54 @@ class AppConfig
{
$this->remoteIp = $remoteIp;
}
/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}
/**
* @param string $accessToken
*/
public function setAccessToken(string $accessToken): void
{
$this->accessToken = $accessToken;
}
/**
* @return string
*/
public function getTradeType(): string
{
return $this->tradeType;
}
/**
* @param string $tradeType
*/
public function setTradeType(string $tradeType): void
{
$this->tradeType = $tradeType;
}
/**
* @return string
*/
public function getSignType(): string
{
return $this->signType;
}
/**
* @param string $signType
*/
public function setSignType(string $signType): void
{
$this->signType = $signType;
}
}
+148 -78
View File
@@ -8,16 +8,18 @@
namespace wchat\common;
use Kiri\Client;
abstract class Multiprogramming implements Progaram
{
/** @var Config */
protected Config $config;
/** @var Config */
protected Config $config;
protected static ?Multiprogramming $instance = null;
protected static ?Multiprogramming $instance = null;
protected int $errorCode = 0;
protected string $errorMsg = '';
protected int $errorCode = 0;
protected string $errorMsg = '';
/**
@@ -26,57 +28,57 @@ abstract class Multiprogramming implements Progaram
protected AppConfig $payConfig;
/**
* @param $message
* @param int $code
* @return Result
*/
protected function sendError($message, int $code = 500): Result
{
return new Result(code: $code, message: $message);
}
/**
* @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 $code
*/
public function setErrorCode($code)
{
$this->errorCode = $code;
}
/**
* @param $message
*/
public function setErrorMessage($message)
{
$this->errorMsg = $message;
}
/**
* @param $message
*/
public function setErrorMessage($message)
{
$this->errorMsg = $message;
}
/**
* @return int
*/
public function getErrorCode(): int
{
return $this->errorCode;
}
/**
* @return int
*/
public function getErrorCode(): int
{
return $this->errorCode;
}
/**
* @return string
*/
public function getErrorMessage(): string
{
return $this->errorMsg;
}
/**
* @return string
*/
public function getErrorMessage(): string
{
return $this->errorMsg;
}
/**
* @param Config $config
* @return void
*/
public function setConfig(Config $config): void
{
$this->config = $config;
}
/**
* @param Config $config
* @return void
*/
public function setConfig(Config $config): void
{
$this->config = $config;
}
/**
* @return AppConfig
@@ -95,39 +97,107 @@ abstract class Multiprogramming implements Progaram
}
/**
* @return Config
*/
public function getConfig(): Config
{
return $this->config;
}
/**
* @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);
/**
* @param $result
* @return array|bool
* @throws \Exception
*/
protected function checkSign($result): array|bool
{
$data = Help::toArray($result);
if (!isset($data['sign'])) {
return $data;
}
if (!isset($data['sign'])) {
return $data;
}
$sign = $data['sign'];
$sign = $data['sign'];
unset($data['sign']);
unset($data['sign']);
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$_sign = Help::sign($data, $key, $sign_type);
if ($sign != $_sign) {
return FALSE;
}
return $data;
}
$_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());
}
}
}