refactor: 抽离未读告警状态,不再持久化

This commit is contained in:
yangsy
2026-01-15 13:58:13 +08:00
parent de35075be8
commit b59b34e68b
9 changed files with 67 additions and 49 deletions

View File

@@ -159,7 +159,7 @@ type IndexedDbStoreId = typeof NDM_STATION_STORE_ID | typeof NDM_DEVICE_STORE_ID
type IndexedDbStoreStates = {
[NDM_STATION_STORE_ID]: { stations: Station[] };
[NDM_DEVICE_STORE_ID]: { lineDevices: LineDevices };
[NDM_ALARM_STORE_ID]: { lineAlarms: LineAlarms; unreadLineAlarms: LineAlarms };
[NDM_ALARM_STORE_ID]: { lineAlarms: LineAlarms };
};
const exportFromIndexedDB = async <K extends IndexedDbStoreId>(storeId: K, options?: { errorMsg?: string }) => {
const { errorMsg } = options ?? {};

View File

@@ -1,6 +1,6 @@
import type { NdmDeviceAlarmLogResultVO, Station, SyncCameraResult } from '@/apis';
import { ALARM_TOPIC, SYNC_CAMERA_STATUS_TOPIC } from '@/constants';
import { useAlarmStore, useSettingStore, useStationStore } from '@/stores';
import { useSettingStore, useStationStore, useUnreadStore } from '@/stores';
import { Client } from '@stomp/stompjs';
import { watchDebounced } from '@vueuse/core';
import destr from 'destr';
@@ -19,8 +19,10 @@ const getBrokerUrl = () => {
export const useStompClient = () => {
const stationStore = useStationStore();
const { stations } = storeToRefs(stationStore);
const alarmStore = useAlarmStore();
const { unreadLineAlarms } = storeToRefs(alarmStore);
const unreadStore = useUnreadStore();
const { unreadLineAlarms } = storeToRefs(unreadStore);
const settingStore = useSettingStore();
const { offlineDev } = storeToRefs(settingStore);
@@ -40,8 +42,9 @@ export const useStompClient = () => {
console.log('Stomp连接成功');
stompClient.value?.subscribe(ALARM_TOPIC, (message) => {
const alarm = destr<NdmDeviceAlarmLogResultVO>(message.body);
if (alarm.alarmCategory === '1') {
alarmStore.pushUnreadAlarm(alarm);
const { alarmCategory, stationCode } = alarm;
if (alarmCategory === '1' && !!stations.value.find((station) => station.code === stationCode)) {
unreadStore.pushUnreadAlarm(alarm);
}
});
stompClient.value?.subscribe(SYNC_CAMERA_STATUS_TOPIC, (message) => {

View File

@@ -3,4 +3,5 @@ export const NDM_DEVICE_STORE_ID = 'ndm-device-store';
export const NDM_POLLIING_STORE_ID = 'ndm-polling-store';
export const NDM_SETTING_STORE_ID = 'ndm-setting-store';
export const NDM_STATION_STORE_ID = 'ndm-station-store';
export const NDM_UNREAD_STORE_ID = 'ndm-unread-store';
export const NDM_USER_STORE_ID = 'ndm-user-store';

View File

@@ -2,7 +2,7 @@
import { SettingsDrawer, SyncCameraResultModal } from '@/components';
import { useLineStationsQuery, useStompClient, useVerifyUserQuery } from '@/composables';
import { LINE_ALARMS_QUERY_KEY, LINE_DEVICES_QUERY_KEY, LINE_STATIONS_MUTATION_KEY, LINE_STATIONS_QUERY_KEY, STATION_ALARMS_MUTATION_KEY, STATION_DEVICES_MUTATION_KEY } from '@/constants';
import { useAlarmStore, useSettingStore, useUserStore } from '@/stores';
import { useSettingStore, useUnreadStore, useUserStore } from '@/stores';
import { parseErrorFeedback } from '@/utils';
import { useIsFetching, useIsMutating, useMutation } from '@tanstack/vue-query';
import { isCancel } from 'axios';
@@ -33,8 +33,8 @@ const router = useRouter();
const userStore = useUserStore();
const { userInfo } = storeToRefs(userStore);
const alarmStore = useAlarmStore();
const { unreadAlarmCount } = storeToRefs(alarmStore);
const unreadStore = useUnreadStore();
const { unreadAlarmCount } = storeToRefs(unreadStore);
const settingStore = useSettingStore();
const { menuCollpased, offlineDev } = storeToRefs(settingStore);
@@ -141,7 +141,7 @@ const routeToRoot = () => {
};
const routeToAlarmPage = () => {
alarmStore.clearUnreadAlarms();
unreadStore.clearUnreadAlarms();
if (route.path !== '/alarm/alarm-log') {
router.push({ path: '/alarm/alarm-log' });
}

View File

@@ -3,7 +3,7 @@ import { exportDeviceAlarmLogApi, pageDeviceAlarmLogApi, type NdmDeviceAlarmLog,
import { useAlarmActionColumn, useCameraSnapColumn } from '@/composables';
import { ALARM_TYPES, DEVICE_TYPE_CODES, DEVICE_TYPE_LITERALS, DEVICE_TYPE_NAMES, FAULT_LEVELS, tryGetDeviceType, type DeviceType } from '@/enums';
import { renderAlarmDateCell, renderAlarmTypeCell, renderDeviceTypeCell, renderFaultLevelCell } from '@/helpers';
import { useAlarmStore, useDeviceStore, useStationStore } from '@/stores';
import { useDeviceStore, useStationStore, useUnreadStore } from '@/stores';
import { downloadByData, parseErrorFeedback } from '@/utils';
import { useMutation } from '@tanstack/vue-query';
import { watchDebounced } from '@vueuse/core';
@@ -44,10 +44,12 @@ const router = useRouter();
const stationStore = useStationStore();
const { stations } = storeToRefs(stationStore);
const deviceStore = useDeviceStore();
const { lineDevices } = storeToRefs(deviceStore);
const alarmStore = useAlarmStore();
const { unreadAlarmCount } = storeToRefs(alarmStore);
const unreadStore = useUnreadStore();
const { unreadAlarmCount } = storeToRefs(unreadStore);
const stationSelectOptions = computed<SelectOption[]>(() => {
return stations.value.map((station) => ({

View File

@@ -1,8 +1,7 @@
import { initStationAlarms, type LineAlarms, type NdmDeviceAlarmLogResultVO, type Station, type StationAlarms } from '@/apis';
import { type LineAlarms, type Station, type StationAlarms } from '@/apis';
import { NDM_ALARM_STORE_ID } from '@/constants';
import { tryGetDeviceType } from '@/enums';
import { defineStore } from 'pinia';
import { computed, shallowRef, triggerRef } from 'vue';
import { shallowRef, triggerRef } from 'vue';
export const useAlarmStore = defineStore(
NDM_ALARM_STORE_ID,
@@ -18,42 +17,10 @@ export const useAlarmStore = defineStore(
triggerRef(lineAlarms);
};
// 全线所有车站的未读告警 (来自stomp订阅)
const unreadLineAlarms = shallowRef<LineAlarms>({});
const unreadAlarmCount = computed(() => {
let count = 0;
Object.values(unreadLineAlarms.value).forEach((stationAlarms) => {
count += stationAlarms['unclassified'].length;
});
return count;
});
const pushUnreadAlarm = (alarm: NdmDeviceAlarmLogResultVO) => {
const stationCode = alarm.stationCode;
if (!stationCode) return;
if (!unreadLineAlarms.value[stationCode]) {
unreadLineAlarms.value[stationCode] = initStationAlarms();
}
const deviceType = tryGetDeviceType(alarm.deviceType);
if (!deviceType) return;
const stationAlarms = unreadLineAlarms.value[stationCode];
stationAlarms[deviceType].push(alarm);
stationAlarms['unclassified'].push(alarm);
triggerRef(unreadLineAlarms);
};
const clearUnreadAlarms = () => {
unreadLineAlarms.value = {};
triggerRef(unreadLineAlarms);
};
return {
lineAlarms,
setLineAlarms,
setStationAlarms,
unreadLineAlarms,
unreadAlarmCount,
pushUnreadAlarm,
clearUnreadAlarms,
};
},
{

View File

@@ -3,4 +3,5 @@ export * from './device';
export * from './polling';
export * from './setting';
export * from './station';
export * from './unread';
export * from './user';

44
src/stores/unread.ts Normal file
View File

@@ -0,0 +1,44 @@
import { initStationAlarms, type LineAlarms, type NdmDeviceAlarmLogResultVO } from '@/apis';
import { NDM_UNREAD_STORE_ID } from '@/constants';
import { tryGetDeviceType } from '@/enums';
import { defineStore } from 'pinia';
import { computed, shallowRef, triggerRef } from 'vue';
export const useUnreadStore = defineStore(NDM_UNREAD_STORE_ID, () => {
// 全线所有车站的未读告警 (来自stomp订阅)
const unreadLineAlarms = shallowRef<LineAlarms>({});
const unreadAlarmCount = computed(() => {
let count = 0;
Object.values(unreadLineAlarms.value).forEach((stationAlarms) => {
count += stationAlarms['unclassified'].length;
});
return count;
});
const pushUnreadAlarm = (alarm: NdmDeviceAlarmLogResultVO) => {
const stationCode = alarm.stationCode;
if (!stationCode) return;
if (!unreadLineAlarms.value[stationCode]) {
unreadLineAlarms.value[stationCode] = initStationAlarms();
}
const deviceType = tryGetDeviceType(alarm.deviceType);
if (!deviceType) return;
const stationAlarms = unreadLineAlarms.value[stationCode];
stationAlarms[deviceType].push(alarm);
stationAlarms['unclassified'].push(alarm);
triggerRef(unreadLineAlarms);
};
const clearUnreadAlarms = () => {
unreadLineAlarms.value = {};
triggerRef(unreadLineAlarms);
};
return {
unreadLineAlarms,
unreadAlarmCount,
pushUnreadAlarm,
clearUnreadAlarms,
};
});