Skip to content

Commit 416df14

Browse files
author
puhui999
committed
【优化】客服重构消息列表加载逻辑
1 parent ae0bca1 commit 416df14

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

src/views/mall/promotion/kefu/components/KeFuMessageList.vue

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import { UserTypeEnum } from '@/utils/constants'
108108
import { formatDate } from '@/utils/formatTime'
109109
import dayjs from 'dayjs'
110110
import relativeTime from 'dayjs/plugin/relativeTime'
111+
import { debounce } from 'lodash-es'
111112
112113
dayjs.extend(relativeTime)
113114
@@ -127,17 +128,7 @@ const queryParams = reactive({
127128
const total = ref(0) // 消息总条数
128129
const refreshContent = ref(false) // 内容刷新,主要解决会话消息页面高度不一致导致的滚动功能精度失效
129130
/** 获得消息列表 */
130-
const getMessageList = async (val: KeFuConversationRespVO, conversationChange: boolean) => {
131-
// 会话切换,重置相关参数
132-
if (conversationChange) {
133-
queryParams.pageNo = 1
134-
messageList.value = []
135-
total.value = 0
136-
loadHistory.value = false
137-
refreshContent.value = false
138-
}
139-
conversation.value = val
140-
queryParams.conversationId = val.id
131+
const getMessageList = async () => {
141132
const res = await KeFuMessageApi.getKeFuMessagePage(queryParams)
142133
total.value = res.total
143134
// 情况一:加载最新消息
@@ -146,14 +137,20 @@ const getMessageList = async (val: KeFuConversationRespVO, conversationChange: b
146137
} else {
147138
// 情况二:加载历史消息
148139
for (const item of res.list) {
149-
if (messageList.value.some((val) => val.id === item.id)) {
150-
continue
151-
}
152-
messageList.value.push(item)
140+
pushMessage(item)
153141
}
154142
}
155143
refreshContent.value = true
156-
await scrollToBottom()
144+
}
145+
/** 添加消息 */
146+
const pushMessage = (message: any) => {
147+
if (message.conversationId !== conversation.value.id) {
148+
return
149+
}
150+
if (messageList.value.some((val) => val.id === message.id)) {
151+
return
152+
}
153+
messageList.value.push(message)
157154
}
158155
159156
/** 按照时间倒序,获取消息列表 */
@@ -163,20 +160,40 @@ const getMessageList0 = computed(() => {
163160
})
164161
165162
/** 刷新消息列表 */
166-
const refreshMessageList = async () => {
163+
const refreshMessageList = async (message?: any) => {
167164
if (!conversation.value) {
168165
return
169166
}
170167
171-
queryParams.pageNo = 1
172-
await getMessageList(conversation.value, false)
168+
if (typeof message !== 'undefined') {
169+
pushMessage(message)
170+
} else {
171+
queryParams.pageNo = 1
172+
await getMessageList()
173+
}
174+
173175
if (loadHistory.value) {
174176
// 右下角显示有新消息提示
175177
showNewMessageTip.value = true
178+
} else {
179+
// 滚动到最新消息处
180+
await handleToNewMessage()
176181
}
177182
}
178-
179-
defineExpose({ getMessageList, refreshMessageList })
183+
const getNewMessageList = async (val: KeFuConversationRespVO) => {
184+
// 会话切换,重置相关参数
185+
queryParams.pageNo = 1
186+
messageList.value = []
187+
total.value = 0
188+
loadHistory.value = false
189+
refreshContent.value = false
190+
// 设置会话相关属性
191+
conversation.value = val
192+
queryParams.conversationId = val.id
193+
// 获取消息
194+
await refreshMessageList()
195+
}
196+
defineExpose({ getNewMessageList, refreshMessageList })
180197
const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) // 是否显示聊天区域
181198
const skipGetMessageList = computed(() => {
182199
// 已加载到最后一页的话则不触发新的消息获取
@@ -221,9 +238,7 @@ const sendMessage = async (msg: any) => {
221238
await KeFuMessageApi.sendKeFuMessage(msg)
222239
message.value = ''
223240
// 加载消息列表
224-
await getMessageList(conversation.value, false)
225-
// 滚动到最新消息处
226-
await scrollToBottom()
241+
await refreshMessageList()
227242
}
228243
229244
/** 滚动到底部 */
@@ -248,17 +263,24 @@ const handleToNewMessage = async () => {
248263
await scrollToBottom()
249264
}
250265
251-
/** 加载历史消息 */
252266
const loadHistory = ref(false) // 加载历史消息
253-
const handleScroll = async ({ scrollTop }) => {
267+
/** 处理消息列表滚动事件(debounce 限流) */
268+
const handleScroll = debounce(({ scrollTop }) => {
254269
if (skipGetMessageList.value) {
255270
return
256271
}
257272
// 触顶自动加载下一页数据
258273
if (scrollTop === 0) {
259-
await handleOldMessage()
274+
handleOldMessage()
260275
}
261-
}
276+
const wrap = scrollbarRef.value?.wrapRef
277+
// 触底重置
278+
if (Math.abs(wrap!.scrollHeight - wrap!.clientHeight - wrap!.scrollTop) < 1) {
279+
loadHistory.value = false
280+
refreshMessageList()
281+
}
282+
}, 200)
283+
/** 加载历史消息 */
262284
const handleOldMessage = async () => {
263285
// 记录已有页面高度
264286
const oldPageHeight = innerRef.value?.clientHeight
@@ -268,7 +290,7 @@ const handleOldMessage = async () => {
268290
loadHistory.value = true
269291
// 加载消息列表
270292
queryParams.pageNo += 1
271-
await getMessageList(conversation.value, false)
293+
await getMessageList()
272294
// 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
273295
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight)
274296
}

src/views/mall/promotion/kefu/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ watchEffect(() => {
6060
// 刷新会话列表
6161
getConversationList()
6262
// 刷新消息列表
63-
keFuChatBoxRef.value?.refreshMessageList()
63+
keFuChatBoxRef.value?.refreshMessageList(JSON.parse(jsonMessage.content))
6464
return
6565
}
6666
// 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
@@ -82,7 +82,7 @@ const getConversationList = () => {
8282
/** 加载指定会话的消息列表 */
8383
const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
8484
const handleChange = (conversation: KeFuConversationRespVO) => {
85-
keFuChatBoxRef.value?.getMessageList(conversation, true)
85+
keFuChatBoxRef.value?.getNewMessageList(conversation)
8686
}
8787
8888
/** 初始化 */

0 commit comments

Comments
 (0)