From eb39105b0c7c2df74a0e7fdcbb7e7a260fbfd720 Mon Sep 17 00:00:00 2001 From: linju Date: Mon, 11 Nov 2024 16:41:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BC=B1=E7=BD=91=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=90=AB=E5=9B=BE=E7=89=87=E7=AD=89=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E4=B8=8A=E4=BC=A0=E5=A4=B1=E8=B4=A5=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E9=87=8D=E5=8F=91=E5=90=8E=E6=8E=A5=E6=94=B6?= =?UTF-8?q?=E6=96=B9=E6=94=B6=E5=88=B0=E7=9A=84=E6=B6=88=E6=81=AF=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-im-msg-list/uni-im-msg-list.vue | 7 +- pages/chat/chat.vue | 91 ++++++++++++------- 2 files changed, 61 insertions(+), 37 deletions(-) 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 6fa0b39..7921f39 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 875ba63..8f3c762 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 => { -- GitLab