【修复】mall 客服会话消息页面高度不一致导致的滚动功能精度失效

This commit is contained in:
puhui999 2024-07-10 18:09:04 +08:00
parent b6cd12a300
commit 79b6b87721
2 changed files with 29 additions and 13 deletions

View File

@ -5,7 +5,7 @@
</el-header> </el-header>
<el-main class="kefu-content overflow-visible"> <el-main class="kefu-content overflow-visible">
<el-scrollbar ref="scrollbarRef" always height="calc(100vh - 495px)" @scroll="handleScroll"> <el-scrollbar ref="scrollbarRef" always height="calc(100vh - 495px)" @scroll="handleScroll">
<div ref="innerRef" class="w-[100%] pb-3px"> <div v-if="refreshContent" ref="innerRef" class="w-[100%] pb-3px">
<!-- 消息列表 --> <!-- 消息列表 -->
<div v-for="(item, index) in getMessageList0" :key="item.id" class="w-[100%]"> <div v-for="(item, index) in getMessageList0" :key="item.id" class="w-[100%]">
<div class="flex justify-center items-center mb-20px"> <div class="flex justify-center items-center mb-20px">
@ -121,17 +121,25 @@ const conversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) /
const showNewMessageTip = ref(false) // const showNewMessageTip = ref(false) //
const queryParams = reactive({ const queryParams = reactive({
pageNo: 1, pageNo: 1,
pageSize: 10,
conversationId: 0 conversationId: 0
}) })
const total = ref(0) // const total = ref(0) //
const refreshContent = ref(false) // ,
/** 获得消息列表 */ /** 获得消息列表 */
const getMessageList = async (val: KeFuConversationRespVO) => { const getMessageList = async (val: KeFuConversationRespVO, conversationChange: boolean) => {
// ,
if (conversationChange) {
queryParams.pageNo = 1
messageList.value = []
total.value = 0
loadHistory.value = false
refreshContent.value = false
}
conversation.value = val conversation.value = val
queryParams.conversationId = val.id queryParams.conversationId = val.id
// //
const messageTotal = messageList.value.length if (skipGetMessageList.value) {
if (total.value > 0 && messageTotal > 0 && messageTotal === total.value) {
return return
} }
const res = await KeFuMessageApi.getKeFuMessagePage(queryParams) const res = await KeFuMessageApi.getKeFuMessagePage(queryParams)
@ -148,6 +156,7 @@ const getMessageList = async (val: KeFuConversationRespVO) => {
messageList.value.push(item) messageList.value.push(item)
} }
} }
refreshContent.value = true
await scrollToBottom() await scrollToBottom()
} }
@ -164,7 +173,7 @@ const refreshMessageList = async () => {
} }
queryParams.pageNo = 1 queryParams.pageNo = 1
await getMessageList(conversation.value) await getMessageList(conversation.value, false)
if (loadHistory.value) { if (loadHistory.value) {
// //
showNewMessageTip.value = true showNewMessageTip.value = true
@ -173,6 +182,10 @@ const refreshMessageList = async () => {
defineExpose({ getMessageList, refreshMessageList }) defineExpose({ getMessageList, refreshMessageList })
const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) // const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) //
const skipGetMessageList = computed(() => {
//
return total.value > 0 && Math.ceil(total.value / queryParams.pageSize) === queryParams.pageNo
}) //
/** 处理表情选择 */ /** 处理表情选择 */
const handleEmojiSelect = (item: Emoji) => { const handleEmojiSelect = (item: Emoji) => {
@ -212,7 +225,7 @@ const sendMessage = async (msg: any) => {
await KeFuMessageApi.sendKeFuMessage(msg) await KeFuMessageApi.sendKeFuMessage(msg)
message.value = '' message.value = ''
// //
await getMessageList(conversation.value) await getMessageList(conversation.value, false)
// //
await scrollToBottom() await scrollToBottom()
} }
@ -242,8 +255,7 @@ const handleToNewMessage = async () => {
/** 加载历史消息 */ /** 加载历史消息 */
const loadHistory = ref(false) // const loadHistory = ref(false) //
const handleScroll = async ({ scrollTop }) => { const handleScroll = async ({ scrollTop }) => {
const messageTotal = messageList.value.length if (skipGetMessageList.value) {
if (total.value > 0 && messageTotal > 0 && messageTotal === total.value) {
return return
} }
// //
@ -253,11 +265,14 @@ const handleScroll = async ({ scrollTop }) => {
} }
const handleOldMessage = async () => { const handleOldMessage = async () => {
// //
const oldPageHeight = innerRef.value!.clientHeight const oldPageHeight = innerRef.value?.clientHeight
if (!oldPageHeight) {
return
}
loadHistory.value = true loadHistory.value = true
// //
queryParams.pageNo += 1 queryParams.pageNo += 1
await getMessageList(conversation.value) await getMessageList(conversation.value, false)
// //
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight) scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight)
} }
@ -286,6 +301,8 @@ const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
} }
&-content { &-content {
position: relative;
.newMessageTip { .newMessageTip {
position: absolute; position: absolute;
bottom: 35px; bottom: 35px;

View File

@ -75,7 +75,6 @@ watchEffect(() => {
} }
}) })
// ======================= WebSocket end ======================= // ======================= WebSocket end =======================
/** 加载会话列表 */ /** 加载会话列表 */
const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>() const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>()
const getConversationList = () => { const getConversationList = () => {
@ -85,7 +84,7 @@ const getConversationList = () => {
/** 加载指定会话的消息列表 */ /** 加载指定会话的消息列表 */
const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>() const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
const handleChange = (conversation: KeFuConversationRespVO) => { const handleChange = (conversation: KeFuConversationRespVO) => {
keFuChatBoxRef.value?.getMessageList(conversation) keFuChatBoxRef.value?.getMessageList(conversation, true)
} }
/** 初始化 */ /** 初始化 */