feat(wechat): 开启在微信群内直接发送验证码绑定自身账号的支持
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user