From 9857797b80480f5d18f7243ac0e0d238be7fe9a3 Mon Sep 17 00:00:00 2001 From: lkddi Date: Thu, 2 Apr 2026 15:37:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(wechat):=20=E5=BC=80=E5=90=AF=E5=9C=A8?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BE=A4=E5=86=85=E7=9B=B4=E6=8E=A5=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=AA=8C=E8=AF=81=E7=A0=81=E7=BB=91=E5=AE=9A=E8=87=AA?= =?UTF-8?q?=E8=BA=AB=E8=B4=A6=E5=8F=B7=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/ConsumeWechatMessages.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/Console/Commands/ConsumeWechatMessages.php b/app/Console/Commands/ConsumeWechatMessages.php index ca23955..3c186dc 100644 --- a/app/Console/Commands/ConsumeWechatMessages.php +++ b/app/Console/Commands/ConsumeWechatMessages.php @@ -102,30 +102,31 @@ class ConsumeWechatMessages extends Command $fromUser = $msg['from_user']; $isChatroom = $msg['is_chatroom']; - // 绑定逻辑:必须是私聊,且内容格式为 BD-xxxxxx - if (! $isChatroom && preg_match('/^BD-\d{6}$/i', $content)) { - $this->info("收到潜在绑定请求: {$content} from {$fromUser}"); - $this->handleBindRequest(strtoupper($content), $fromUser, $apiService); + // 绑定逻辑:支持私聊和群聊。只要内容格式为 BD-xxxxxx + if (preg_match('/^BD-\d{6}$/i', $content)) { + $replyTarget = $isChatroom ? $msg['chatroom_id'] : $fromUser; + $this->info("收到潜在绑定请求: {$content} from {$fromUser} (Reply to: {$replyTarget})"); + $this->handleBindRequest(strtoupper($content), $fromUser, $replyTarget, $apiService); } } /** * 处理账号绑定请求 */ - protected function handleBindRequest(string $code, string $wxid, WechatBotApiService $apiService): void + protected function handleBindRequest(string $code, string $wxid, string $replyTarget, WechatBotApiService $apiService): void { $cacheKey = 'wechat_bind_code:'.$code; $username = Cache::get($cacheKey); if (! $username) { - $apiService->sendTextMessage($wxid, '❌ 绑定失败:该验证码无效或已过有效期(5分钟)。请在个人中心重新生成。'); + $apiService->sendTextMessage($replyTarget, '❌ 绑定失败:该验证码无效或已过有效期(5分钟)。请在个人中心重新生成。'); return; } $user = User::where('username', $username)->first(); if (! $user) { - $apiService->sendTextMessage($wxid, '❌ 绑定失败:找不到对应的用户账号。'); + $apiService->sendTextMessage($replyTarget, '❌ 绑定失败:找不到对应的用户账号。'); return; } @@ -133,7 +134,7 @@ class ConsumeWechatMessages extends Command // 判断该微信号是否已经被其他用户绑定(防止碰撞或安全隐患) $existing = User::where('wxid', $wxid)->where('id', '!=', $user->id)->first(); if ($existing) { - $apiService->sendTextMessage($wxid, "❌ 绑定失败:当前微信号已经被其他账号 [{$existing->username}] 绑定。请先解绑后再试。"); + $apiService->sendTextMessage($replyTarget, "❌ 绑定失败:当前微信号已经被其他账号 [{$existing->username}] 绑定。请先解绑后再试。"); return; } @@ -150,6 +151,6 @@ class ConsumeWechatMessages extends Command ."您已成功绑定聊天室账号:[{$username}]。\n" .'现在您可以接收重要系统通知了。'; - $apiService->sendTextMessage($wxid, $successMsg); + $apiService->sendTextMessage($replyTarget, $successMsg); } }