Browse Source

优化加载消息效率

master
xie.bx 2 years ago
parent
commit
410e459761
  1. 5
      im-ui/src/view/Chat.vue
  2. 10
      im-uniapp/App.vue
  3. 37
      im-uniapp/store/chatStore.js
  4. 1
      im-uniapp/store/friendStore.js

5
im-ui/src/view/Chat.vue

@ -8,6 +8,7 @@
</div> </div>
<div class="l-chat-loadding" v-if="loading" v-loading="true" element-loading-text="消息接收中..." <div class="l-chat-loadding" v-if="loading" v-loading="true" element-loading-text="消息接收中..."
element-loading-spinner="el-icon-loading" element-loading-background="#eee"> element-loading-spinner="el-icon-loading" element-loading-background="#eee">
<div class="chat-loading-box"></div>
</div> </div>
<el-scrollbar class="l-chat-list"> <el-scrollbar class="l-chat-list">
<div v-for="(chat,index) in chatStore.chats" :key="index"> <div v-for="(chat,index) in chatStore.chats" :key="index">
@ -96,6 +97,10 @@
.l-chat-loadding{ .l-chat-loadding{
height: 50px; height: 50px;
background-color: #eee; background-color: #eee;
.chat-loading-box{
height: 100%;
}
} }
.l-friend-ist { .l-friend-ist {

10
im-uniapp/App.vue

@ -58,6 +58,10 @@
this.exit(); this.exit();
} else if (res.code != 3000) { } else if (res.code != 3000) {
// //
uni.showToast({
title: '连接已断开,尝试重新连接...',
icon: 'none',
})
wsApi.connect(); wsApi.connect();
} }
}) })
@ -66,7 +70,7 @@
store.commit("loadingPrivateMsg", true) store.commit("loadingPrivateMsg", true)
http({ http({
url: "/message/private/loadMessage?minId=" + minId, url: "/message/private/loadMessage?minId=" + minId,
method: 'get' method: 'GET'
}).then((msgInfos) => { }).then((msgInfos) => {
msgInfos.forEach((msgInfo) => { msgInfos.forEach((msgInfo) => {
msgInfo.selfSend = msgInfo.sendId == store.state.userStore.userInfo.id; msgInfo.selfSend = msgInfo.sendId == store.state.userStore.userInfo.id;
@ -76,6 +80,7 @@
this.insertPrivateMessage(friend,msgInfo); this.insertPrivateMessage(friend,msgInfo);
} }
}) })
store.commit("refreshChats");
if (msgInfos.length == 100) { if (msgInfos.length == 100) {
// //
this.loadPrivateMessage(msgInfos[99].id); this.loadPrivateMessage(msgInfos[99].id);
@ -88,7 +93,7 @@
store.commit("loadingGroupMsg", true) store.commit("loadingGroupMsg", true)
http({ http({
url: "/message/group/loadMessage?minId=" + minId, url: "/message/group/loadMessage?minId=" + minId,
method: 'get' method: 'GET'
}).then((msgInfos) => { }).then((msgInfos) => {
msgInfos.forEach((msgInfo) => { msgInfos.forEach((msgInfo) => {
msgInfo.selfSend = msgInfo.sendId == store.state.userStore.userInfo.id; msgInfo.selfSend = msgInfo.sendId == store.state.userStore.userInfo.id;
@ -98,6 +103,7 @@
this.insertGroupMessage(group,msgInfo); this.insertGroupMessage(group,msgInfo);
} }
}) })
store.commit("refreshChats");
if (msgInfos.length == 100) { if (msgInfos.length == 100) {
// //
this.loadGroupMessage(msgInfos[99].id); this.loadGroupMessage(msgInfos[99].id);

37
im-uniapp/store/chatStore.js

@ -79,7 +79,6 @@ export default {
if (state.chats[idx].type == 'PRIVATE' && if (state.chats[idx].type == 'PRIVATE' &&
state.chats[idx].targetId == friendId) { state.chats[idx].targetId == friendId) {
state.chats[idx].messages.forEach((m) => { state.chats[idx].messages.forEach((m) => {
console.log("readedMessage")
if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) { if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) {
m.status = MESSAGE_STATUS.READED m.status = MESSAGE_STATUS.READED
} }
@ -128,7 +127,9 @@ export default {
break; break;
} }
} }
// 插入新的数据
// 会话列表内容
if(!state.loadingPrivateMsg && !state.loadingPrivateMsg){
if (msgInfo.type == MESSAGE_TYPE.IMAGE) { if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
chat.lastContent = "[图片]"; chat.lastContent = "[图片]";
} else if (msgInfo.type == MESSAGE_TYPE.FILE) { } else if (msgInfo.type == MESSAGE_TYPE.FILE) {
@ -139,6 +140,7 @@ export default {
chat.lastContent = msgInfo.content; chat.lastContent = msgInfo.content;
} }
chat.lastSendTime = msgInfo.sendTime; chat.lastSendTime = msgInfo.sendTime;
}
// 未读加1 // 未读加1
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) {
chat.unreadCount++; chat.unreadCount++;
@ -234,6 +236,29 @@ export default {
loadingGroupMsg(state, loadding) { loadingGroupMsg(state, loadding) {
state.loadingGroupMsg = loadding; state.loadingGroupMsg = loadding;
}, },
refreshChats(state){
state.chats.forEach((chat)=>{
if(chat.messages.length>0){
let msgInfo = chat.messages[chat.messages.length-1];
if (msgInfo.type == MESSAGE_TYPE.IMAGE) {
chat.lastContent = "[图片]";
} else if (msgInfo.type == MESSAGE_TYPE.FILE) {
chat.lastContent = "[文件]";
} else if (msgInfo.type == MESSAGE_TYPE.AUDIO) {
chat.lastContent = "[语音]";
} else {
chat.lastContent = msgInfo.content;
}
chat.lastSendTime = msgInfo.sendTime;
}else{
chat.lastContent = "";
chat.lastSendTime = new Date()
}
})
state.chats.sort((chat1, chat2) => {
return chat2.lastSendTime-chat1.lastSendTime;
});
},
saveToStorage(state) { saveToStorage(state) {
let userId = userStore.state.userInfo.id; let userId = userStore.state.userInfo.id;
let key = "chats-" + userId; let key = "chats-" + userId;
@ -242,10 +267,10 @@ export default {
groupMsgMaxId: state.groupMsgMaxId, groupMsgMaxId: state.groupMsgMaxId,
chats: state.chats chats: state.chats
} }
uni.setStorage({ // uni.setStorage({
key: key, // key: key,
data: chatsData // data: chatsData
}) // })
}, },
clear(state) { clear(state) {
state.chats = []; state.chats = [];

1
im-uniapp/store/friendStore.js

@ -36,7 +36,6 @@ export default {
state.friends.forEach((f) => { state.friends.forEach((f) => {
let userTerminal = onlineTerminals.find((o) => f.id == o.userId); let userTerminal = onlineTerminals.find((o) => f.id == o.userId);
if (userTerminal) { if (userTerminal) {
console.log(userTerminal)
f.online = true; f.online = true;
f.onlineTerminals = userTerminal.terminals; f.onlineTerminals = userTerminal.terminals;
f.onlineWeb = userTerminal.terminals.indexOf(TERMINAL_TYPE.WEB) >= 0 f.onlineWeb = userTerminal.terminals.indexOf(TERMINAL_TYPE.WEB) >= 0

Loading…
Cancel
Save