From 395cfeb14abfdeaf3ee147dd0c19e70f1a1f9c48 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 29 Aug 2026 06:36:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20new=5Fwms=5Fnotify=20reply=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E7=94=A8=E6=88=B7=20@=EF=BC=88user=5Fid=20?= =?UTF-8?q?=E6=95=B0=E7=BB=84/=E5=88=86=E9=9A=94=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/index/controller/qiwe/Index.php | 27 ++++++-- .../library/new_wms/notify/ReplyHandler.php | 66 +++++++++++++++---- 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/application/index/controller/qiwe/Index.php b/application/index/controller/qiwe/Index.php index 89cb504..a7ef7a9 100644 --- a/application/index/controller/qiwe/Index.php +++ b/application/index/controller/qiwe/Index.php @@ -1133,12 +1133,27 @@ class Index extends Frontend if(!$atid){ $this->query->query("sendText", [$this->guid, $wxid, $msg, false]); }else{ - //如果有@,要清理掉msg消息中的@+toName内容 - $msg = str_replace("@".$toName, "", $msg); - $message = [ - ["subtype"=>1,"text"=>strval($atid)], - ["subtype"=>0,"text"=>strval($msg)] - ]; + // 支持多 @:atid 可为数组或逗号分隔字符串,拆分为多个 @ 项 + $atIds = is_array($atid) + ? $atid + : (preg_split('/[,,、;;|\s]+/u', strval($atid)) ?: []); + $atIds = array_values(array_unique(array_filter(array_map('strval', $atIds), function ($v) { + return $v !== ''; + }))); + + // 清理正文中的 @名字 前缀(逐个 @ 目标及单聊 toName) + foreach ($atIds as $id) { + $msg = str_replace("@".$id, "", $msg); + } + if ($toName !== '') { + $msg = str_replace("@".$toName, "", $msg); + } + + $message = []; + foreach ($atIds as $id) { + $message[] = ["subtype"=>1,"text"=>$id]; + } + $message[] = ["subtype"=>0,"text"=>strval($msg)]; $result = $this->query->query("sendHyperText", [$this->guid, $wxid, $message]); } break; diff --git a/application/index/library/new_wms/notify/ReplyHandler.php b/application/index/library/new_wms/notify/ReplyHandler.php index 060f278..a81df45 100644 --- a/application/index/library/new_wms/notify/ReplyHandler.php +++ b/application/index/library/new_wms/notify/ReplyHandler.php @@ -27,12 +27,12 @@ class ReplyHandler $content = $data['content'] ?? ''; $extId = $data['ext_id'] ?? ''; $groupId = $data['group_id'] ?? ''; - $userId = $data['user_id'] ?? ''; + $userIds = self::parseUserIds($data['user_id'] ?? ''); $groupName = $data['group_name'] ?? ''; $userName = $data['user_name'] ?? ''; $extData = $data['ext_data'] ?? null; - \think\Log::error("[WmsNotify] reply 入站: type={$type} ext_id={$extId} group={$groupId} user={$userId} content={$content}"); + \think\Log::error("[WmsNotify] reply 入站: type={$type} ext_id={$extId} group={$groupId} user=" . json_encode($userIds, JSON_UNESCAPED_UNICODE) . " content={$content}"); // ---- 根据 ext_data.source 判断消息来源 ---- $source = is_array($extData) ? ($extData['source'] ?? 'wx') : 'wx'; @@ -66,26 +66,56 @@ class ReplyHandler // ---- 根据 ext_data.source 判断消息来源,发送回复 ---- if ($groupId && $content) { - $this->sendToGroup($groupId, $content, $userId, $source); + $this->sendToGroup($groupId, $content, $userIds, $source); } return ['code' => 200, 'msg' => 'ok', 'data' => null]; } /** - * 发送回复消息到 IM 群 + * 解析 user_id 为数组 + * + * 支持以下形式: + * - 数组: ['U001', 'U002'] + * - 逗号/空格/分号等分隔字符串: 'U001,U002'、'U001,U002' + * - 单个字符串: 'U001' + * - 纯数字: 12345(JSON 未加引号时) + * 其他类型(null / 对象等)返回空数组。 + * + * @param mixed $userId + * @return array 去空去重后的用户 ID 列表(保持顺序) */ - private function sendToGroup($groupId, $content, $userId, $source) + private static function parseUserIds($userId) + { + if (is_array($userId)) { + $list = $userId; + } elseif (is_string($userId) && trim($userId) !== '') { + $list = preg_split('/[,,、;;|\s]+/u', trim($userId)); + } elseif (is_numeric($userId)) { + // 纯数字用户 ID(JSON 未加引号时) + $list = [strval($userId)]; + } else { + $list = []; + } + + $list = array_map('strval', (array) $list); + $list = array_filter($list, function ($v) { + return $v !== ''; + }); + + return array_values(array_unique($list)); + } + + /** + * 发送回复消息到 IM 群 + * + * 多 @ 兼容两端消费协议:atid 使用逗号拼接多个用户 ID,单条入队。 + * - wx 端消费程序原生支持逗号分隔多 atid + * - qiwe 端消费时拆分为多个 subtype:1 项逐个 @ + */ + private function sendToGroup($groupId, $content, array $userIds, $source) { try { - $msg = [ - 'method' => 'sendText', - 'wxid' => $groupId, - 'msg' => $content, - 'atid' => $userId ?: '', - 'pid' => 0, - ]; - $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('qwe2019@'); @@ -93,9 +123,17 @@ class ReplyHandler // 根据 ext_data.source 选择对应队列 $msgKey = ($source === 'qiwe') ? 'QiweMessage' : 'Message'; + + $msg = [ + 'method' => 'sendText', + 'wxid' => $groupId, + 'msg' => $content, + 'atid' => implode(',', $userIds), + 'pid' => 0, + ]; $redis->lpush($msgKey, json_encode($msg)); - \think\Log::info("[WmsNotify] reply 已发送: source={$source} group={$groupId} key={$msgKey}"); + \think\Log::info("[WmsNotify] reply 已发送: source={$source} group={$groupId} key={$msgKey} atUsers=" . json_encode($userIds, JSON_UNESCAPED_UNICODE)); } catch (\Exception $e) { \think\Log::error("[WmsNotify] reply 发送失败: " . $e->getMessage()); }