useAlarmNotify.ts
3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { AlarmStatus, AlarmStatusMean } from '../config/detail.config';
import { clearOrAckAlarm, getDeviceAlarm } from '/@/api/device/deviceManager';
import { notification, Button, Tag } from 'ant-design-vue';
import { h, onMounted, onUnmounted } from 'vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { alarmLevel } from '/@/views/device/list/config/detail.config';
import { RoleEnum } from '/@/enums/roleEnum';
import { usePermission } from '/@/hooks/web/usePermission';
import { useUserStore } from '/@/store/modules/user';
interface UseAlarmNotifyParams {
alarmNotifyStatus?: AlarmStatus;
interval?: number;
color?: string;
duration?: number;
}
export function useAlarmNotify(params: UseAlarmNotifyParams = {}) {
const {
alarmNotifyStatus = AlarmStatus.ACTIVE_UN_ACK,
interval = import.meta.env.VITE_ALARM_NOTIFY_POLLING_INTERVAL_TIME,
duration = import.meta.env.VITE_ALARM_NOTIFY_DURATION,
color = 'orange',
} = params;
const alarmNotifyStatusMean = AlarmStatusMean[alarmNotifyStatus];
const handleMarkRead = async (id: string) => {
try {
await clearOrAckAlarm(id, false);
} catch (error) {}
};
let timeout: Nullable<NodeJS.Timer> = null;
const getAlarmLog = async () => {
try {
const { items = [] } =
(await getDeviceAlarm({ status: alarmNotifyStatus, page: 1, pageSize: 10 })) || {};
if (items.length) {
const first = items.at(0)!;
const { deviceName, id, severity } = first;
let key: Nullable<string> = `open-notify-${id}`;
const severityMean = alarmLevel(severity);
notification.open({
message: '设备告警',
duration: Number(duration),
key,
description: h('div', {}, [
h('div', { style: { marginRight: '5px' } }, [
h('span', { style: { marginRight: '5px' } }, '设备:'),
h('span', {}, `[${deviceName}]`),
]),
h('div', { style: { marginTop: '5px' } }, [
h('span', { style: { marginRight: '5px' } }, '告警状态:'),
h(Tag, { color }, () => `${alarmNotifyStatusMean}`),
]),
h('div', { style: { marginTop: '5px' } }, [
h('span', { style: { marginRight: '5px' } }, '告警级别:'),
h(Tag, { color: '#f50' }, () => `${severityMean}`),
]),
]),
icon: h(ExclamationCircleOutlined, { style: { color: '#faa22d' } }),
onClose: () => {
key = null;
},
btn: h(
Button,
{
type: 'primary',
size: 'small',
onClick: async () => {
await handleMarkRead(id);
notification.close(key!);
key = null;
},
},
() => '标记已读'
),
});
}
} catch (error) {}
};
const polling = () => {
timeout = setInterval(() => {
getAlarmLog();
}, interval);
};
const { hasPermission } = usePermission();
onMounted(() => {
const alarmPermissionKey = 'api:alarm:global:notify';
const hasPermissionRole = [RoleEnum.CUSTOMER_USER, RoleEnum.TENANT_ADMIN];
const userInfo = useUserStore().getUserInfo;
const userRoles = userInfo.roles || [];
const getPermissionFlag = () => {
for (const item of userRoles) {
const flag = hasPermissionRole.find((each) => item === each);
if (flag) return true;
}
return false;
};
if (hasPermission(alarmPermissionKey) && getPermissionFlag()) polling();
});
onUnmounted(() => {
clearInterval(timeout as NodeJS.Timer);
timeout = null;
// currentNotifyId = null;
});
}