diff --git a/apps/web-antd/src/store/auth.ts b/apps/web-antd/src/store/auth.ts index 56a6fdd2..2160ba05 100644 --- a/apps/web-antd/src/store/auth.ts +++ b/apps/web-antd/src/store/auth.ts @@ -16,8 +16,6 @@ import { $t } from '#/locales' import { useDictStore } from './dict' -import { initWebSocket, getWebSocketService } from '#/api/websocket' - export const useAuthStore = defineStore('auth', () => { const accessStore = useAccessStore() const userStore = useUserStore() @@ -81,13 +79,6 @@ export const useAuthStore = defineStore('auth', () => { try { await seeConnectionClose() await doLogout() - /** - * 断开websocket连接 - */ - const ws = getWebSocketService(); - if(ws) { - ws.close(); - } } catch (error) { console.error(error) } finally { @@ -128,11 +119,6 @@ export const useAuthStore = defineStore('auth', () => { } userStore.setUserInfo(userInfo) - /** - * 初始化websocket - */ - initWebSocket() - /** * 需要重新加载字典 * 比如退出登录切换到其他租户 diff --git a/apps/web-antd/src/store/notify.ts b/apps/web-antd/src/store/notify.ts index a183ba15..00e81fa4 100644 --- a/apps/web-antd/src/store/notify.ts +++ b/apps/web-antd/src/store/notify.ts @@ -1,21 +1,21 @@ -import type { NotificationItem } from '@vben/layouts'; +import type { NotificationItem } from '@vben/layouts' -import { computed, ref, watch } from 'vue'; +import { computed, ref, watch } from 'vue' -import { useAppConfig } from '@vben/hooks'; -import { SvgMessageUrl } from '@vben/icons'; -import { $t } from '@vben/locales'; -import { useAccessStore, useUserStore } from '@vben/stores'; +import { useAppConfig } from '@vben/hooks' +import { SvgMessageUrl } from '@vben/icons' +import { $t } from '@vben/locales' +import { useAccessStore, useUserStore } from '@vben/stores' -import { useEventSource } from '@vueuse/core'; -import { notification } from 'ant-design-vue'; -import dayjs from 'dayjs'; -import { defineStore } from 'pinia'; +import { useEventSource } from '@vueuse/core' +import { notification } from 'ant-design-vue' +import dayjs from 'dayjs' +import { defineStore } from 'pinia' const { apiURL, clientId, sseEnable } = useAppConfig( import.meta.env, import.meta.env.PROD, -); +) export const useNotifyStore = defineStore( 'app-notify', @@ -23,18 +23,18 @@ export const useNotifyStore = defineStore( /** * return才会被持久化 存储全部消息 */ - const notificationList = ref([]); - const sseList = ref(["111"]); - const userStore = useUserStore(); + const notificationList = ref([]) + const sseList = ref(["111"]) + const userStore = useUserStore() const userId = computed(() => { - return userStore.userInfo?.userId || '0'; - }); + return userStore.userInfo?.userId || '0' + }) const notifications = computed(() => { return notificationList.value.filter( (item) => item.userId === userId.value, - ); - }); + ) + }) /** * 开始监听sse消息 @@ -44,40 +44,45 @@ export const useNotifyStore = defineStore( * 未开启 不监听 */ if (!sseEnable) { - return; + return } - const accessStore = useAccessStore(); - const token = accessStore.accessToken; + const accessStore = useAccessStore() + const token = accessStore.accessToken - const sseAddr = `${apiURL}/resource/sse?clientid=${clientId}&Authorization=Bearer ${token}`; + const sseAddr = `${apiURL}/resource/sse?clientid=${clientId}&Authorization=Bearer ${token}` const { data } = useEventSource(sseAddr, [], { autoReconnect: { delay: 1000, onFailed() { - console.error('sse重连失败.'); + console.error('sse重连失败.') }, retries: 3, }, - }); + }) watch(data, (message) => { - if (!message) return; - console.log(`接收到消息: ${message}`); + if (!message) return + console.log(`接收到消息: ${message}`) try { // 尝试解析JSON - const obj = JSON.parse(message); + const obj = JSON.parse(message) // 检查解析结果是否为对象且不为null - if (obj.getType() ==="yvjin"){ - sseList.value.join(message) + if (obj.type === "yvjin") { + sseList.value.push(message) + } + + // 仪表数据 + if (obj.type === "meter") { + sseList.value.push(message) } } catch (e) { notification.success({ description: message, duration: 3, message: $t('component.notice.received'), - }); + }) notificationList.value.unshift({ // avatar: `https://api.multiavatar.com/${random(0, 10_000)}.png`, 随机头像 @@ -87,12 +92,12 @@ export const useNotifyStore = defineStore( message, title: $t('component.notice.title'), userId: userId.value, - }); + }) // 需要手动置空 vue3在值相同时不会触发watch - data.value = null; + data.value = null } - }); + }) } /** @@ -102,10 +107,10 @@ export const useNotifyStore = defineStore( notificationList.value .filter((item) => item.userId === userId.value) .forEach((item) => { - item.isRead = true; - }); + item.isRead = true + }) } - function getsseList(){ + function getsseList() { console.log(sseList.value) return sseList.value } @@ -115,7 +120,7 @@ export const useNotifyStore = defineStore( * @param item 通知 */ function setRead(item: NotificationItem) { - !item.isRead && (item.isRead = true); + !item.isRead && (item.isRead = true) } /** @@ -124,7 +129,7 @@ export const useNotifyStore = defineStore( function clearAllMessage() { notificationList.value = notificationList.value.filter( (item) => item.userId !== userId.value, - ); + ) } /** @@ -141,7 +146,7 @@ export const useNotifyStore = defineStore( notificationList.value .filter((item) => item.userId === userId.value) .some((item) => !item.isRead), - ); + ) return { $reset, @@ -154,11 +159,11 @@ export const useNotifyStore = defineStore( setRead, showDot, startListeningMessage, - }; + } }, { persist: { pick: ['notificationList'], }, }, -); +) diff --git a/apps/web-antd/src/views/property/energyManagement/electricEnergy/elctricitySituation/index.vue b/apps/web-antd/src/views/property/energyManagement/electricEnergy/elctricitySituation/index.vue index e5642255..767526bb 100644 --- a/apps/web-antd/src/views/property/energyManagement/electricEnergy/elctricitySituation/index.vue +++ b/apps/web-antd/src/views/property/energyManagement/electricEnergy/elctricitySituation/index.vue @@ -1,45 +1,49 @@