fix(chat): 修复聊天消息排序中微秒级时间戳解析问题
This commit is contained in:
parent
8a35f79902
commit
751c130657
|
|
@ -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;
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue