Skip to content

Commit 3910684

Browse files
author
puhui999
committed
【缺陷修复】mall: 修复客服中心页面接收到消息时,重复调用了多次获取会话接口的问题
1 parent fef86bc commit 3910684

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,45 @@ const { data, close, open } = useWebSocket(server.value, {
3737
})
3838
3939
/** 监听 WebSocket 数据 */
40-
watchEffect(() => {
41-
if (!data.value) {
42-
return
43-
}
44-
try {
45-
// 1. 收到心跳
46-
if (data.value === 'pong') {
47-
return
48-
}
49-
// 2.1 解析 type 消息类型
50-
const jsonMessage = JSON.parse(data.value)
51-
const type = jsonMessage.type
52-
if (!type) {
53-
message.error('未知的消息类型:' + data.value)
54-
return
55-
}
56-
// 2.2 消息类型:KEFU_MESSAGE_TYPE
57-
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
58-
const message = JSON.parse(jsonMessage.content)
59-
// 刷新会话列表
60-
kefuStore.updateConversation(message.conversationId)
61-
// 刷新消息列表
62-
keFuChatBoxRef.value?.refreshMessageList(message)
63-
return
64-
}
65-
// 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
66-
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
67-
// 更新会话已读
68-
kefuStore.updateConversationStatus(jsonParse(jsonMessage.content))
40+
watch(
41+
() => data.value,
42+
(newData) => {
43+
if (!newData) return
44+
try {
45+
// 1. 收到心跳
46+
if (newData === 'pong') return
47+
48+
// 2.1 解析 type 消息类型
49+
const jsonMessage = JSON.parse(newData)
50+
const type = jsonMessage.type
51+
if (!type) {
52+
message.error('未知的消息类型:' + newData)
53+
return
54+
}
55+
56+
// 2.2 消息类型:KEFU_MESSAGE_TYPE
57+
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
58+
const message = JSON.parse(jsonMessage.content)
59+
// 刷新会话列表
60+
kefuStore.updateConversation(message.conversationId)
61+
// 刷新消息列表
62+
keFuChatBoxRef.value?.refreshMessageList(message)
63+
return
64+
}
65+
66+
// 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
67+
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
68+
// 更新会话已读
69+
kefuStore.updateConversationStatus(jsonParse(jsonMessage.content))
70+
}
71+
} catch (error) {
72+
console.error(error)
6973
}
70-
} catch (error) {
71-
console.error(error)
74+
},
75+
{
76+
immediate: false // 不立即执行
7277
}
73-
})
78+
)
7479
// ======================= WebSocket end =======================
7580
7681
/** 加载指定会话的消息列表 */

0 commit comments

Comments
 (0)