【功能完善】客服消息输入键盘弹起适配

pull/146/head
puhui999 2025-05-08 17:49:12 +08:00
parent 9f1cd4c3a7
commit e0f7ec3629
1 changed files with 51 additions and 5 deletions

View File

@ -30,20 +30,21 @@
<script setup> <script setup>
import MessageListItem from '@/pages/chat/components/messageListItem.vue'; import MessageListItem from '@/pages/chat/components/messageListItem.vue';
import { onMounted, reactive, ref } from 'vue'; import { onMounted, reactive, ref, computed } from 'vue';
import KeFuApi from '@/sheep/api/promotion/kefu'; import KeFuApi from '@/sheep/api/promotion/kefu';
import { isEmpty } from '@/sheep/helper/utils'; import { isEmpty } from '@/sheep/helper/utils';
import { formatDate } from '@/sheep/util'; import { formatDate } from '@/sheep/util';
import sheep from '@/sheep'; import sheep from '@/sheep';
const { safeAreaInsets } = sheep.$platform.device; const { safeAreaInsets } = sheep.$platform.device;
const safeAreaInsetsBottom = safeAreaInsets.bottom + 'px' // const safeAreaInsetsBottom = safeAreaInsets.bottom + 'px'; //
const messageList = ref([]); // const messageList = ref([]); //
const showTip = ref(false); // const showTip = ref(false); //
const showNewMessageTip = ref(false); // const showNewMessageTip = ref(false); //
const refreshMessage = ref(false); // const refreshMessage = ref(false); //
const isLoading = ref(false); // const isLoading = ref(false); //
const hasMore = ref(true); // const hasMore = ref(true); //
const keyboardHeight = ref(0); //
const scroll = ref({ const scroll = ref({
top: 0, top: 0,
oldTop: 0, oldTop: 0,
@ -54,6 +55,16 @@
createTime: undefined, createTime: undefined,
}); // }); //
//
const chatScrollHeight = computed(() => {
const baseHeight = 'calc(100vh - 100px - ' + safeAreaInsetsBottom + ')';
if (keyboardHeight.value > 0) {
//
return `calc(${baseHeight} - ${keyboardHeight.value}px)`;
}
return baseHeight;
});
// //
const getMessageList = async () => { const getMessageList = async () => {
isLoading.value = true; isLoading.value = true;
@ -147,7 +158,16 @@
showTip.value = false; showTip.value = false;
}; };
defineExpose({ getMessageList, refreshMessageList, scrollToTop }); /** 设置键盘高度 */
const setKeyboardHeight = (height) => {
keyboardHeight.value = height;
//
if (height > 0) {
scrollToTop();
}
};
defineExpose({ getMessageList, refreshMessageList });
/** 监听消息列表滚动 */ /** 监听消息列表滚动 */
const onScroll = (e) => { const onScroll = (e) => {
@ -161,16 +181,42 @@
} }
}; };
//
const setupKeyboardListeners = () => {
// #ifdef H5
// H5
window.addEventListener('resize', () => {
//
if (document.activeElement && (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA')) {
//
const currentHeight = window.innerHeight;
const viewportHeight = window.visualViewport ? window.visualViewport.height : window.innerHeight;
const keyboardHeight = currentHeight - viewportHeight;
setKeyboardHeight(keyboardHeight > 0 ? keyboardHeight : 0);
} else {
setKeyboardHeight(0);
}
});
// #endif
// #ifdef MP-WEIXIN
//
uni.onKeyboardHeightChange((res) => {
setKeyboardHeight(res.height);
});
// #endif
};
onMounted(() => { onMounted(() => {
queryParams.no = 1; // queryParams.no = 1; //
getMessageList(); getMessageList();
setupKeyboardListeners();
}); });
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.chat-scroll-view { .chat-scroll-view {
/* 减去底部输入框的高度 */ height: v-bind(chatScrollHeight);
height: calc(100vh - 100px - v-bind(safeAreaInsetsBottom));
width: 100%; width: 100%;
position: relative; position: relative;
background-color: #f8f8f8; background-color: #f8f8f8;