From b66d1de24428a7b2eb40bbe848c69277d55a7416 Mon Sep 17 00:00:00 2001 From: xl Date: Tue, 11 Jul 2023 21:04:33 +0800 Subject: [PATCH] Default Changelist --- Container.php | 9 +- wchat/common/Multiprogramming.php | 2 - wchat/common/Result.php | 263 +++++++++++++++++------------- wchat/qq/Account.php | 12 +- wchat/qq/QqFactory.php | 14 +- wchat/wx/WxFactory.php | 26 +-- 6 files changed, 178 insertions(+), 148 deletions(-) diff --git a/Container.php b/Container.php index af0a931..4e3aaef 100644 --- a/Container.php +++ b/Container.php @@ -12,7 +12,6 @@ class Container /** @var Container */ private static $_instance; - /** @var \common\Config */ private $config; private $container = []; @@ -38,8 +37,8 @@ class Container if (static::$_instance->exists($class)) { $newInstance = static::$_instance->get($class); - if (method_exists($newInstance, 'initConfig')) { - $newInstance->initConfig($config); + if (method_exists($newInstance, 'setConfig')) { + $newInstance->setConfig($config); } return $newInstance; } else { @@ -71,8 +70,8 @@ class Container throw new Exception('Class Con\'t instance.'); } $newInstance = $newInstance->newInstance(); - if (method_exists($newInstance, 'initConfig')) { - $newInstance->initConfig($this->config); + if (method_exists($newInstance, 'setConfig')) { + $newInstance->setConfig($this->config); } $this->container[$class] = $newInstance; diff --git a/wchat/common/Multiprogramming.php b/wchat/common/Multiprogramming.php index acd384b..eacf03c 100644 --- a/wchat/common/Multiprogramming.php +++ b/wchat/common/Multiprogramming.php @@ -18,8 +18,6 @@ abstract class Multiprogramming implements Progaram protected static ?Multiprogramming $instance = null; - protected ?HttpClient $request = null; - protected int $errorCode = 0; protected string $errorMsg = ''; diff --git a/wchat/common/Result.php b/wchat/common/Result.php index cf5e106..071eae0 100644 --- a/wchat/common/Result.php +++ b/wchat/common/Result.php @@ -2,7 +2,9 @@ namespace wchat\common; +use Exception; use JetBrains\PhpStorm\ArrayShape; +use Kiri\Client; /** * Class Result @@ -17,126 +19,163 @@ use JetBrains\PhpStorm\ArrayShape; class Result { - private ?int $startTime; - private ?int $requestTime; - private ?int $runTime; + private ?int $startTime; + private ?int $requestTime; + private ?int $runTime; - /** - * @param int $code - * @param string $message - * @param int $count - * @param mixed $data - * @param array $header - */ - public function __construct(public int $code, public string $message = '', public int $count = 0, public mixed $data = [], public array $header = [],) - { - } + /** + * @param int $code + * @param string $message + * @param int $count + * @param mixed $data + * @param array $header + */ + public function __construct(public int $code, public string $message = '', public int $count = 0, public mixed $data = [], public array $header = [],) + { + } - /** - * @param $name - * @return mixed - */ - public function __get($name) - { - return $this->$name; - } + /** + * @param string $data + * @return static + */ + public static function wxInit(string $data): static + { + $json = json_decode($data, true); + if (isset($json['errcode']) && $json['errcode'] != 0) { + return new Result(code: $json['errcode'], message: $json['errmsg']); + } else { + return new Result(code: 0, data: $json); + } + } - /** - * @param $name - * @param $value - * @return $this - */ - public function __set($name, $value) - { - $this->$name = $value; - - return $this; - } - - /** - * @return array - */ - #[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])] - public function getTime(): array - { - return [ - 'startTime' => $this->startTime, - 'requestTime' => $this->requestTime, - 'runTime' => $this->runTime, - ]; - } - - /** - * @param $key - * @param $data - * @return $this - * @throws \Exception - */ - public function setAttr($key, $data): static - { - if (!property_exists($this, $key)) { - throw new \Exception('未查找到相应对象属性'); - } - $this->$key = $data; - return $this; - } - - /** - * @return bool - */ - public function isResultsOK(): bool - { - if (!is_numeric($this->code)) { - return false; - } - return $this->code == 0; - } + /** + * @param Client $client + * @return static + */ + public static function init(Client $client): static + { + if (!in_array($client->getStatusCode(), [101, 200, 201])) { + return new Result(code: 505, message: 'network error.'); + } + $body = json_decode($client->getBody(), true); + if (isset($body['errcode']) && $body['errcode'] != 0) { + return new Result(code: $body['errcode'], message: $body['errmsg']); + } else { + return new Result(code: 0, data: $body); + } + } - /** - * @param string $name - * @return mixed - */ - public function getData(string $name = ''): mixed - { - if (!$this->isResultsOK()) { - return $this->data; - } - if (!empty($name) && isset($this->data[$name])) { - return $this->data[$name]; - } - return $this->data; - } + /** + * @param $name + * @return mixed + */ + public function __get($name) + { + return $this->$name; + } - /** - * @param $key - * @param $data - * @return $this - */ - public function append($key, $data): static - { - if (!is_array($this->data)) { - $this->data = ['origin' => $this->data]; - } - $this->data[$key] = $data; - return $this; - } - /** - * @return string - */ - public function getMessage(): string - { - return $this->message; - } + /** + * @param $name + * @param $value + * @return $this + */ + public function __set($name, $value) + { + $this->$name = $value; - /** - * @return int - */ - public function getCode(): int - { - return $this->code; - } + return $this; + } + + /** + * @return array + */ + #[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])] + public function getTime(): array + { + return [ + 'startTime' => $this->startTime, + 'requestTime' => $this->requestTime, + 'runTime' => $this->runTime, + ]; + } + + /** + * @param $key + * @param $data + * @return $this + * @throws Exception + */ + public function setAttr($key, $data): static + { + if (!property_exists($this, $key)) { + throw new Exception('未查找到相应对象属性'); + } + $this->$key = $data; + return $this; + } + + /** + * @return bool + */ + public function isResultsOK(): bool + { + if (!is_numeric($this->code)) { + return false; + } + return $this->code == 0; + } + + + /** + * @param string $name + * @return mixed + */ + public function getData(string $name = ''): mixed + { + if (!$this->isResultsOK()) { + return $this->data; + } + if (!is_array($this->data)) { + return $this->data; + } + if ($name != '') { + return $this->data[$name] ?? null; + } else { + return $this->data; + } + } + + /** + * @param $key + * @param $data + * @return $this + */ + public function append($key, $data): static + { + if (!is_array($this->data)) { + $this->data = ['origin' => $this->data]; + } + $this->data[$key] = $data; + return $this; + } + + /** + * @return string + */ + public function getMessage(): string + { + return $this->message; + } + + /** + * @return int + */ + public function getCode(): int + { + return $this->code; + } } diff --git a/wchat/qq/Account.php b/wchat/qq/Account.php index c23f269..282ff77 100644 --- a/wchat/qq/Account.php +++ b/wchat/qq/Account.php @@ -9,8 +9,6 @@ namespace wchat\qq; use Kiri\Client; -use wchat\common\Decode; -use wchat\common\HttpClient; use wchat\common\Result; class Account extends SmallProgram @@ -32,15 +30,7 @@ class Account extends SmallProgram $client->get('sns/jscode2session', $param); $client->close(); - if (!in_array($client->getStatusCode(), [101, 200, 201])) { - return new Result(code: 505, message: 'network error.'); - } - $body = json_decode($client->getBody(), true); - if (isset($body['errcode']) && $body['errcode'] != 0) { - return new Result(code: $body['errcode'], message: $body['errmsg']); - } else { - return new Result(code: 0, data: $body); - } + return Result::init($client); } } diff --git a/wchat/qq/QqFactory.php b/wchat/qq/QqFactory.php index 7ab1a91..71443e6 100644 --- a/wchat/qq/QqFactory.php +++ b/wchat/qq/QqFactory.php @@ -3,20 +3,22 @@ namespace wchat\wx; use Kiri\Di\Container; +use ReflectionException; use wchat\common\Config; class QqFactory { - /** - * @param $class - * @param Config $config - * @return mixed - */ + /** + * @param $class + * @param Config $config + * @return mixed + * @throws ReflectionException + */ public static function get($class, Config $config): mixed { - $container = Container::getInstance(); + $container = Container::instance(); $object = $container->get($class); $object->setConfig($config); return $object; diff --git a/wchat/wx/WxFactory.php b/wchat/wx/WxFactory.php index b175a25..62baf09 100644 --- a/wchat/wx/WxFactory.php +++ b/wchat/wx/WxFactory.php @@ -3,24 +3,26 @@ namespace wchat\wx; use Kiri\Di\Container; +use ReflectionException; use wchat\common\Config; class WxFactory { - /** - * @param $class - * @param Config $config - * @return mixed - */ - public static function get($class, Config $config) - { - $container = Container::getInstance(); - $object = $container->get($class); - $object->setConfig($config); - return $object; - } + /** + * @param $class + * @param Config $config + * @return object|null + * @throws ReflectionException + */ + public static function get($class, Config $config): ?object + { + $container = Container::instance(); + $object = $container->get($class); + $object->setConfig($config); + return $object; + } }