From 39106843f3db86ba3e17478d74220936328355c6 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Wed, 22 Jan 2025 18:10:51 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=BC=BA=E9=99=B7=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91mall:=20=E4=BF=AE=E5=A4=8D=E5=AE=A2=E6=9C=8D=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E9=A1=B5=E9=9D=A2=E6=8E=A5=E6=94=B6=E5=88=B0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=97=B6=EF=BC=8C=E9=87=8D=E5=A4=8D=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E4=BA=86=E5=A4=9A=E6=AC=A1=E8=8E=B7=E5=8F=96=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/mall/promotion/kefu/index.vue | 71 +++++++++++++------------ 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/views/mall/promotion/kefu/index.vue b/src/views/mall/promotion/kefu/index.vue index f0fa2a02..916c375f 100644 --- a/src/views/mall/promotion/kefu/index.vue +++ b/src/views/mall/promotion/kefu/index.vue @@ -37,40 +37,45 @@ const { data, close, open } = useWebSocket(server.value, { }) /** 监听 WebSocket 数据 */ -watchEffect(() => { - if (!data.value) { - return +watch( + () => data.value, + (newData) => { + if (!newData) return + try { + // 1. 收到心跳 + if (newData === 'pong') return + + // 2.1 解析 type 消息类型 + const jsonMessage = JSON.parse(newData) + const type = jsonMessage.type + if (!type) { + message.error('未知的消息类型:' + newData) + return + } + + // 2.2 消息类型:KEFU_MESSAGE_TYPE + if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) { + const message = JSON.parse(jsonMessage.content) + // 刷新会话列表 + kefuStore.updateConversation(message.conversationId) + // 刷新消息列表 + keFuChatBoxRef.value?.refreshMessageList(message) + return + } + + // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ + if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) { + // 更新会话已读 + kefuStore.updateConversationStatus(jsonParse(jsonMessage.content)) + } + } catch (error) { + console.error(error) + } + }, + { + immediate: false // 不立即执行 } - try { - // 1. 收到心跳 - if (data.value === 'pong') { - return - } - // 2.1 解析 type 消息类型 - const jsonMessage = JSON.parse(data.value) - const type = jsonMessage.type - if (!type) { - message.error('未知的消息类型:' + data.value) - return - } - // 2.2 消息类型:KEFU_MESSAGE_TYPE - if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) { - const message = JSON.parse(jsonMessage.content) - // 刷新会话列表 - kefuStore.updateConversation(message.conversationId) - // 刷新消息列表 - keFuChatBoxRef.value?.refreshMessageList(message) - return - } - // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ - if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) { - // 更新会话已读 - kefuStore.updateConversationStatus(jsonParse(jsonMessage.content)) - } - } catch (error) { - console.error(error) - } -}) +) // ======================= WebSocket end ======================= /** 加载指定会话的消息列表 */