【优化】客服重构消息列表加载逻辑

This commit is contained in:
puhui999 2024-07-17 11:25:26 +08:00
parent ae0bca1774
commit 416df14b8c
2 changed files with 53 additions and 31 deletions

View File

@ -108,6 +108,7 @@ import { UserTypeEnum } from '@/utils/constants'
import { formatDate } from '@/utils/formatTime' import { formatDate } from '@/utils/formatTime'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime' import relativeTime from 'dayjs/plugin/relativeTime'
import { debounce } from 'lodash-es'
dayjs.extend(relativeTime) dayjs.extend(relativeTime)
@ -127,17 +128,7 @@ const queryParams = reactive({
const total = ref(0) // const total = ref(0) //
const refreshContent = ref(false) // , const refreshContent = ref(false) // ,
/** 获得消息列表 */ /** 获得消息列表 */
const getMessageList = async (val: KeFuConversationRespVO, conversationChange: boolean) => { const getMessageList = async () => {
// ,
if (conversationChange) {
queryParams.pageNo = 1
messageList.value = []
total.value = 0
loadHistory.value = false
refreshContent.value = false
}
conversation.value = val
queryParams.conversationId = val.id
const res = await KeFuMessageApi.getKeFuMessagePage(queryParams) const res = await KeFuMessageApi.getKeFuMessagePage(queryParams)
total.value = res.total total.value = res.total
// //
@ -146,14 +137,20 @@ const getMessageList = async (val: KeFuConversationRespVO, conversationChange: b
} else { } else {
// //
for (const item of res.list) { for (const item of res.list) {
if (messageList.value.some((val) => val.id === item.id)) { pushMessage(item)
continue
}
messageList.value.push(item)
} }
} }
refreshContent.value = true refreshContent.value = true
await scrollToBottom() }
/** 添加消息 */
const pushMessage = (message: any) => {
if (message.conversationId !== conversation.value.id) {
return
}
if (messageList.value.some((val) => val.id === message.id)) {
return
}
messageList.value.push(message)
} }
/** 按照时间倒序,获取消息列表 */ /** 按照时间倒序,获取消息列表 */
@ -163,20 +160,40 @@ const getMessageList0 = computed(() => {
}) })
/** 刷新消息列表 */ /** 刷新消息列表 */
const refreshMessageList = async () => { const refreshMessageList = async (message?: any) => {
if (!conversation.value) { if (!conversation.value) {
return return
} }
if (typeof message !== 'undefined') {
pushMessage(message)
} else {
queryParams.pageNo = 1 queryParams.pageNo = 1
await getMessageList(conversation.value, false) await getMessageList()
}
if (loadHistory.value) { if (loadHistory.value) {
// //
showNewMessageTip.value = true showNewMessageTip.value = true
} else {
//
await handleToNewMessage()
} }
} }
const getNewMessageList = async (val: KeFuConversationRespVO) => {
defineExpose({ getMessageList, refreshMessageList }) // ,
queryParams.pageNo = 1
messageList.value = []
total.value = 0
loadHistory.value = false
refreshContent.value = false
//
conversation.value = val
queryParams.conversationId = val.id
//
await refreshMessageList()
}
defineExpose({ getNewMessageList, refreshMessageList })
const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) // const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) //
const skipGetMessageList = computed(() => { const skipGetMessageList = computed(() => {
// //
@ -221,9 +238,7 @@ const sendMessage = async (msg: any) => {
await KeFuMessageApi.sendKeFuMessage(msg) await KeFuMessageApi.sendKeFuMessage(msg)
message.value = '' message.value = ''
// //
await getMessageList(conversation.value, false) await refreshMessageList()
//
await scrollToBottom()
} }
/** 滚动到底部 */ /** 滚动到底部 */
@ -248,17 +263,24 @@ const handleToNewMessage = async () => {
await scrollToBottom() await scrollToBottom()
} }
/** 加载历史消息 */
const loadHistory = ref(false) // const loadHistory = ref(false) //
const handleScroll = async ({ scrollTop }) => { /** 处理消息列表滚动事件(debounce 限流) */
const handleScroll = debounce(({ scrollTop }) => {
if (skipGetMessageList.value) { if (skipGetMessageList.value) {
return return
} }
// //
if (scrollTop === 0) { if (scrollTop === 0) {
await handleOldMessage() handleOldMessage()
} }
const wrap = scrollbarRef.value?.wrapRef
//
if (Math.abs(wrap!.scrollHeight - wrap!.clientHeight - wrap!.scrollTop) < 1) {
loadHistory.value = false
refreshMessageList()
} }
}, 200)
/** 加载历史消息 */
const handleOldMessage = async () => { const handleOldMessage = async () => {
// //
const oldPageHeight = innerRef.value?.clientHeight const oldPageHeight = innerRef.value?.clientHeight
@ -268,7 +290,7 @@ const handleOldMessage = async () => {
loadHistory.value = true loadHistory.value = true
// //
queryParams.pageNo += 1 queryParams.pageNo += 1
await getMessageList(conversation.value, false) await getMessageList()
// //
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight) scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight)
} }

View File

@ -60,7 +60,7 @@ watchEffect(() => {
// //
getConversationList() getConversationList()
// //
keFuChatBoxRef.value?.refreshMessageList() keFuChatBoxRef.value?.refreshMessageList(JSON.parse(jsonMessage.content))
return return
} }
// 2.3 KEFU_MESSAGE_ADMIN_READ // 2.3 KEFU_MESSAGE_ADMIN_READ
@ -82,7 +82,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, true) keFuChatBoxRef.value?.getNewMessageList(conversation)
} }
/** 初始化 */ /** 初始化 */