From 850e42536bf099b9ec2d7d551c6dff7251108801 Mon Sep 17 00:00:00 2001 From: sugar1569 Date: Wed, 20 May 2026 17:29:44 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91refactor(admin=20login=20listener):=20restructure=20ad?= =?UTF-8?q?min=20login=20event=20listener=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 拆分原handle方法为独立的队列和定时任务状态检查方法,重构代码逻辑提升可读性,统一异常处理流程,优化变量命名规范 --- .../app/listener/admin/AdminLoginListener.php | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/crmeb/app/listener/admin/AdminLoginListener.php b/crmeb/app/listener/admin/AdminLoginListener.php index 768563a0c..c7f7037d9 100644 --- a/crmeb/app/listener/admin/AdminLoginListener.php +++ b/crmeb/app/listener/admin/AdminLoginListener.php @@ -12,35 +12,78 @@ namespace app\listener\admin; /** - * Class AdminLogin + * 管理员登录事件监听器 * @package app\listener\admin */ class AdminLoginListener { - + /** + * 处理管理员登录事件 + * @param array $event 事件数据 [key: 队列检测key, timer: 定时器状态] + * @return array [queue: 队列状态, timer: 定时器状态] + */ public function handle($event) { - $res = false; - $res1 = false; - try { - [$key] = $event; - //检测消息队列是否执行 - $path = root_path('runtime') . '.queue'; - $content = file_get_contents($path); - $res = $key === $content; - if (sys_config('queue_open', 0) == 0) $res = true; - } catch (\Throwable $e) { - } + $queueStatus = $this->checkQueueStatus($event); + $timerStatus = $this->checkTimerStatus(); + return [$queueStatus, $timerStatus]; + } + + /** + * 检查消息队列状态 + * @param array $event + * @return bool + */ + private function checkQueueStatus(array $event): bool + { try { - $timerPath = root_path('runtime') . '.timer'; - $timer = file_get_contents($timerPath); - if ($timer && $timer <= time() && $timer > (time() - 70)) { - $res1 = true; + if (sys_config('queue_open', 0) == 0) { + return true; } - } catch (\Throwable $e) { - } - return [$res, $res1]; + if (empty($event)) { + return false; + } + + [$key] = $event; + + $queueFilePath = root_path('runtime') . '.queue'; + if (!file_exists($queueFilePath)) { + return false; + } + + $content = file_get_contents($queueFilePath); + return $key === $content; + } catch (\Throwable $e) { + return false; + } + } + + /** + * 检查定时任务状态 + * @return bool + */ + private function checkTimerStatus(): bool + { + try { + $timerFilePath = root_path('runtime') . '.timer'; + + if (!file_exists($timerFilePath)) { + return false; + } + + $timer = file_get_contents($timerFilePath); + if (empty($timer)) { + return false; + } + + $currentTime = time(); + $isValid = $timer <= $currentTime && $timer > ($currentTime - 70); + + return $isValid; + } catch (\Throwable $e) { + return false; + } } }