From 751c130657b4724fac09cbae7740a5b48d37a49c Mon Sep 17 00:00:00 2001 From: yangzhe Date: Fri, 20 Mar 2026 16:38:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(chat):=20=E4=BF=AE=E5=A4=8D=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E6=B6=88=E6=81=AF=E6=8E=92=E5=BA=8F=E4=B8=AD=E5=BE=AE?= =?UTF-8?q?=E7=A7=92=E7=BA=A7=E6=97=B6=E9=97=B4=E6=88=B3=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/chat.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/utils/chat.js b/utils/chat.js index 38243b5..89b70e8 100644 --- a/utils/chat.js +++ b/utils/chat.js @@ -106,7 +106,7 @@ export function formatChatShowTime(sendDate) { */ export function scrollToBottomByContentHeight( vm, - { selector = ".chat-content", extraOffset = 200 } = {} + { selector = ".chat-content", extraOffset = 200 } = {}, ) { if (!vm) return; @@ -154,6 +154,27 @@ export function processChatMessageContent(message) { return message; } +// 解析 sendDate 为微秒级时间戳(防止毫秒级精度丢失) +function getSendDateMicroValue(sendDate) { + const raw = String(sendDate || "").trim(); + if (!raw) return Number.NaN; + const match = raw.match(/^(.+?)(?:\.(\d+))?(Z|[+-]\d{2}:?\d{2})?$/); + if (!match) { + const ms = new Date(raw).getTime(); + return Number.isNaN(ms) ? Number.NaN : ms * 1000; + } + + const base = match[1] || ""; + const fraction = match[2] || ""; + const zone = match[3] || ""; + const baseMs = new Date(`${base}${zone}`).getTime(); + if (Number.isNaN(baseMs)) return Number.NaN; + + const microStr = `${fraction}000000`.slice(0, 6); + const microValue = Number(microStr); + return baseMs * 1000 + microValue; +} + /** * 聊天消息排序:按 `sendDate` 升序;时间相同时,用户消息(`interactMode=0`)排在前面。 * 同时会对每条消息的 `message` 字段进行 `processChatMessageContent` 处理。 @@ -169,8 +190,13 @@ export function sortChatMessages(list = []) { })); return processedList.sort((a, b) => { - const timeA = new Date(a && a.sendDate).getTime(); - const timeB = new Date(b && b.sendDate).getTime(); + const timeA = getSendDateMicroValue(a && a.sendDate); + const timeB = getSendDateMicroValue(b && b.sendDate); + if (Number.isNaN(timeA) && Number.isNaN(timeB)) { + return (a && a.interactMode) - (b && b.interactMode); + } + if (Number.isNaN(timeA)) return 1; + if (Number.isNaN(timeB)) return -1; if (timeA === timeB) return (a && a.interactMode) - (b && b.interactMode); return timeA - timeB; });