60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { ref } from 'vue'
|
|
import { getAlertDetails } from '@/api/dashboard'
|
|
|
|
export interface AlertItem {
|
|
text: string
|
|
error?: boolean
|
|
warn?: boolean
|
|
}
|
|
|
|
export function useAlertManager() {
|
|
const alertDetails = ref<AlertItem[]>([])
|
|
const timeoutDetails = ref<AlertItem[]>([])
|
|
let alertRotationInterval: NodeJS.Timeout | null = null
|
|
let currentAlertType = 1
|
|
|
|
const fetchAlertDetails = async (type: 'risk' | 'timeout'): Promise<AlertItem[]> => {
|
|
try {
|
|
return await getAlertDetails(type)
|
|
} catch (error) {
|
|
console.error('获取告警详情失败:', error)
|
|
return []
|
|
}
|
|
}
|
|
|
|
const startAlertRotation = async (): Promise<void> => {
|
|
// 初始化数据
|
|
alertDetails.value = await fetchAlertDetails('risk')
|
|
timeoutDetails.value = await fetchAlertDetails('timeout')
|
|
|
|
// 启动轮播
|
|
alertRotationInterval = setInterval(async () => {
|
|
if (currentAlertType === 1) {
|
|
currentAlertType = 2
|
|
alertDetails.value = await fetchAlertDetails('risk')
|
|
} else {
|
|
currentAlertType = 1
|
|
timeoutDetails.value = await fetchAlertDetails('timeout')
|
|
}
|
|
}, 3000)
|
|
}
|
|
|
|
const stopAlertRotation = (): void => {
|
|
if (alertRotationInterval) {
|
|
clearInterval(alertRotationInterval)
|
|
alertRotationInterval = null
|
|
}
|
|
}
|
|
|
|
return {
|
|
alertDetails,
|
|
timeoutDetails,
|
|
startAlertRotation,
|
|
stopAlertRotation,
|
|
// 返回清理函数,让调用方在组件卸载时调用
|
|
cleanup: () => {
|
|
stopAlertRotation()
|
|
}
|
|
}
|
|
}
|