@@ -108,6 +108,7 @@ import { UserTypeEnum } from '@/utils/constants'
108
108
import { formatDate } from ' @/utils/formatTime'
109
109
import dayjs from ' dayjs'
110
110
import relativeTime from ' dayjs/plugin/relativeTime'
111
+ import { debounce } from ' lodash-es'
111
112
112
113
dayjs .extend (relativeTime )
113
114
@@ -127,17 +128,7 @@ const queryParams = reactive({
127
128
const total = ref (0 ) // 消息总条数
128
129
const refreshContent = ref (false ) // 内容刷新,主要解决会话消息页面高度不一致导致的滚动功能精度失效
129
130
/** 获得消息列表 */
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 () => {
141
132
const res = await KeFuMessageApi .getKeFuMessagePage (queryParams )
142
133
total .value = res .total
143
134
// 情况一:加载最新消息
@@ -146,14 +137,20 @@ const getMessageList = async (val: KeFuConversationRespVO, conversationChange: b
146
137
} else {
147
138
// 情况二:加载历史消息
148
139
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 )
153
141
}
154
142
}
155
143
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 )
157
154
}
158
155
159
156
/** 按照时间倒序,获取消息列表 */
@@ -163,20 +160,40 @@ const getMessageList0 = computed(() => {
163
160
})
164
161
165
162
/** 刷新消息列表 */
166
- const refreshMessageList = async () => {
163
+ const refreshMessageList = async (message ? : any ) => {
167
164
if (! conversation .value ) {
168
165
return
169
166
}
170
167
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
+
173
175
if (loadHistory .value ) {
174
176
// 右下角显示有新消息提示
175
177
showNewMessageTip .value = true
178
+ } else {
179
+ // 滚动到最新消息处
180
+ await handleToNewMessage ()
176
181
}
177
182
}
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 })
180
197
const showKeFuMessageList = computed (() => ! isEmpty (conversation .value )) // 是否显示聊天区域
181
198
const skipGetMessageList = computed (() => {
182
199
// 已加载到最后一页的话则不触发新的消息获取
@@ -221,9 +238,7 @@ const sendMessage = async (msg: any) => {
221
238
await KeFuMessageApi .sendKeFuMessage (msg )
222
239
message .value = ' '
223
240
// 加载消息列表
224
- await getMessageList (conversation .value , false )
225
- // 滚动到最新消息处
226
- await scrollToBottom ()
241
+ await refreshMessageList ()
227
242
}
228
243
229
244
/** 滚动到底部 */
@@ -248,17 +263,24 @@ const handleToNewMessage = async () => {
248
263
await scrollToBottom ()
249
264
}
250
265
251
- /** 加载历史消息 */
252
266
const loadHistory = ref (false ) // 加载历史消息
253
- const handleScroll = async ({ scrollTop }) => {
267
+ /** 处理消息列表滚动事件(debounce 限流) */
268
+ const handleScroll = debounce (({ scrollTop }) => {
254
269
if (skipGetMessageList .value ) {
255
270
return
256
271
}
257
272
// 触顶自动加载下一页数据
258
273
if (scrollTop === 0 ) {
259
- await handleOldMessage ()
274
+ handleOldMessage ()
260
275
}
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
+ /** 加载历史消息 */
262
284
const handleOldMessage = async () => {
263
285
// 记录已有页面高度
264
286
const oldPageHeight = innerRef .value ?.clientHeight
@@ -268,7 +290,7 @@ const handleOldMessage = async () => {
268
290
loadHistory .value = true
269
291
// 加载消息列表
270
292
queryParams .pageNo += 1
271
- await getMessageList (conversation . value , false )
293
+ await getMessageList ()
272
294
// 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
273
295
scrollbarRef .value ! .setScrollTop (innerRef .value ! .clientHeight - oldPageHeight )
274
296
}
0 commit comments