142 lines
5.1 KiB
PHP
142 lines
5.1 KiB
PHP
<?php
|
||
|
||
namespace app\index\library\new_wms\notify;
|
||
|
||
/**
|
||
* notify.reply — 消息回复通知
|
||
*
|
||
* WMS 处理入站消息后,将 AI 回复/绑群结果/面单余额推送到外部系统。
|
||
* 根据 data.type 区分:ai / bind / balance / error
|
||
*
|
||
* 回复消息通过 Redis 队列发送到对应的 IM 群聊。
|
||
*/
|
||
class ReplyHandler
|
||
{
|
||
/**
|
||
* 处理 reply 通知
|
||
*
|
||
* @param array $body 请求体 { code, data: { type, content, ext_id, group_id, user_id, ... } }
|
||
* @param array $query URL query 参数
|
||
* @return array
|
||
*/
|
||
public function handle(array $body, array $query)
|
||
{
|
||
$data = $body['data'] ?? [];
|
||
|
||
$type = $data['type'] ?? '';
|
||
$content = $data['content'] ?? '';
|
||
$extId = $data['ext_id'] ?? '';
|
||
$groupId = $data['group_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=" . json_encode($userIds, JSON_UNESCAPED_UNICODE) . " content={$content}");
|
||
|
||
// ---- 根据 ext_data.source 判断消息来源 ----
|
||
$source = is_array($extData) ? ($extData['source'] ?? 'wx') : 'wx';
|
||
|
||
// ---- 检测对应 IM 通道是否在线(放在去重之前,离线时不写去重键,WMS 重试会重新检测) ----
|
||
if ($groupId && $content) {
|
||
$isOnline = ($source === 'qiwe') ? OnlineChecker::qiwe() : OnlineChecker::wx();
|
||
if (!$isOnline) {
|
||
\think\Log::error("[WmsNotify] reply 被拒绝: {$source} 不在线");
|
||
return ['code' => 503, 'msg' => "{$source} offline", 'data' => null];
|
||
}
|
||
}
|
||
|
||
// ---- 幂等去重 ----
|
||
if ($extId) {
|
||
try {
|
||
$redis = new \Redis();
|
||
$redis->connect('127.0.0.1', 6379);
|
||
$redis->auth('qwe2019@');
|
||
$redis->select(3);
|
||
$dedupeKey = "wms_reply:{$extId}";
|
||
if ($redis->exists($dedupeKey)) {
|
||
\think\Log::info("[WmsNotify] reply 重复推送已忽略: ext_id={$extId}");
|
||
return ['code' => 200, 'msg' => 'duplicate', 'data' => null];
|
||
}
|
||
$redis->setex($dedupeKey, 3600, 1);
|
||
} catch (\Exception $e) {
|
||
// Redis 不可用时跳过去重,不阻塞
|
||
}
|
||
}
|
||
|
||
// ---- 根据 ext_data.source 判断消息来源,发送回复 ----
|
||
if ($groupId && $content) {
|
||
$this->sendToGroup($groupId, $content, $userIds, $source);
|
||
}
|
||
|
||
return ['code' => 200, 'msg' => 'ok', 'data' => null];
|
||
}
|
||
|
||
/**
|
||
* 解析 user_id 为数组
|
||
*
|
||
* 支持以下形式:
|
||
* - 数组: ['U001', 'U002']
|
||
* - 逗号/空格/分号等分隔字符串: 'U001,U002'、'U001,U002'
|
||
* - 单个字符串: 'U001'
|
||
* - 纯数字: 12345(JSON 未加引号时)
|
||
* 其他类型(null / 对象等)返回空数组。
|
||
*
|
||
* @param mixed $userId
|
||
* @return array 去空去重后的用户 ID 列表(保持顺序)
|
||
*/
|
||
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 {
|
||
$redis = new \Redis();
|
||
$redis->connect('127.0.0.1', 6379);
|
||
$redis->auth('qwe2019@');
|
||
$redis->select(2);
|
||
|
||
// 根据 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} atUsers=" . json_encode($userIds, JSON_UNESCAPED_UNICODE));
|
||
} catch (\Exception $e) {
|
||
\think\Log::error("[WmsNotify] reply 发送失败: " . $e->getMessage());
|
||
}
|
||
}
|
||
}
|