feat: 修复发送消息后的键盘处理

This commit is contained in:
2026-05-14 15:20:34 +08:00
parent e453a78212
commit 405be2ca99
2 changed files with 42 additions and 19 deletions

View File

@@ -284,23 +284,31 @@ onUnmounted(() => {
resetUI();
});
const hideKeyboardAfterSend = () => {
isFocused.value = false;
const textarea = textareaRef.value;
if (textarea && typeof textarea.blur === "function") {
textarea.blur();
}
nextTick(() => {
uni.hideKeyboard();
});
};
const sendMessage = () => {
if (props.isSessionActive) {
// 如果会话进行中,调用停止请求函数
if (props.stopRequest) {
props.stopRequest();
}
hideKeyboardAfterSend();
} else {
// 否则发送新消息
if (!inputMessage.value.trim()) return;
emit("send", inputMessage.value);
// 发送后保持焦点(可选)
if (props.holdKeyboard && textareaRef.value) {
nextTick(() => {
textareaRef.value.focus();
});
}
hideKeyboardAfterSend();
}
};
@@ -325,8 +333,9 @@ const handleTouchEnd = () => {
const focusInput = () => {
isFocused.value = true;
nextTick(() => {
if (textareaRef.value) {
textareaRef.value.focus();
const textarea = textareaRef.value;
if (textarea && typeof textarea.focus === "function") {
textarea.focus();
}
});
};
@@ -334,8 +343,10 @@ const focusInput = () => {
// 手动失焦输入框
const blurInput = () => {
isFocused.value = false;
if (textareaRef.value) {
textareaRef.value.blur();
const textarea = textareaRef.value;
if (textarea && typeof textarea.blur === "function") {
textarea.blur();
}
nextTick(() => {

View File

@@ -381,7 +381,10 @@ const hideKeyboardByScroll = () => {
holdKeyboardFlag.value = true;
isKeyboardShow.value = false;
if (inputAreaRef.value && inputAreaRef.value.blurInput) {
if (
inputAreaRef.value &&
typeof inputAreaRef.value.blurInput === "function"
) {
inputAreaRef.value.blurInput();
}
@@ -394,6 +397,21 @@ const hideKeyboardByScroll = () => {
// #endif
};
const hideKeyboardAfterSend = () => {
holdKeyboard.value = false;
holdKeyboardFlag.value = true;
isKeyboardShow.value = false;
if (
inputAreaRef.value &&
typeof inputAreaRef.value.blurInput === "function"
) {
inputAreaRef.value.blurInput();
} else {
uni.hideKeyboard();
}
};
const handleScrollAreaTouchStart = () => {
lastScrollTouchAt = Date.now();
hideKeyboardByScroll();
@@ -476,7 +494,6 @@ const handleReplyInstruct = async (item) => {
const sendMessageAction = (inputText) => {
console.log("输入消息:", inputText);
if (!inputText.trim()) return;
handleNoHideKeyboard();
// 发送消息时,强制开启自动滚动
isAutoScroll.value = true;
@@ -484,12 +501,7 @@ const sendMessageAction = (inputText) => {
resetMessageState();
sendMessage(inputText);
// 发送消息后保持键盘状态
if (holdKeyboard.value && inputAreaRef.value) {
setTimeout(() => {
inputAreaRef.value.focusInput();
}, 100);
}
hideKeyboardAfterSend();
setTimeoutScrollToBottom(true);
};