diff --git a/components/uni-im-msg-list/uni-im-msg-list.vue b/components/uni-im-msg-list/uni-im-msg-list.vue index 6fa0b3961e5c208b28761774ba693166d482c666..7921f398755d26524d80c5951db434b2ddb63c90 100644 --- a/components/uni-im-msg-list/uni-im-msg-list.vue +++ b/components/uni-im-msg-list/uni-im-msg-list.vue @@ -562,8 +562,11 @@ longpressMsgAvatar(e) { this.$emit('longpressMsgAvatar', e) }, - retriesSendMsg(e) { - this.$emit('retriesSendMsg', e) + retriesSendMsg(msg) { + if(msg.state === 0){ + return console.error('消息发送中') + } + this.$emit('retriesSendMsg', msg) }, clickItem() { this.$emit('clickItem') diff --git a/pages/chat/chat.vue b/pages/chat/chat.vue index 875ba63e395bf7b0ac6b7d167c6d8c7ae37395e9..8f3c7629715208125c1c8a1512aa7cb1eb1adfa9 100644 --- a/pages/chat/chat.vue +++ b/pages/chat/chat.vue @@ -476,18 +476,23 @@ data[fileType] = fileInfo let msg = await this.beforeSendMsg(data,false) // console.log('~~~beforeSendMsg',msg); - try{ + const uploadFileFn = async ()=>{ const result = await uniCloud.uploadFile({ filePath: tempFile.path, cloudPath: Date.now() + this.current_uid + '.' + name.split('.').pop(), }); // console.log('result.fileID',result.fileID); msg.body.url = result.fileID + } + try{ + await uploadFileFn() this.sendMsg(msg) }catch(e){ console.error('uploadFile error:',e) + // 重发之前增加先上传图片的逻辑 + msg.__beforeRetriesAction = uploadFileFn + msg.state = -200 } - }); }, async chooseFileSendMsg(type,_config={}) { @@ -632,41 +637,51 @@ "rich-text":HtmlArr },false) - // 上传消息中的图片 - let promiseArr = [] - msg.body.forEach(async item=>{ - if(item.name === 'img'){ - promiseArr.push(new Promise((resolve,reject)=>{ - uni.getImageInfo({ - src:item.attrs.src, - success:res=>{ - item.attrs.width = res.width - item.attrs.height = res.height - resolve() - }, - fail:reject - }); - })) - - if(item.attrs.src.indexOf('blob:http') === 0){ + const uploadFileFn = async ()=>{ + // 上传消息中的图片 + let promiseArr = [] + msg.body.forEach(async item=>{ + if(item.name === 'img'){ promiseArr.push(new Promise((resolve,reject)=>{ - uniCloud.uploadFile({ - filePath: item.attrs.src, - cloudPath: Date.now() + this.current_uid + '.' + name.split('.').pop(), - }).then(res=>{ - item.attrs.src = res.fileID - // console.log('上传成功',res); - resolve() - }).catch(e=>{ - reject() - }) + uni.getImageInfo({ + src:item.attrs.src, + success:res=>{ + item.attrs.width = res.width + item.attrs.height = res.height + resolve() + }, + fail:reject + }); })) + + if(item.attrs.src.indexOf('blob:http') === 0){ + promiseArr.push(new Promise((resolve,reject)=>{ + uniCloud.uploadFile({ + filePath: item.attrs.src, + cloudPath: Date.now() + this.current_uid + '.' + name.split('.').pop(), + }).then(res=>{ + item.attrs.src = res.fileID + // console.log('上传成功',res); + resolve() + }).catch(e=>{ + reject() + }) + })) + } } - } - }) - await Promise.all(promiseArr) - // 执行发送 - this.sendMsg(msg) + }) + await Promise.all(promiseArr) + } + try{ + await uploadFileFn() + // 执行发送 + this.sendMsg(msg) + }catch(e){ + // 重发之前增加先上传图片的逻辑 + msg.__beforeRetriesAction = uploadFileFn + msg.state = -200 + } + }else{ // 把this.chatInputContent中的 变成空格,再把头尾的空格去掉 this.chatInputContent = this.chatInputContent.replace(/ /g, ' ').trim() @@ -787,10 +802,16 @@ } }); }, - retriesSendMsg(msg) { + async retriesSendMsg(msg) { uni.showLoading({ mask: true }); + msg.state = 0 + // 检查文件是否上传成功 + if(msg.__beforeRetriesAction){ + await msg.__beforeRetriesAction() + } + delete msg.__beforeRetriesAction // console.log('retriesSendMsg', msg); msg.isRetries = true this.sendMsg(msg, e => {