This commit is contained in:
wong
2026-01-19 09:26:55 +08:00
parent 5dfaba943b
commit ee4dcb1b8b

View File

@@ -450,9 +450,11 @@ class MessageController extends BaseController
*/ */
public function saveChatroomMessage($item) public function saveChatroomMessage($item)
{ {
// 检查消息是否已存在 // 检查消息是否已存在(必须指定 type=2 表示群聊消息)
$exists = WechatMessageModel::where('id', $item['id'])->find(); $exists = WechatMessageModel::where(['id' => $item['id'], 'type' => 2])->find();
// 如果消息已存在且 sendStatus == 0已发送则跳过更新
// 注意这里只跳过已发送的消息未发送的消息sendStatus != 0仍然需要更新
if (!empty($exists) && $exists['sendStatus'] == 0){ if (!empty($exists) && $exists['sendStatus'] == 0){
return true; return true;
} }
@@ -503,16 +505,29 @@ class MessageController extends BaseController
'recallId' => $item['recallId'] ?? false 'recallId' => $item['recallId'] ?? false
]; ];
// 创建新记录 // 创建或更新记录
try { try {
if(empty($exists)){ if(empty($exists)){
WechatMessageModel::create($data); // 新记录,直接创建
$result = WechatMessageModel::create($data);
if (!$result) {
throw new \Exception('创建群聊消息记录失败');
}
}else{ }else{
// 已存在记录,更新(排除 id 字段)
unset($data['id']); unset($data['id']);
$exists->save($data); $result = $exists->save($data);
if ($result === false) {
throw new \Exception('更新群聊消息记录失败');
}
} }
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
// 记录错误日志,便于调试
\think\facade\Log::error('保存群聊消息失败:' . $e->getMessage(), [
'message_id' => $item['id'] ?? '',
'data' => $data ?? []
]);
return false; return false;
} }
} }