diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java index a4f68d0..311936e 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java @@ -109,7 +109,7 @@ public class GroupMessageServiceImpl extends ServiceImpl { - let info = { - type: this.chat.type, - targetId: file.raw.targetId, - msgId : id, - fileId: file.raw.uid, - content: JSON.stringify(res.data), - loadStatus: "ok" - } - this.$store.commit("handleFileUpload", info); + msgInfo.loadStatus = 'ok'; + msgInfo.id = id; + this.$store.commit("insertMessage", msgInfo); }) }, handleImageFail(res, file) { - let info = { - type: this.chat.type, - targetId: file.raw.targetId, - fileId: file.raw.uid, - loadStatus: "fail" - } - this.$store.commit("handleFileUpload", info); + let msgInfo = JSON.parse(JSON.stringify(file.raw.msgInfo)); + msgInfo.loadStatus = 'fail'; + this.$store.commit("insertMessage", msgInfo); }, handleImageBefore(file) { let url = URL.createObjectURL(file); @@ -153,8 +137,8 @@ this.$store.commit("insertMessage", msgInfo); // 滚动到底部 this.scrollToBottom(); - // 借助file对象保存对方id - file.targetId = this.chat.targetId; + // 借助file对象保存 + file.msgInfo = msgInfo; }, handleFileSuccess(res, file) { let data = { @@ -162,36 +146,22 @@ size: file.size, url: res.data } - let msgInfo = { - content: JSON.stringify(data), - type: 2 - } - // 填充对方id - this.setTargetId(msgInfo, this.chat.targetId); + let msgInfo = JSON.parse(JSON.stringify(file.raw.msgInfo)); + msgInfo.content = JSON.stringify(data); this.$http({ url: this.messageAction, method: 'post', data: msgInfo }).then((id) => { - let info = { - type: this.chat.type, - targetId: file.raw.targetId, - fileId: file.raw.uid, - msgId : id, - content: JSON.stringify(data), - loadStatus: "ok" - } - this.$store.commit("handleFileUpload", info); + msgInfo.loadStatus = 'ok'; + msgInfo.id = id; + this.$store.commit("insertMessage", msgInfo); }) }, handleFileFail(res, file) { - let info = { - type: this.chat.type, - targetId: file.raw.targetId, - fileId: file.raw.uid, - loadStatus: "fail" - } - this.$store.commit("handleFileUpload", info); + let msgInfo = JSON.parse(JSON.stringify(file.raw.msgInfo)); + msgInfo.loadStatus = 'fail'; + this.$store.commit("insertMessage", msgInfo); }, handleFileBefore(file) { let url = URL.createObjectURL(file); @@ -202,7 +172,6 @@ } let msgInfo = { id: 0, - fileId: file.uid, sendId: this.mine.id, content: JSON.stringify(data), sendTime: new Date().getTime(), @@ -216,8 +185,8 @@ this.$store.commit("insertMessage", msgInfo); // 滚动到底部 this.scrollToBottom(); - // 借助file对象 - file.targetId = this.chat.targetId; + // 借助file对象透传 + file.msgInfo = msgInfo; }, handleCloseSide() { this.showSide = false; @@ -413,7 +382,6 @@ messageAction() { return `/message/${this.chat.type.toLowerCase()}/send`; } - }, watch: { chat: { diff --git a/im-ui/src/store/chatStore.js b/im-ui/src/store/chatStore.js index 8170872..ed86801 100644 --- a/im-ui/src/store/chatStore.js +++ b/im-ui/src/store/chatStore.js @@ -100,13 +100,18 @@ export default { if(msgInfo.selfSend){ chat.unreadCount=0; } - console.log(msgInfo); // 如果是已存在消息,则覆盖旧的消息数据 for (let idx in chat.messages) { if(msgInfo.id && chat.messages[idx].id == msgInfo.id){ Object.assign(chat.messages[idx], msgInfo); return; } + // 正在发送中的消息可能没有id,通过发送时间判断 + if(msgInfo.selfSend && chat.messages[idx].selfSend + && chat.messages[idx].sendTime == msgInfo.sendTime){ + Object.assign(chat.messages[idx], msgInfo); + return; + } } // 新的消息 chat.messages.push(msgInfo); @@ -131,25 +136,14 @@ export default { chat.messages.splice(idx, 1); break; } - // 没有发送成功的,根据发送时间删除 - if(!chat.messages[idx].id && chat.messages[idx].sendTime == msgInfo.sendTime){ + // 正在发送中的消息可能没有id,根据发送时间删除 + if(msgInfo.selfSend && chat.messages[idx].selfSend + &&chat.messages[idx].sendTime == msgInfo.sendTime){ chat.messages.splice(idx, 1); break; } } }, - handleFileUpload(state, info) { - // 文件上传后数据更新 - let chat = state.chats.find((c) => c.type==info.type && c.targetId === info.targetId); - let msg = chat.messages.find((m) => info.fileId == m.fileId); - msg.loadStatus = info.loadStatus; - if (info.content) { - msg.content = info.content; - } - if(info.msgId){ - msg.id = info.msgId; - } - }, updateChatFromFriend(state, friend) { for (let i in state.chats) { let chat = state.chats[i];