Переглянути джерело

fix: 同时上传文件失败的bug

xsx 4 місяців тому
батько
коміт
0457e706ee

+ 11 - 2
im-uniapp/pages/chat/chat-box.vue

@@ -152,7 +152,8 @@ export default {
 			isInBottom: true, // 滚动条是否在底部
 			newMessageSize: 0, // 滚动条不在底部时新的消息数量
 			scrollTop: 0, // 用于ios h5定位滚动条
-			scrollViewHeight: 0 // 滚动条总长度
+			scrollViewHeight: 0, // 滚动条总长度
+			maxTmpId: 0 // 最后生成的临时id
 		}
 	},
 	methods: {
@@ -983,7 +984,13 @@ export default {
 		},
 		generateId() {
 			// 生成临时id 
-			return String(new Date().getTime()) + String(Math.floor(Math.random() * 1000));
+			const id = String(new Date().getTime()) + String(Math.floor(Math.random() * 1000));
+			// 必须保证id是递增
+			if (this.maxTmpId > id) {
+				return this.generateId();
+			}
+			this.maxTmpId = id;
+			return id;
 		}
 	},
 	computed: {
@@ -1108,6 +1115,8 @@ export default {
 		// 清空底部标志
 		this.isInBottom = true;
 		this.newMessageSize = 0;
+		// 清空消息临时id
+		this.maxTmpId = 0;
 		// 监听键盘高度
 		this.listenKeyBoard();
 		// 计算聊天窗口高度

+ 18 - 9
im-web/src/components/chat/ChatBox.vue

@@ -132,7 +132,8 @@ export default {
 			reqQueue: [], // 等待发送的请求队列
 			isSending: false, // 是否正在发消息
 			isInBottom: false, // 滚动条是否在底部
-			newMessageSize: 0 // 滚动条不在底部时新的消息数量
+			newMessageSize: 0, // 滚动条不在底部时新的消息数量
+			maxTmpId: 0 // 最后生成的临时ID
 		}
 	},
 	methods: {
@@ -573,7 +574,7 @@ export default {
 				this.$http({
 					url: url,
 					method: 'put'
-				}).then(() => { })
+				}).then(() => {})
 				this.chatStore.resetUnreadCount(this.chat)
 			}
 		},
@@ -724,25 +725,31 @@ export default {
 		getImageSize(file) {
 			return new Promise((resolve, reject) => {
 				const reader = new FileReader();
-				reader.onload = function (event) {
+				reader.onload = function(event) {
 					const img = new Image();
-					img.onload = function () {
+					img.onload = function() {
 						resolve({ width: img.width, height: img.height });
 					};
-					img.onerror = function () {
+					img.onerror = function() {
 						reject(new Error('无法加载图片'));
 					};
 					img.src = event.target.result;
 				};
-				reader.onerror = function () {
+				reader.onerror = function() {
 					reject(new Error('无法读取文件'));
 				};
 				reader.readAsDataURL(file);
 			});
 		},
 		generateId() {
-			// 生成临时id
-			return String(new Date().getTime()) + String(Math.floor(Math.random() * 1000));
+			// 生成临时id 
+			const id = String(new Date().getTime()) + String(Math.floor(Math.random() * 1000));
+			// 必须保证id是递增
+			if (this.maxTmpId > id) {
+				return this.generateId();
+			}
+			this.maxTmpId = id;
+			return id;
 		}
 	},
 	computed: {
@@ -799,7 +806,7 @@ export default {
 		chat: {
 			handler(newChat, oldChat) {
 				if (newChat.targetId > 0 && (!oldChat || newChat.type != oldChat.type ||
-					newChat.targetId != oldChat.targetId)) {
+						newChat.targetId != oldChat.targetId)) {
 					this.userInfo = {}
 					this.group = {};
 					this.groupMembers = [];
@@ -822,6 +829,8 @@ export default {
 					this.resetEditor();
 					// 复位回执消息
 					this.isReceipt = false;
+					// 清空消息临时id
+					this.maxTmpId = 0;
 					// 更新placeholder
 					this.refreshPlaceHolder();
 				}