Jelajahi Sumber

优化加载消息效率

xie.bx 2 tahun lalu
induk
melakukan
410e459761
4 mengubah file dengan 53 tambahan dan 18 penghapusan
  1. 5 0
      im-ui/src/view/Chat.vue
  2. 8 2
      im-uniapp/App.vue
  3. 40 15
      im-uniapp/store/chatStore.js
  4. 0 1
      im-uniapp/store/friendStore.js

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

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

+ 8 - 2
im-uniapp/App.vue

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

+ 40 - 15
im-uniapp/store/chatStore.js

@@ -79,7 +79,6 @@ export default {
 				if (state.chats[idx].type == 'PRIVATE' &&
 					state.chats[idx].targetId == friendId) {
 					state.chats[idx].messages.forEach((m) => {
-						console.log("readedMessage")
 						if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) {
 							m.status = MESSAGE_STATUS.READED
 						}
@@ -128,17 +127,20 @@ export default {
 					break;
 				}
 			}
-			// 插入新的数据
-			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;
+			
+			// 会话列表内容
+			if(!state.loadingPrivateMsg && !state.loadingPrivateMsg){
+				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;
 			}
-			chat.lastSendTime = msgInfo.sendTime;
 			// 未读加1
 			if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) {
 				chat.unreadCount++;
@@ -234,6 +236,29 @@ export default {
 		loadingGroupMsg(state, 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) {
 			let userId = userStore.state.userInfo.id;
 			let key = "chats-" + userId;
@@ -242,10 +267,10 @@ export default {
 				groupMsgMaxId: state.groupMsgMaxId,
 				chats: state.chats
 			}
-			uni.setStorage({
-				key: key,
-				data: chatsData
-			})
+			// uni.setStorage({
+			// 	key: key,
+			// 	data: chatsData
+			// })
 		},
 		clear(state) {
 			state.chats = [];

+ 0 - 1
im-uniapp/store/friendStore.js

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