Default Changelist

This commit is contained in:
xl
2023-07-11 21:04:33 +08:00
parent 191624b7e6
commit b66d1de244
6 changed files with 178 additions and 148 deletions
+4 -5
View File
@@ -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;
-2
View File
@@ -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 = '';
+44 -5
View File
@@ -2,7 +2,9 @@
namespace wchat\common;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri\Client;
/**
* Class Result
@@ -33,6 +35,39 @@ class Result
}
/**
* @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 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 $name
* @return mixed
@@ -72,12 +107,12 @@ class Result
* @param $key
* @param $data
* @return $this
* @throws \Exception
* @throws Exception
*/
public function setAttr($key, $data): static
{
if (!property_exists($this, $key)) {
throw new \Exception('未查找到相应对象属性');
throw new Exception('未查找到相应对象属性');
}
$this->$key = $data;
return $this;
@@ -104,11 +139,15 @@ class Result
if (!$this->isResultsOK()) {
return $this->data;
}
if (!empty($name) && isset($this->data[$name])) {
return $this->data[$name];
}
if (!is_array($this->data)) {
return $this->data;
}
if ($name != '') {
return $this->data[$name] ?? null;
} else {
return $this->data;
}
}
/**
* @param $key
+1 -11
View File
@@ -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);
}
}
+3 -1
View File
@@ -3,6 +3,7 @@
namespace wchat\wx;
use Kiri\Di\Container;
use ReflectionException;
use wchat\common\Config;
class QqFactory
@@ -13,10 +14,11 @@ class QqFactory
* @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;
+5 -3
View File
@@ -3,6 +3,7 @@
namespace wchat\wx;
use Kiri\Di\Container;
use ReflectionException;
use wchat\common\Config;
class WxFactory
@@ -12,11 +13,12 @@ class WxFactory
/**
* @param $class
* @param Config $config
* @return mixed
* @return object|null
* @throws ReflectionException
*/
public static function get($class, Config $config)
public static function get($class, Config $config): ?object
{
$container = Container::getInstance();
$container = Container::instance();
$object = $container->get($class);
$object->setConfig($config);
return $object;