Merge pull request #224 from zhamao-robot/edge-bug-fix

边缘 Bug 修复
This commit is contained in:
Jerry
2022-12-31 20:53:35 +08:00
committed by GitHub
2 changed files with 30 additions and 17 deletions

View File

@@ -9,8 +9,6 @@ use OneBot\Driver\Event\WebSocket\WebSocketMessageEvent;
use OneBot\V12\Object\Action; use OneBot\V12\Object\Action;
use OneBot\V12\Object\MessageSegment; use OneBot\V12\Object\MessageSegment;
use OneBot\V12\Object\OneBotEvent; use OneBot\V12\Object\OneBotEvent;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ZM\Context\Trait\BotActionTrait; use ZM\Context\Trait\BotActionTrait;
use ZM\Exception\OneBot12Exception; use ZM\Exception\OneBot12Exception;
use ZM\Utils\MessageUtil; use ZM\Utils\MessageUtil;
@@ -19,6 +17,8 @@ class BotContext implements ContextInterface
{ {
use BotActionTrait; use BotActionTrait;
private static array $bots = [];
private static array $echo_id_list = []; private static array $echo_id_list = [];
private array $self; private array $self;
@@ -30,6 +30,7 @@ class BotContext implements ContextInterface
public function __construct(string $bot_id, string $platform, null|WebSocketMessageEvent|HttpRequestEvent $event = null) public function __construct(string $bot_id, string $platform, null|WebSocketMessageEvent|HttpRequestEvent $event = null)
{ {
$this->self = ['user_id' => $bot_id, 'platform' => $platform]; $this->self = ['user_id' => $bot_id, 'platform' => $platform];
self::$bots[$bot_id][$platform] = $this;
$this->base_event = $event; $this->base_event = $event;
} }
@@ -41,11 +42,7 @@ class BotContext implements ContextInterface
/** /**
* 快速回复机器人消息文本 * 快速回复机器人消息文本
* *
* @param array|MessageSegment|string|\Stringable $message 消息内容、消息段或消息段数组 * @param array|MessageSegment|string|\Stringable $message 消息内容、消息段或消息段数组
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws OneBot12Exception
* @throws \Throwable
*/ */
public function reply(\Stringable|MessageSegment|array|string $message) public function reply(\Stringable|MessageSegment|array|string $message)
{ {
@@ -76,13 +73,24 @@ class BotContext implements ContextInterface
/** /**
* 获取其他机器人的上下文操作对象 * 获取其他机器人的上下文操作对象
* *
* @param string $bot_id 机器人的 self.user_id 对应的 ID * @param string $bot_id 机器人的 self.user_id 对应的 ID
* @param string $platform 机器人的 self.platform 对应的 platform * @param string $platform 机器人的 self.platform 对应的 platform
* @return $this * @throws OneBot12Exception
*/ */
public function getBot(string $bot_id, string $platform = ''): BotContext public function getBot(string $bot_id, string $platform = ''): BotContext
{ {
return $this; if (isset(self::$bots[$bot_id])) {
if ($platform === '') {
$one = current(self::$bots[$bot_id]);
if ($one instanceof BotContext) {
return $one;
}
} elseif (isset(self::$bots[$bot_id][$platform])) {
return self::$bots[$bot_id][$platform];
}
}
// 到这里说明没找到对应的机器人,抛出异常
throw new OneBot12Exception('Bot not found.');
} }
/** /**
@@ -122,4 +130,9 @@ class BotContext implements ContextInterface
{ {
return self::$echo_id_list[$echo] ?? null; return self::$echo_id_list[$echo] ?? null;
} }
public function getSelf(): array
{
return $this->self;
}
} }

View File

@@ -45,23 +45,23 @@ class HttpEventListener
$result = HttpUtil::parseUri($event->getRequest(), $node, $params); $result = HttpUtil::parseUri($event->getRequest(), $node, $params);
switch ($result) { switch ($result) {
case ZM_ERR_NONE: // 解析到存在路由了 case ZM_ERR_NONE: // 解析到存在路由了
$handler = new AnnotationHandler(Route::class); $route_handler = new AnnotationHandler(Route::class);
$div = new Route($node['route']); $div = new Route($node['route']);
$div->params = $params; $div->params = $params;
$div->method = $node['method']; $div->method = $node['method'];
// TODO这里有个bug逻辑上 request_method 应该是个数组,而不是字符串,但是这里 $node['method'] 是字符串,所以这里只能用字符串来判断 // TODO这里有个bug逻辑上 request_method 应该是个数组,而不是字符串,但是这里 $node['method'] 是字符串,所以这里只能用字符串来判断
// $div->request_method = $node['request_method']; // $div->request_method = $node['request_method'];
$div->class = $node['class']; $div->class = $node['class'];
$starttime = microtime(true); $route_handler->handle($div, null, $params, $event->getRequest(), $event);
$handler->handle($div, null, $params, $event->getRequest(), $event); if (is_string($val = $route_handler->getReturnVal()) || ($val instanceof \Stringable)) {
if (is_string($val = $handler->getReturnVal()) || ($val instanceof \Stringable)) { // 返回的内容是可以被字符串化的,就当作 Body 来返回,状态码 200
$event->withResponse(HttpFactory::createResponse(200, null, [], Stream::create($val))); $event->withResponse(HttpFactory::createResponse(200, null, [], Stream::create($val)));
} elseif ($event->getResponse() === null) { } elseif ($event->getResponse() === null) {
// 过了一遍 Route没有促成 Response则返回 500路由必须有返回才行
$event->withResponse(HttpFactory::createResponse(500)); $event->withResponse(HttpFactory::createResponse(500));
} }
logger()->warning('Used ' . round((microtime(true) - $starttime) * 1000, 3) . ' ms');
break; break;
case ZM_ERR_ROUTE_METHOD_NOT_ALLOWED: case ZM_ERR_ROUTE_METHOD_NOT_ALLOWED: // 路由检测到存在,但是方法不匹配,则返回 405表示方法不受支持
$event->withResponse(HttpUtil::handleHttpCodePage(405)); $event->withResponse(HttpUtil::handleHttpCodePage(405));
break; break;
} }