From 6f9c4c64dee7c45b9737dc9814b90e11e3a004e9 Mon Sep 17 00:00:00 2001 From: "Ray.Hao" <1490493387@qq.com> Date: Thu, 24 Apr 2025 08:20:52 +0800 Subject: [PATCH] =?UTF-8?q?wip:=20=E5=AD=97=E5=85=B8=20websocket=20?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Dict/index.vue | 14 +- src/hooks/useWebSocketDict.ts | 30 +- src/views/dashboard/index.vue | 4 +- src/views/demo/dict-websocket.vue | 442 +++++++++++++++++++----------- 4 files changed, 309 insertions(+), 181 deletions(-) diff --git a/src/components/Dict/index.vue b/src/components/Dict/index.vue index 14dbc9c7..79d98e4d 100644 --- a/src/components/Dict/index.vue +++ b/src/components/Dict/index.vue @@ -23,12 +23,7 @@ :style="style" @change="handleChange" > - + {{ option.label }} @@ -40,12 +35,7 @@ :style="style" @change="handleChange" > - + {{ option.label }} diff --git a/src/hooks/useWebSocketDict.ts b/src/hooks/useWebSocketDict.ts index 9570b701..3f2d5bd6 100644 --- a/src/hooks/useWebSocketDict.ts +++ b/src/hooks/useWebSocketDict.ts @@ -77,20 +77,44 @@ export function useWebSocketDict() { try { // 尝试解析消息 const eventData = JSON.parse(message.body) as DictEvent; + console.log( + `[WebSocket] 接收到字典事件: ${eventData.type}, 字典编码: ${eventData.dictCode}`, + eventData + ); if (eventData.type === "DICT_UPDATED") { // 删除缓存,强制重新加载 dictStore.removeDictItem(eventData.dictCode); - console.log(`字典 ${eventData.dictCode} 已更新,缓存已清除`); + console.log(`[WebSocket] 字典 ${eventData.dictCode} 已更新,缓存已清除`); ElMessage.success(`字典 ${eventData.dictCode} 已更新`); + + // 派发自定义事件,通知组件刷新数据 + window.dispatchEvent( + new CustomEvent("dict-updated", { + detail: { + dictCode: eventData.dictCode, + timestamp: eventData.timestamp, + }, + }) + ); } else if (eventData.type === "DICT_DELETED") { // 删除缓存 dictStore.removeDictItem(eventData.dictCode); - console.log(`字典 ${eventData.dictCode} 已删除,缓存已清除`); + console.log(`[WebSocket] 字典 ${eventData.dictCode} 已删除,缓存已清除`); ElMessage.warning(`字典 ${eventData.dictCode} 已删除`); + + // 派发自定义事件,通知组件刷新数据 + window.dispatchEvent( + new CustomEvent("dict-deleted", { + detail: { + dictCode: eventData.dictCode, + timestamp: eventData.timestamp, + }, + }) + ); } } catch (error) { - console.error("解析字典WebSocket消息失败:", error); + console.error("[WebSocket] 解析字典WebSocket消息失败:", error, message.body); } }; diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 7f9f3c64..2271030e 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -210,8 +210,8 @@
访问趋势 - - + 近7天 + 近30天
diff --git a/src/views/demo/dict-websocket.vue b/src/views/demo/dict-websocket.vue index dd8dcd1d..ed5e4f82 100644 --- a/src/views/demo/dict-websocket.vue +++ b/src/views/demo/dict-websocket.vue @@ -3,124 +3,160 @@ - -

本示例展示了当字典数据在服务端更新时,如何通过WebSocket实时更新前端缓存。

-

- 当管理员修改字典数据后,其他在线用户的字典缓存将自动刷新,无需手动刷新页面。 -

+ + + 本示例展示WebSocket实时更新字典缓存的效果。您可以编辑"男"性别字典项,保存后后端将通过WebSocket通知所有客户端刷新缓存。 -
- - - - -
- - - - + + + + + +
+
+ + + + + + + + + + + + + + success + + + warning + + + danger + + + info + + + primary + + + 保存 + 重置 + - - - - - -
- - + +
+
+
- - - -
-
- 暂无WebSocket消息 -
-
-
-
- {{ msg.title }} - {{ formatTime(msg.time) }} -
-
{{ JSON.stringify(msg.data, null, 2) }}
+ + + + +
+
+ 暂无WebSocket消息 +
+
+
+
+ {{ msg.title }} + {{ formatTime(msg.time) }}
+
{{ JSON.stringify(msg.data, null, 2) }}
- - - -
+
+ + -
- - -

这里模拟后端管理员更新字典数据后发送WebSocket通知

-

注意:这只是前端模拟,实际应用中由后端触发

- - - - - - - - 字典更新 - 字典删除 - - - - 模拟发送WebSocket消息 - 清空字典缓存 - - -
-
+ + + + +
+
{{
+                JSON.stringify(dictStore.getDictItems("gender"), null, 2)
+              }}
+
+
+
+