From f18a8e62ebc1edb19fced1a72e0a9e52f60e244c Mon Sep 17 00:00:00 2001 From: xsx <825657193@qq.com> Date: Sat, 30 Mar 2024 13:49:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E6=96=AD=E7=BA=BF?= =?UTF-8?q?=E9=87=8D=E8=BF=9E=E6=97=B6=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E6=B6=88=E6=81=AF=E4=B9=B1=E5=BA=8F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- im-ui/src/store/chatStore.js | 12 ++++++++++-- im-uniapp/store/chatStore.js | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/im-ui/src/store/chatStore.js b/im-ui/src/store/chatStore.js index 4bc504e..505cc70 100644 --- a/im-ui/src/store/chatStore.js +++ b/im-ui/src/store/chatStore.js @@ -174,8 +174,16 @@ export default { }); chat.lastTimeTip = msgInfo.sendTime; } - // 新的消息 - chat.messages.push(msgInfo); + // 根据id顺序插入,防止消息乱序 + let insertPos = chat.messages.length; + for (let idx in chat.messages) { + if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) { + insertPos = idx; + console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`); + break; + } + } + chat.messages.splice(insertPos, 0, msgInfo); this.commit("saveToStorage"); }, updateMessage(state, msgInfo) { diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index b959ab6..005ec61 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -176,8 +176,16 @@ export default { }); chat.lastTimeTip = msgInfo.sendTime; } - // 新的消息 - chat.messages.push(msgInfo); + // 根据id顺序插入,防止消息乱序 + let insertPos = chat.messages.length; + for (let idx in chat.messages) { + if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) { + insertPos = idx; + console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`); + break; + } + } + chat.messages.splice(insertPos, 0, msgInfo); this.commit("saveToStorage"); }, updateMessage(state, msgInfo) {