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

Loading…
Cancel
Save