Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
416df14b
authored
Jul 17, 2024
by
puhui999
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【优化】客服重构消息列表加载逻辑
parent
ae0bca17
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
31 deletions
+53
-31
src/views/mall/promotion/kefu/components/KeFuMessageList.vue
+51
-29
src/views/mall/promotion/kefu/index.vue
+2
-2
No files found.
src/views/mall/promotion/kefu/components/KeFuMessageList.vue
View file @
416df14b
...
@@ -108,6 +108,7 @@ import { UserTypeEnum } from '@/utils/constants'
...
@@ -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({
...
@@ -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
...
@@ -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(() => {
...
@@ -163,20 +160,40 @@ const getMessageList0 = computed(() => {
})
})
/** 刷新消息列表 */
/** 刷新消息列表 */
const
refreshMessageList
=
async
()
=>
{
const
refreshMessageList
=
async
(
message
?:
any
)
=>
{
if
(
!
conversation
.
value
)
{
if
(
!
conversation
.
value
)
{
return
return
}
}
queryParams
.
pageNo
=
1
if
(
typeof
message
!==
'undefined'
)
{
await
getMessageList
(
conversation
.
value
,
false
)
pushMessage
(
message
)
}
else
{
queryParams
.
pageNo
=
1
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) => {
...
@@ -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 () => {
...
@@ -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 () => {
...
@@ -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
)
}
}
...
...
src/views/mall/promotion/kefu/index.vue
View file @
416df14b
...
@@ -60,7 +60,7 @@ watchEffect(() => {
...
@@ -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 = () => {
...
@@ -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
?.
get
MessageList
(
conversation
,
true
)
keFuChatBoxRef
.
value
?.
get
NewMessageList
(
conversation
)
}
}
/** 初始化 */
/** 初始化 */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment