|
|
@ -677,25 +677,49 @@ function sendEnhancedMessage(messageData) { |
|
|
// 如果是create_conversation消息,使用userId+managerId+timestamp生成临时ID
|
|
|
// 如果是create_conversation消息,使用userId+managerId+timestamp生成临时ID
|
|
|
if (actualMessageData.type === 'create_conversation') { |
|
|
if (actualMessageData.type === 'create_conversation') { |
|
|
actualMessageData.messageId = `create_${actualMessageData.userId}_${actualMessageData.managerId}_${actualMessageData.timestamp}`; |
|
|
actualMessageData.messageId = `create_${actualMessageData.userId}_${actualMessageData.managerId}_${actualMessageData.timestamp}`; |
|
|
} else { |
|
|
} |
|
|
|
|
|
// 如果是get_messages消息,使用特殊格式生成ID,避免重复发送
|
|
|
|
|
|
else if (actualMessageData.type === 'get_messages') { |
|
|
|
|
|
actualMessageData.messageId = `getmsg_${actualMessageData.conversationId || actualMessageData.targetUserId || 'unknown'}_${Date.now()}`; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
// 其他消息类型生成随机ID
|
|
|
// 其他消息类型生成随机ID
|
|
|
actualMessageData.messageId = `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; |
|
|
actualMessageData.messageId = `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 消息去重检查
|
|
|
// 【关键修复】对于get_messages消息,不进行去重检查,确保每次都能获取最新消息
|
|
|
if (!shouldSendMessage(actualMessageData.messageId)) { |
|
|
if (actualMessageData.type !== 'get_messages') { |
|
|
return false; |
|
|
// 消息去重检查
|
|
|
|
|
|
if (!shouldSendMessage(actualMessageData.messageId)) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
// 确保包含必要字段
|
|
|
// 确保包含必要字段
|
|
|
if (actualMessageData.type === 'chat_message') { |
|
|
if (actualMessageData.type === 'chat_message') { |
|
|
if (!actualMessageData.userId || !actualMessageData.managerId) { |
|
|
// 客服消息不需要userId,只需要managerId
|
|
|
|
|
|
const isManager = actualMessageData.userType === 'manager' || actualMessageData.manager_mode; |
|
|
|
|
|
if (!isManager && (!actualMessageData.userId || !actualMessageData.managerId)) { |
|
|
console.error('[WebSocket] 聊天消息缺少必要的userId或managerId字段'); |
|
|
console.error('[WebSocket] 聊天消息缺少必要的userId或managerId字段'); |
|
|
updateMessageStatus(actualMessageData.messageId, MESSAGE_FAILED); |
|
|
updateMessageStatus(actualMessageData.messageId, MESSAGE_FAILED); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
// 客服消息只需要managerId
|
|
|
|
|
|
if (isManager && !actualMessageData.managerId) { |
|
|
|
|
|
console.error('[WebSocket] 客服消息缺少必要的managerId字段'); |
|
|
|
|
|
updateMessageStatus(actualMessageData.messageId, MESSAGE_FAILED); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 【关键修复】确保消息使用正确的字段名
|
|
|
|
|
|
if (actualMessageData.userId && !actualMessageData.sender_id) { |
|
|
|
|
|
actualMessageData.sender_id = actualMessageData.userId; |
|
|
|
|
|
} |
|
|
|
|
|
if (actualMessageData.targetUserId && !actualMessageData.receiver_id) { |
|
|
|
|
|
actualMessageData.receiver_id = actualMessageData.targetUserId; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 设置消息状态为发送中
|
|
|
// 设置消息状态为发送中
|
|
|
@ -714,11 +738,12 @@ function sendEnhancedMessage(messageData) { |
|
|
|
|
|
|
|
|
// 设置消息发送超时检测
|
|
|
// 设置消息发送超时检测
|
|
|
setTimeout(() => { |
|
|
setTimeout(() => { |
|
|
if (messageStatus.get(actualMessageData.messageId) === MESSAGE_SENDING) { |
|
|
const status = messageStatus.get(actualMessageData.messageId); |
|
|
|
|
|
if (status === MESSAGE_SENDING) { |
|
|
console.warn(`[WebSocket] 消息 ${actualMessageData.messageId} 发送超时,可能需要重试`); |
|
|
console.warn(`[WebSocket] 消息 ${actualMessageData.messageId} 发送超时,可能需要重试`); |
|
|
updateMessageStatus(actualMessageData.messageId, MESSAGE_FAILED); |
|
|
updateMessageStatus(actualMessageData.messageId, MESSAGE_FAILED); |
|
|
} |
|
|
} |
|
|
}, 5000); // 5秒超时
|
|
|
}, 10000); // 增加超时时间到10秒,确保历史消息请求有足够时间返回
|
|
|
|
|
|
|
|
|
return true; |
|
|
return true; |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
|