Skip to content

Commit fa08929

Browse files
author
puhui999
committed
【优化】mall 客服加载历史消息时页面保持在当前正在查看的消息位置
1 parent 32d6ac0 commit fa08929

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import { useEmoji } from './tools/emoji'
7373
import { formatDate } from '@/utils/formatTime'
7474
import { KeFuMessageContentTypeEnum } from './tools/constants'
7575
76-
defineOptions({ name: 'KeFuConversationBox' })
76+
defineOptions({ name: 'KeFuConversationList' })
7777
7878
const message = useMessage() // 消息弹窗
7979

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<template>
2-
<el-container v-if="showChatBox" class="kefu">
2+
<el-container v-if="showKeFuMessageList" class="kefu">
33
<el-header>
44
<div class="kefu-title">{{ conversation.userNickname }}</div>
5-
</el-header>
6-
<el-main class="kefu-content overflow-visible">
75
<!-- 加载历史消息 -->
86
<div
97
v-show="loadingMore"
@@ -12,6 +10,8 @@
1210
>
1311
加载更多
1412
</div>
13+
</el-header>
14+
<el-main class="kefu-content overflow-visible">
1515
<el-scrollbar ref="scrollbarRef" always height="calc(100vh - 495px)" @scroll="handleScroll">
1616
<div ref="innerRef" class="w-[100%] pb-3px">
1717
<!-- 消息列表 -->
@@ -119,7 +119,7 @@ import relativeTime from 'dayjs/plugin/relativeTime'
119119
120120
dayjs.extend(relativeTime)
121121
122-
defineOptions({ name: 'KeFuMessageBox' })
122+
defineOptions({ name: 'KeFuMessageList' })
123123
124124
const message = ref('') // 消息弹窗
125125
@@ -137,17 +137,24 @@ const total = ref(0) // 消息总条数
137137
const getMessageList = async (val: KeFuConversationRespVO) => {
138138
conversation.value = val
139139
queryParams.conversationId = val.id
140+
// 情况一:已经加载完所有消息
140141
const messageTotal = messageList.value.length
141142
if (total.value > 0 && messageTotal > 0 && messageTotal === total.value) {
142143
return
143144
}
144145
const res = await KeFuMessageApi.getKeFuMessagePage(queryParams)
145146
total.value = res.total
146-
for (const item of res.list) {
147-
if (messageList.value.some((val) => val.id === item.id)) {
148-
continue
147+
// 情况二:加载最新消息
148+
if (queryParams.pageNo === 1) {
149+
messageList.value = res.list
150+
} else {
151+
// 情况三:加载历史消息
152+
for (const item of res.list) {
153+
if (messageList.value.some((val) => val.id === item.id)) {
154+
continue
155+
}
156+
messageList.value.push(item)
149157
}
150-
messageList.value.push(item)
151158
}
152159
await scrollToBottom()
153160
}
@@ -167,13 +174,13 @@ const refreshMessageList = async () => {
167174
queryParams.pageNo = 1
168175
await getMessageList(conversation.value)
169176
if (loadHistory.value) {
170-
// 有下角显示有新消息提示
177+
// 右下角显示有新消息提示
171178
showNewMessageTip.value = true
172179
}
173180
}
174181
175182
defineExpose({ getMessageList, refreshMessageList })
176-
const showChatBox = computed(() => !isEmpty(conversation.value)) // 是否显示聊天区域
183+
const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) // 是否显示聊天区域
177184
178185
/** 处理表情选择 */
179186
const handleEmojiSelect = (item: Emoji) => {
@@ -252,12 +259,15 @@ const handleScroll = async ({ scrollTop }) => {
252259
loadingMore.value = scrollTop < 20
253260
}
254261
const handleOldMessage = async () => {
262+
// 记录已有页面高度
263+
const oldPageHeight = innerRef.value!.clientHeight
255264
loadHistory.value = true
256265
// 加载消息列表
257266
queryParams.pageNo += 1
258267
await getMessageList(conversation.value)
259268
loadingMore.value = false
260-
// TODO puhui999: 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
269+
// 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
270+
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight)
261271
}
262272
263273
/**
@@ -283,23 +293,18 @@ const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
283293
line-height: 60px;
284294
}
285295
286-
&-content {
287-
position: relative;
288-
289-
.loadingMore {
290-
position: absolute;
291-
top: 0;
292-
left: 0;
293-
width: 100%;
294-
height: 50px;
295-
background-color: #eee;
296-
color: #666;
297-
text-align: center;
298-
line-height: 50px;
299-
transform: translateY(-100%);
300-
transition: transform 0.3s ease-in-out;
301-
}
296+
.loadingMore {
297+
width: 100%;
298+
height: 50px;
299+
background-color: #eee;
300+
color: #666;
301+
text-align: center;
302+
line-height: 50px;
303+
transform: translateY(-100%);
304+
transition: transform 0.3s ease-in-out;
305+
}
302306
307+
&-content {
303308
.newMessageTip {
304309
position: absolute;
305310
bottom: 35px;

0 commit comments

Comments
 (0)