feat:修复键盘的问题

This commit is contained in:
zoujing 2025-08-05 20:01:30 +08:00
parent 9ca2d72256
commit e539c7d35d
4 changed files with 133 additions and 21 deletions

View File

@ -13,6 +13,17 @@
"navigationStyle": "custom"
}
},
{
"path": "pages/chat/ChatMainList",
"style": {
"navigationStyle": "custom",
"app-plus": {
"softinputMode": "adjustPan",
"bounce": "none",
"titleNView": false
}
}
},
{
"path": "pages/chat/ChatQuickAccess",
"style": {

View File

@ -6,6 +6,7 @@
</view>
<!-- 输入框 -->
<textarea
ref="textareaRef"
class="textarea"
type="text"
:placeholder="placeholder"
@ -13,11 +14,15 @@
confirm-type='done'
v-model="inputMessage"
@confirm="sendMessage"
@touchend="handleNoHideKeyboard"
@focus="handleFocus"
@blur="handleBlur"
@touchstart="handleTouchStart"
@touchend="handleTouchEnd"
:confirm-hold="true"
auto-height
:show-confirm-bar='false'
:hold-keyboard="holdKeyboard"
:adjust-position="true"
maxlength="300"
/>
<view class="input-container-send" @click="sendMessage">
@ -27,33 +32,90 @@
</template>
<script setup>
import { ref, watch } from 'vue'
import { ref, watch, nextTick, onMounted, onUnmounted } from 'vue'
const props = defineProps({
inputMessage: String,
holdKeyboard: Boolean
})
const emit = defineEmits(['update:inputMessage', 'send', 'noHideKeyboard'])
const emit = defineEmits(['update:inputMessage', 'send', 'noHideKeyboard', 'keyboardShow', 'keyboardHide'])
const textareaRef = ref(null)
const placeholder = ref('快告诉朵朵您在想什么~')
const inputMessage = ref(props.inputMessage || '')
const isFocused = ref(false)
const keyboardHeight = ref(0)
//
watch(() => props.inputMessage, (val) => {
inputMessage.value = val
})
//
onMounted(() => {
//
uni.onKeyboardHeightChange((res) => {
keyboardHeight.value = res.height
if (res.height > 0) {
emit('keyboardShow', res.height)
} else {
emit('keyboardHide')
}
})
})
const sendMessage = () => {
if (!inputMessage.value.trim()) return;
emit('send', inputMessage.value)
inputMessage.value = ''
emit('update:inputMessage', inputMessage.value)
//
if (props.holdKeyboard && textareaRef.value) {
nextTick(() => {
textareaRef.value.focus()
})
}
}
const handleNoHideKeyboard = () => {
const handleFocus = () => {
isFocused.value = true
emit('noHideKeyboard')
}
const handleBlur = () => {
isFocused.value = false
}
const handleTouchStart = () => {
emit('noHideKeyboard')
}
const handleTouchEnd = () => {
emit('noHideKeyboard')
}
//
const focusInput = () => {
if (textareaRef.value) {
textareaRef.value.focus()
}
}
//
const blurInput = () => {
if (textareaRef.value) {
textareaRef.value.blur()
}
}
//
defineExpose({
focusInput,
blurInput,
isFocused
})
</script>
<style scoped lang="scss">
@ -64,6 +126,8 @@ const handleNoHideKeyboard = () => {
background-color: #FFFFFF;
box-shadow: 0px 0px 20px 0px rgba(52,25,204,0.05);
margin: 0 12px;
/* 确保输入框在安全区域内 */
margin-bottom: 8px;
.input-container-voice {
display: flex;

View File

@ -61,10 +61,13 @@
<view class="footer-area">
<ChatQuickAccess @replySent="handleReplyInstruct"/>
<ChatInputArea
ref="inputAreaRef"
v-model="inputMessage"
:holdKeyboard="holdKeyboard"
@send="sendMessageAction"
@noHideKeyboard="handleNoHideKeyboard"
@keyboardShow="handleKeyboardShow"
@keyboardHide="handleKeyboardHide"
/>
</view>
</view>
@ -72,7 +75,7 @@
</template>
<script setup >
import { onMounted, nextTick } from 'vue'
import { onMounted, nextTick, computed } from 'vue'
import { ref } from 'vue'
import { defineEmits } from 'vue'
import { onLoad } from '@dcloudio/uni-app';
@ -106,12 +109,18 @@
///
const statusBarHeight = ref(20);
///
const inputAreaRef = ref(null);
const timer = ref(null)
/// focus
const holdKeyboard = ref(false)
///
const holdKeyboardFlag = ref(true)
///
const keyboardHeight = ref(0)
///
const isKeyboardShow = ref(false)
///
const scrollTop = ref(99999);
@ -136,6 +145,8 @@
let commonType = ''
//
const emits = defineEmits(['openDrawer'])
const openDrawer = () => {
@ -144,23 +155,37 @@
}
const handleTouchEnd = () => {
// #ifdef MP-WEIXIN
clearTimeout(timer.value)
timer.value = setTimeout(() => {
//
if (handleNoHideKeyboard) {
if (holdKeyboardFlag.value && isKeyboardShow.value) {
uni.hideKeyboard()
}
holdKeyboardFlag.value = true
}, 50)
// #endif
}, 100)
}
//
const handleNoHideKeyboard = () => {
// #ifdef MP-WEIXIN
holdKeyboardFlag.value = false
// #endif
}
//
const handleKeyboardShow = (height) => {
keyboardHeight.value = height
isKeyboardShow.value = true
holdKeyboard.value = true
//
setTimeout(() => {
scrollToBottom()
}, 150)
}
//
const handleKeyboardHide = () => {
keyboardHeight.value = 0
isKeyboardShow.value = false
holdKeyboard.value = false
}
///
@ -207,6 +232,12 @@
if(!isSessionActive) {
inputMessage.value = ''
}
//
if (holdKeyboard.value && inputAreaRef.value) {
setTimeout(() => {
inputAreaRef.value.focusInput()
}, 100)
}
setTimeoutScrollToBottom()
}

View File

@ -7,6 +7,8 @@
flex-direction: column;
overflow: hidden !important;
position: relative;
/* 确保在键盘弹起时布局正确 */
box-sizing: border-box;
.chat-container-bg {
position: fixed;
@ -20,12 +22,10 @@
.chat-content {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
flex: 1;
min-height: 0;
z-index: 1;
overflow: hidden;
}
@ -77,13 +77,19 @@
.footer-area {
width: 100vw;
flex-shrink: 0;
padding: 4px 0 24px 0;
padding: 4px 0 20px 0; /* 直接设置20px底部安全距离 */
background-color: #E9F3F7;
touch-action: pan-x; /* 仅允许横向触摸滚动 */
overflow-x: auto; /* 允许横向滚动 */
overflow-y: hidden; /* 禁止垂直滚动 */
/* 确保高度能够正确计算 */
touch-action: pan-x;
overflow-x: auto;
overflow-y: hidden;
min-height: fit-content;
/* 安卓键盘适配 - 使用相对定位配合adjustPan */
position: relative;
z-index: 1000;
transition: padding-bottom 0.3s ease;
/* 确保输入区域始终可见 */
transform: translateZ(0);
-webkit-transform: translateZ(0);
}
.area-input {