diff --git a/src/lang/en_US.ts b/src/lang/en_US.ts index 59df4ba..034ea91 100644 --- a/src/lang/en_US.ts +++ b/src/lang/en_US.ts @@ -18,6 +18,7 @@ export default { language: 'Language', dashboard: 'Dashboard', document: 'Document', + message: 'Message', layoutSize: 'Layout Size', selectTenant: 'Select Tenant', layoutSetting: 'Layout Setting', diff --git a/src/lang/zh_CN.ts b/src/lang/zh_CN.ts index d778f7d..666a400 100644 --- a/src/lang/zh_CN.ts +++ b/src/lang/zh_CN.ts @@ -17,6 +17,7 @@ export default { language: '语言', dashboard: '首页', document: '项目文档', + message: '消息', layoutSize: '布局大小', selectTenant: '选择租户', layoutSetting: '布局设置', diff --git a/src/layout/components/Navbar.vue b/src/layout/components/Navbar.vue index 3c0e45d..03fcedb 100644 --- a/src/layout/components/Navbar.vue +++ b/src/layout/components/Navbar.vue @@ -27,6 +27,21 @@ + + + + + + + + + + + + + + + @@ -81,11 +96,14 @@ import { getTenantList } from "@/api/login"; import { dynamicClear, dynamicTenant } from "@/api/system/tenant"; import { ComponentInternalInstance } from "vue"; import { TenantVO } from "@/api/types"; +import notice from './notice/index.vue'; +import useNoticeStore from '@/store/modules/notice'; const appStore = useAppStore(); const userStore = useUserStore(); const settingsStore = useSettingsStore(); - +const noticeStore = storeToRefs(useNoticeStore()); +const newNotice = ref(0); const { proxy } = getCurrentInstance() as ComponentInternalInstance; const userId = ref(userStore.userId); @@ -161,6 +179,10 @@ const handleCommand = (command: string) => { commandMap[command](); } } +//用深度监听 +watch(() => noticeStore.state.value.notices, (newVal, oldVal) => { + newNotice.value = newVal.length; +}, { deep: true }); diff --git a/src/store/modules/notice.ts b/src/store/modules/notice.ts new file mode 100644 index 0000000..2f645a6 --- /dev/null +++ b/src/store/modules/notice.ts @@ -0,0 +1,33 @@ +import { defineStore } from 'pinia'; + +interface NoticeItem { + title?: string; + message: any; + time: string; +} + +export const useNoticeStore = defineStore('notice', () => { + const state = reactive({ + notices: [] as NoticeItem[] + }); + + const addNotice = (notice: NoticeItem) => { + state.notices.push(notice); + }; + + const removeNotice = (notice: NoticeItem) => { + state.notices.splice(state.notices.indexOf(notice), 1); + }; + + const clearNotice = () => { + state.notices = []; + }; + return { + state, + addNotice, + removeNotice, + clearNotice + }; +}); + +export default useNoticeStore; diff --git a/src/utils/websocket.ts b/src/utils/websocket.ts index 791b5f5..0560ed4 100644 --- a/src/utils/websocket.ts +++ b/src/utils/websocket.ts @@ -19,6 +19,9 @@ */ import { getToken } from '@/utils/auth'; +import useNoticeStore from '@/store/modules/notice'; + +const { addNotice } = useNoticeStore(); let socketUrl: any = ''; // socket地址 let websocket: any = null; // websocket 实例 @@ -37,17 +40,13 @@ export const initWebSocket = (url: any) => { websocketonerror(); websocketclose(); sendSocketHeart(); + return websocket; }; -onMounted(() => { - // 初始化websocket - initWebSocket(socketUrl); -}); - // socket 连接成功 export const websocketonopen = () => { - websocket.onopen = function (e: any) { - console.log('连接 websocket 成功', e); + websocket.onopen = function () { + console.log('连接 websocket 成功'); resetHeart(); }; }; @@ -76,7 +75,6 @@ export const resetHeart = () => { // socket心跳发送 export const sendSocketHeart = () => { - console.log(websocket); heartTime = setInterval(() => { // 如果连接正常则发送心跳 if (websocket.readyState == 1) { @@ -120,15 +118,14 @@ export const websocketonmessage = () => { const msg = JSON.parse(e.data) as any; if (msg.type === 'heartbeat') { resetHeart(); - console.log('心跳'); } if (msg.type === 'ping') { - console.log('收到心跳', socketHeart); return; } - ElNotification({ - title: '收到一条消息', - message: msg + addNotice({ + message: msg, + time: new Date().toLocaleString() }); + return msg; }; }; diff --git a/src/views/tool/webSocket/index.vue b/src/views/tool/webSocket/index.vue new file mode 100644 index 0000000..3517666 --- /dev/null +++ b/src/views/tool/webSocket/index.vue @@ -0,0 +1,63 @@ + + + + + + + + + + + 连接 + 断开 + + + + + + 发送 + + + + + + + + + +