feat: 优化常用语发送逻辑,添加图片发送确认提示及日志记录
This commit is contained in:
parent
db1df052eb
commit
371801a758
@ -452,41 +452,39 @@ const handleCategoryClick = (category) => {
|
|||||||
currentCategory.value = category.id;
|
currentCategory.value = category.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
const confirmSendImages = (count) => {
|
const sendPhrase = (phrase) => {
|
||||||
return new Promise((resolve) => {
|
|
||||||
uni.showModal({
|
|
||||||
title: "提示",
|
|
||||||
content: `该常用语包含${count}张图片,是否一并发送?`,
|
|
||||||
cancelText: "不发送图片",
|
|
||||||
confirmText: "发送图片",
|
|
||||||
success: (res) => resolve(Boolean(res.confirm)),
|
|
||||||
fail: () => resolve(false),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const sendPhrase = async (phrase) => {
|
|
||||||
const pages = getCurrentPages();
|
const pages = getCurrentPages();
|
||||||
const prevPage = pages[pages.length - 2];
|
const prevPage = pages[pages.length - 2];
|
||||||
const files = normalizeFiles(phrase.files);
|
const files = normalizeFiles(phrase.files);
|
||||||
const shouldSendImages = files.length > 0 ? await confirmSendImages(files.length) : false;
|
console.log("[common-phrases] sendPhrase click", {
|
||||||
|
phraseId: phrase?.id,
|
||||||
|
rawFiles: phrase?.files,
|
||||||
|
normalizedFiles: files,
|
||||||
|
pageCount: pages.length,
|
||||||
|
hasPrevPage: Boolean(prevPage),
|
||||||
|
hasSendCommonPhrase: Boolean(prevPage?.$vm?.sendCommonPhrase),
|
||||||
|
});
|
||||||
const phraseToSend = {
|
const phraseToSend = {
|
||||||
...phrase,
|
...phrase,
|
||||||
files: shouldSendImages ? files : [],
|
files,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!phraseToSend.content?.trim() && phraseToSend.files.length === 0) {
|
uni.navigateBack({
|
||||||
return;
|
success: () => {
|
||||||
}
|
setTimeout(() => {
|
||||||
|
if (!prevPage?.$vm?.sendCommonPhrase) return;
|
||||||
if (prevPage) {
|
console.log("[common-phrases] call prevPage.sendCommonPhrase", phraseToSend);
|
||||||
const result = prevPage.$vm.sendCommonPhrase(phraseToSend);
|
const result = prevPage.$vm.sendCommonPhrase(phraseToSend);
|
||||||
if (result && typeof result.then === "function") {
|
if (result && typeof result.then === "function") {
|
||||||
await result;
|
result.then(() => {
|
||||||
}
|
console.log("[common-phrases] prevPage.sendCommonPhrase done", {
|
||||||
}
|
phraseId: phrase?.id,
|
||||||
|
});
|
||||||
uni.navigateBack();
|
});
|
||||||
|
}
|
||||||
|
}, 200);
|
||||||
|
},
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const showAddPhraseDialog = () => {
|
const showAddPhraseDialog = () => {
|
||||||
|
|||||||
@ -208,7 +208,9 @@ const normalizePhraseImageFile = (file) => {
|
|||||||
? file
|
? file
|
||||||
: file?.url || file?.URL || file?.download_url || file?.tempFilePath || file?.path;
|
: file?.url || file?.URL || file?.download_url || file?.tempFilePath || file?.path;
|
||||||
|
|
||||||
|
console.log("[chat-input] normalizePhraseImageFile start", { file, imageUrl });
|
||||||
if (!imageUrl) {
|
if (!imageUrl) {
|
||||||
|
console.log("[chat-input] normalizePhraseImageFile missing imageUrl", { file });
|
||||||
resolve(null);
|
resolve(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -229,32 +231,45 @@ const normalizePhraseImageFile = (file) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (/^https?:\/\//.test(imageUrl)) {
|
if (/^https?:\/\//.test(imageUrl)) {
|
||||||
|
console.log("[chat-input] download phrase image", { imageUrl });
|
||||||
uni.downloadFile({
|
uni.downloadFile({
|
||||||
url: imageUrl,
|
url: imageUrl,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
|
console.log("[chat-input] download phrase image success", res);
|
||||||
if (res.statusCode === 200 && res.tempFilePath) {
|
if (res.statusCode === 200 && res.tempFilePath) {
|
||||||
resolve(makeImageFile(res.tempFilePath, file?.size || 0));
|
const imageFile = makeImageFile(res.tempFilePath, file?.size || 0);
|
||||||
|
console.log("[chat-input] normalized downloaded phrase image", imageFile);
|
||||||
|
resolve(imageFile);
|
||||||
} else {
|
} else {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: () => resolve(null),
|
fail: (error) => {
|
||||||
|
console.log("[chat-input] download phrase image fail", error);
|
||||||
|
resolve(null);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(makeImageFile(imageUrl, file?.size || 0));
|
const imageFile = makeImageFile(imageUrl, file?.size || 0);
|
||||||
|
console.log("[chat-input] normalized local phrase image", imageFile);
|
||||||
|
resolve(imageFile);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendImageMessageFromPhrase = async (file) => {
|
const sendImageMessageFromPhrase = async (file) => {
|
||||||
|
console.log("[chat-input] sendImageMessageFromPhrase start", { file });
|
||||||
const imageFile = await normalizePhraseImageFile(file);
|
const imageFile = await normalizePhraseImageFile(file);
|
||||||
if (!imageFile) {
|
if (!imageFile) {
|
||||||
|
console.log("[chat-input] sendImageMessageFromPhrase normalize failed", { file });
|
||||||
uni.showToast({ title: "图片发送失败", icon: "none" });
|
uni.showToast({ title: "图片发送失败", icon: "none" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("[chat-input] sendImageMessageFromPhrase call sendImageMessage", imageFile);
|
||||||
await sendImageMessage(imageFile);
|
await sendImageMessage(imageFile);
|
||||||
|
console.log("[chat-input] sendImageMessageFromPhrase done", imageFile);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 设置输入框文本(覆盖原内容)
|
// 设置输入框文本(覆盖原内容)
|
||||||
|
|||||||
@ -993,6 +993,7 @@ onHide(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const sendCommonPhrase = async (phraseOrContent) => {
|
const sendCommonPhrase = async (phraseOrContent) => {
|
||||||
|
console.log("[message-index] sendCommonPhrase start", phraseOrContent);
|
||||||
if (!chatInputRef.value) return;
|
if (!chatInputRef.value) return;
|
||||||
|
|
||||||
const content =
|
const content =
|
||||||
@ -1000,14 +1001,51 @@ const sendCommonPhrase = async (phraseOrContent) => {
|
|||||||
? phraseOrContent
|
? phraseOrContent
|
||||||
: phraseOrContent?.content || "";
|
: phraseOrContent?.content || "";
|
||||||
const files = Array.isArray(phraseOrContent?.files) ? phraseOrContent.files : [];
|
const files = Array.isArray(phraseOrContent?.files) ? phraseOrContent.files : [];
|
||||||
|
console.log("[message-index] sendCommonPhrase parsed", {
|
||||||
|
hasChatInputRef: Boolean(chatInputRef.value),
|
||||||
|
contentLength: content.length,
|
||||||
|
files,
|
||||||
|
fileCount: files.length,
|
||||||
|
});
|
||||||
|
|
||||||
if (content.trim()) {
|
if (content.trim()) {
|
||||||
await chatInputRef.value.sendTextMessageFromPhrase(content);
|
console.log("[message-index] sendCommonPhrase set input text");
|
||||||
|
chatInputRef.value.setInputText(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const file of files) {
|
if (files.length === 0) {
|
||||||
|
console.log("[message-index] sendCommonPhrase done without images");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shouldSendImages = await new Promise((resolve) => {
|
||||||
|
console.log("[message-index] confirm phrase images open", { count: files.length });
|
||||||
|
uni.showModal({
|
||||||
|
title: "提示",
|
||||||
|
content: `该常用语包含${files.length}张图片,是否发送图片?`,
|
||||||
|
cancelText: "不发",
|
||||||
|
confirmText: "发送",
|
||||||
|
success: (res) => {
|
||||||
|
console.log("[message-index] confirm phrase images success", res);
|
||||||
|
resolve(Boolean(res.confirm));
|
||||||
|
},
|
||||||
|
fail: (error) => {
|
||||||
|
console.log("[message-index] confirm phrase images fail", error);
|
||||||
|
resolve(false);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!shouldSendImages) {
|
||||||
|
console.log("[message-index] sendCommonPhrase skip images");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [index, file] of files.entries()) {
|
||||||
|
console.log("[message-index] sendCommonPhrase send image", { index, file });
|
||||||
await chatInputRef.value.sendImageMessageFromPhrase(file);
|
await chatInputRef.value.sendImageMessageFromPhrase(file);
|
||||||
}
|
}
|
||||||
|
console.log("[message-index] sendCommonPhrase done");
|
||||||
};
|
};
|
||||||
|
|
||||||
// 处理流式文本输入
|
// 处理流式文本输入
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user