Browse Source

复制粘贴上传图片完善

master
La123123 4 weeks ago
parent
commit
e2f5263fd6
  1. 52
      im-uniapp/pages/chat/chat-box.vue

52
im-uniapp/pages/chat/chat-box.vue

@ -384,7 +384,7 @@ export default {
method: "post",
data: replyMsgInfo
}).then(res => {
console.log("自动回复发送成功", res);
//console.log("", res);
// ""
this.chatStore.deleteMessage(typingMessage, this.chat);
@ -429,7 +429,7 @@ export default {
// recvId: this.mine.id, //
// type: autoReply.replyType, //
// };
// console.log(replyMsgInfo)
// //console.log(replyMsgInfo)
// //
// if (autoReply.replyType === 0) {
// //
@ -448,7 +448,7 @@ export default {
// method: "post",
// data: replyMsgInfo
// }).then(res => {
// console.log("", res);
// //console.log("", res);
// // WebSocket
// }).catch(err => {
// console.error("", err);
@ -1090,7 +1090,7 @@ export default {
if (window.__isPasting__ === undefined) {
window.__isPasting__ = false;
}
console.log('[chat-box] 首次添加粘贴事件监听器');
//console.log('[chat-box] ');
//
if (this.pasteHandler) {
@ -1117,7 +1117,7 @@ export default {
async onDocumentPaste(e) {
// #ifdef H5
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] 粘贴事件触发, isPasting: ${window.__isPasting__}`);
//console.log(`[${timestamp}] , isPasting: ${window.__isPasting__}`);
// editor
const activeElement = document.activeElement;
@ -1127,13 +1127,13 @@ export default {
);
if (!isInEditor) {
console.log(`[${timestamp}] 不在editor中,忽略`);
//console.log(`[${timestamp}] editor`);
return;
}
// 使
if (window.__isPasting__) {
console.log(`[${timestamp}] 全局isPasting为true,忽略重复触发`);
//console.log(`[${timestamp}] isPastingtrue`);
e.preventDefault();
e.stopPropagation();
return;
@ -1145,7 +1145,7 @@ export default {
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.kind === 'file' && item.type.startsWith('image/')) {
console.log(`[${timestamp}] 检测到图片,开始处理`);
//console.log(`[${timestamp}] `);
e.preventDefault();
e.stopPropagation();
hasImage = true;
@ -1156,7 +1156,7 @@ export default {
// base64
const base64 = await this.fileToBase64(file);
console.log(`[${timestamp}] base64转换完成,长度: ${base64.length}`);
//console.log(`[${timestamp}] base64: ${base64.length}`);
//
const tempFile = {
@ -1169,7 +1169,7 @@ export default {
//
await this.handlePasteImage(tempFile);
} catch (error) {
console.log(`[${timestamp}] 处理粘贴图片失败:`, error);
//console.log(`[${timestamp}] :`, error);
} finally {
window.__isPasting__ = false;
}
@ -1180,7 +1180,7 @@ export default {
//
if (!hasImage) {
console.log(`[${timestamp}] 没有检测到图片`);
//console.log(`[${timestamp}] `);
this.isPasting = false;
}
// #endif
@ -1188,15 +1188,15 @@ export default {
async fileToBase64(file) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] fileToBase64被调用,文件大小:`, file.size);
//console.log(`[${timestamp}] fileToBase64:`, file.size);
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
console.log(`[${timestamp}] fileToBase64 onload,结果长度:`, event.target.result.length);
//console.log(`[${timestamp}] fileToBase64 onload:`, event.target.result.length);
resolve(event.target.result);
};
reader.onerror = (error) => {
console.log(`[${timestamp}] fileToBase64 onerror:`, error);
//console.log(`[${timestamp}] fileToBase64 onerror:`, error);
reject(error);
};
reader.readAsDataURL(file);
@ -1205,11 +1205,11 @@ export default {
async handlePasteImage(file) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] handlePasteImage被调用, isPasting: ${window.__isPasting__}, chat:`, this.chat);
//console.log(`[${timestamp}] handlePasteImage, isPasting: ${window.__isPasting__}, chat:`, this.chat);
// chat
if (!this.chat) {
console.log(`[${timestamp}] handlePasteImage: chat为空,忽略`);
//console.log(`[${timestamp}] handlePasteImage: chat`);
return;
}
@ -1263,14 +1263,14 @@ export default {
await this.uploadPastedImage(file);
} catch (error) {
console.log(`[${timestamp}] 处理粘贴图片失败:`, error);
//console.log(`[${timestamp}] :`, error);
this.isPasting = false;
}
},
async uploadPastedImage(file) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] uploadPastedImage被调用`);
//console.log(`[${timestamp}] uploadPastedImage`);
// #ifdef H5
try {
@ -1293,7 +1293,7 @@ export default {
const formData = new FormData();
formData.append('file', blob, file.name);
console.log(`[${timestamp}] 开始上传图片`);
//console.log(`[${timestamp}] `);
// 使fetch
const response = await fetch(UNI_APP.BASE_URL + `/image/upload?isPermanent=false&thumbSize=50`, {
@ -1302,10 +1302,10 @@ export default {
});
const data = await response.json();
console.log(`[${timestamp}] 上传响应:`, data);
//console.log(`[${timestamp}] :`, data);
if (data.code === 200) {
console.log(`[${timestamp}] 上传成功,准备发送消息`);
//console.log(`[${timestamp}] `);
//
let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
msgInfo.content = JSON.stringify(data.data);
@ -1314,13 +1314,13 @@ export default {
//
const m = await this.sendMessageRequest(msgInfo);
console.log(`[${timestamp}] 消息发送成功`);
//console.log(`[${timestamp}] `);
msgInfo.id = m.id;
msgInfo.status = m.status;
this.isReceipt = false;
this.chatStore.updateMessage(msgInfo, file.chat);
} else {
console.log(`[${timestamp}] 上传失败:`, data);
//console.log(`[${timestamp}] :`, data);
uni.showToast({
icon: "none",
title: data.message || "上传失败",
@ -1330,7 +1330,7 @@ export default {
this.chatStore.updateMessage(msgInfo, file.chat);
}
} catch (error) {
console.log(`[${timestamp}] 上传异常:`, error);
//console.log(`[${timestamp}] :`, error);
uni.showToast({
icon: "none",
title: "上传失败",
@ -1438,7 +1438,7 @@ export default {
if (this.reqQueue.length && !this.isSending) {
this.isSending = true;
const reqData = this.reqQueue.shift();
console.log(reqData.msgInfo);
//console.log(reqData.msgInfo);
this.$http({
url: this.messageAction,
method: "post",
@ -1493,7 +1493,7 @@ export default {
// #ifdef H5
if (navigator.platform == "Win32") {
//
console.log("navigator.platform:", navigator.platform);
//console.log("navigator.platform:", navigator.platform);
return;
}
if (uni.getSystemInfoSync().platform == "ios") {

Loading…
Cancel
Save