liufumingren d617f9ee88 1
2026-08-07 00:37:41 +08:00

92 lines
3.1 KiB
PHP

<?php
namespace app\index\library\new_wms;
use app\index\library\new_wms\func\Inbox;
/**
* 消息转发处理器
*
* 收集群聊消息上下文,通过 inbox.push 推送到 WMS 仓库站内信。
* WMS 异步处理后通过回调/Webhook 返回结果。
*
* 调用方式:
* $forward = new Forward();
* $result = $forward->handle($params, $SOURCE, $text);
* if ($result !== null) return $result; // 跳过本地处理
*/
class Forward
{
/**
* 处理消息转发 → inbox.push
*
* @param array $params 统一消息结构(与 processMessage 入参一致)
* @param string $source 消息来源: 'wx' | 'qiwe'
* @param string $text 已提取的消息文本内容
* @return array|null 返回 null 表示继续本地逻辑
*/
public function handle($params, $source, $text)
{
// ---- 配置未就绪时跳过 ----
if (empty(config('wms.app_key')) || empty(config('wms.app_secret'))) {
return null;
}
// ---- 收集消息上下文,映射到 inbox.push 参数 ----
$data = $params['data'] ?? [];
$fromId = $data['fromid'] ?? '';
$memId = $data['memid'] ?? '';
$memName = $data['memname'] ?? '';
$nickName = $data['nickName'] ?? '';
$msgId = $data['id'] ?? '';
$msgType = $params['type'] ?? 1;
$inboxParams = [
'requestId' => (string) $msgId, // 消息 ID → 幂等去重
'content' => $text, // 消息正文
'groupId' => (string) $fromId, // 群/会话 ID
'groupName' => $nickName, // 群名称
'userId' => (string) $memId, // 发送者 ID
'userName' => $memName, // 发送者名称
'parentId' => 0,
'targetType' => 'im',
'extData' => [
'source' => $source, // wx / qiwe
'type' => $msgType, // 消息类型
],
'flags' => new \stdClass(),
];
// ---- 转发到 WMS ----
try {
$inbox = $this->getInbox();
$result = $inbox->push($inboxParams);
if (!empty($result['data']['ok'])) {
\think\Log::info('[Forward] 已推送站内信: ext_id=' . ($result['data']['ext_id'] ?? '?'));
} else {
\think\Log::error('[Forward] 推送失败: ' . json_encode($result, JSON_UNESCAPED_UNICODE));
}
} catch (\Exception $e) {
\think\Log::error('[Forward] 推送异常: ' . $e->getMessage());
}
// inbox.push 是异步的,始终回退到本地逻辑处理
return null;
}
/**
* 获取 Inbox 实例(懒加载)
* @return Inbox
*/
private function getInbox()
{
static $inbox = null;
if ($inbox === null) {
$inbox = new Inbox();
}
return $inbox;
}
}