Files
web-Iot/src/components/alerts/AlarmHistory.tsx

327 lines
9.3 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
2026-05-07 14:13:26 +08:00
import {
2026-05-13 09:06:50 +08:00
Card, Table, Tag, Space, Button,
Drawer, Descriptions, Form, Select, Pagination, message, DatePicker, Input
2026-05-07 14:13:26 +08:00
} from 'antd';
import { EyeOutlined } from '@ant-design/icons';
import dayjs from 'dayjs';
2026-05-13 09:06:50 +08:00
// 接口
import {
getAlarmList,
getAlarmDetail
} from '../../api/alarmApi.js';
2026-05-07 14:13:26 +08:00
const { Option } = Select;
const { RangePicker } = DatePicker;
2026-05-07 14:13:26 +08:00
2026-05-13 09:28:34 +08:00
// ====================== 枚举配置 ======================
const alarmTypeMap = {
1: '设备故障',
2: '数据异常',
3: '通信中断',
4: '性能告警',
5: '安全告警',
6: '环境告警'
};
const alarmLevelConfig = {
1: { description: '严重', color: '#FF4D4F' },
2: { description: '重要', color: '#FA8C16' },
3: { description: '一般', color: '#FADB14' },
4: { description: '提示', color: '#1890FF' }
};
const handleStatusMap = {
1: '待处理',
2: '处理中',
3: '已关闭',
4: '已忽略'
};
// 自动生成颜色/文字映射
const levelColor = Object.keys(alarmLevelConfig).reduce((map, key) => {
map[key] = alarmLevelConfig[key].color;
return map;
}, {});
const levelText = Object.keys(alarmLevelConfig).reduce((map, key) => {
map[key] = alarmLevelConfig[key].description;
return map;
}, {});
2026-05-07 14:13:26 +08:00
const AlarmHistory = () => {
const [form] = Form.useForm();
2026-05-13 09:06:50 +08:00
// 抽屉
const [drawerVisible, setDrawerVisible] = useState(false);
const [currentAlarm, setCurrentAlarm] = useState(null);
2026-05-13 09:06:50 +08:00
const [detailLoading, setDetailLoading] = useState(false);
2026-05-13 09:06:50 +08:00
// 列表
const [loading, setLoading] = useState(false);
const [alarmList, setAlarmList] = useState([]);
const [total, setTotal] = useState(0);
const [pageNum, setPageNum] = useState(1);
const [pageSize, setPageSize] = useState(10);
// 查询条件
const [queryParams, setQueryParams] = useState({
deviceId: '',
alarmLevel: '',
handleStatus: '',
startTime: '',
endTime: '',
});
2026-05-13 09:06:50 +08:00
// 获取列表
const fetchList = async () => {
setLoading(true);
try {
2026-05-13 09:06:50 +08:00
const res = await getAlarmList({ pageNum, pageSize, ...queryParams });
if (res.code === 0) {
setAlarmList(res.rows || []);
setTotal(res.total || 0);
}
} catch (err) {
2026-05-13 09:06:50 +08:00
message.error('加载失败');
} finally {
setLoading(false);
}
2026-05-07 14:13:26 +08:00
};
2026-05-13 09:06:50 +08:00
// 查看详情(打开抽屉)
const showDetail = async (id) => {
2026-05-13 09:06:50 +08:00
setDetailLoading(true);
try {
const res = await getAlarmDetail(id);
if (res.code === 200) {
setCurrentAlarm(res.data);
2026-05-13 09:06:50 +08:00
setDrawerVisible(true);
} else {
message.error('获取详情失败');
}
} catch (err) {
2026-05-13 09:06:50 +08:00
message.error('请求异常');
} finally {
setDetailLoading(false);
}
2026-05-07 14:13:26 +08:00
};
// 搜索
const handleSearch = (values) => {
let startTime = '';
let endTime = '';
if (values.timeRange && values.timeRange.length === 2) {
startTime = values.timeRange[0].format('YYYY-MM-DD HH:mm:ss');
endTime = values.timeRange[1].format('YYYY-MM-DD HH:mm:ss');
}
setQueryParams({
deviceId: values.deviceId || '',
alarmLevel: values.alarmLevel || '',
handleStatus: values.handleStatus || '',
startTime,
endTime,
});
setPageNum(1);
2026-05-07 14:13:26 +08:00
};
useEffect(() => {
fetchList();
}, [pageNum, pageSize, queryParams]);
2026-05-07 14:13:26 +08:00
// 表格列
const columns = [
2026-05-13 09:06:50 +08:00
{ title: '告警编号', dataIndex: 'alarmNo', width: 180 },
{
title: '告警时间',
2026-05-13 09:06:50 +08:00
dataIndex: 'alarmTime',
width: 190,
2026-05-13 09:06:50 +08:00
render: (t) => t ? dayjs(t).format('YYYY-MM-DD HH:mm:ss') : '-'
},
{ title: '设备ID', dataIndex: 'deviceId', width: 160 },
2026-05-13 09:06:50 +08:00
{ title: '设备名称', dataIndex: 'deviceName', ellipsis: true },
2026-05-07 14:13:26 +08:00
{
title: '告警级别',
width: 100,
render: (r) => (
<Tag color={levelColor[r.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
{levelText[r.alarmLevel]}
</Tag>
)
},
2026-05-13 09:06:50 +08:00
{ title: '告警标题', dataIndex: 'alarmTitle', ellipsis: true, width: 180 },
{ title: '告警内容', dataIndex: 'alarmContent', ellipsis: true },
2026-05-07 14:13:26 +08:00
{
title: '处理状态',
2026-05-13 09:06:50 +08:00
width: 110,
2026-05-13 09:28:34 +08:00
render: (r) => {
const s = r.handleStatus;
let color = 'default';
if (s === 1) color = 'orange';
if (s === 2) color = 'blue';
if (s === 3) color = 'green';
if (s === 4) color = 'default';
return <Tag color={color}>{handleStatusMap[s] || '-'}</Tag>;
}
2026-05-07 14:13:26 +08:00
},
{
title: '操作',
width: 120,
2026-05-13 09:06:50 +08:00
fixed: 'right',
render: (_, r) => (
<Button type="link" icon={<EyeOutlined />} onClick={() => showDetail(r.id)}>
2026-05-07 14:13:26 +08:00
查看详情
</Button>
)
}
];
return (
2026-05-13 09:06:50 +08:00
<div style={{ padding: 24, background: '#f5f7fa', minHeight: '100vh' }}>
<Card title="告警历史" style={{ borderRadius: 8 }}>
<Form
form={form}
layout="inline"
onFinish={handleSearch}
style={{ marginBottom: 16, flexWrap: 'wrap', gap: 16 }}
>
{/* 设备ID */}
<Form.Item name="deviceId" label="设备ID">
<Input placeholder="请输入设备ID" style={{ width: 160 }} allowClear />
</Form.Item>
<Form.Item name="alarmLevel" label="告警级别">
<Select placeholder="全部" style={{ width: 130 }} allowClear>
<Option value={1}>严重</Option>
<Option value={2}>重要</Option>
<Option value={3}>一般</Option>
<Option value={4}>提示</Option>
</Select>
</Form.Item>
<Form.Item name="handleStatus" label="处理状态">
<Select placeholder="全部" style={{ width: 130 }} allowClear>
2026-05-13 09:28:34 +08:00
<Option value={1}>待处理</Option>
<Option value={2}>处理中</Option>
<Option value={3}>已关闭</Option>
<Option value={4}>已忽略</Option>
</Select>
</Form.Item>
{/* 时间范围 */}
<Form.Item name="timeRange" label="时间范围">
<RangePicker
showTime
format="YYYY-MM-DD HH:mm:ss"
style={{ width: 350 }}
/>
</Form.Item>
<Form.Item>
<Space>
<Button type="primary" htmlType="submit">查询</Button>
<Button htmlType="reset">重置</Button>
</Space>
</Form.Item>
</Form>
2026-05-07 14:13:26 +08:00
<Table
rowKey="id"
columns={columns}
dataSource={alarmList}
loading={loading}
pagination={false}
scroll={{ x: 'max-content' }}
2026-05-07 14:13:26 +08:00
/>
<div style={{ textAlign: 'center', marginTop: 16 }}>
<Pagination
current={pageNum}
pageSize={pageSize}
total={total}
2026-05-13 09:06:50 +08:00
onChange={(p, s) => { setPageNum(p); setPageSize(s); }}
showSizeChanger
showQuickJumper
showTotal={(total) => `共 ${total} 条`}
/>
</div>
</Card>
2026-05-07 14:13:26 +08:00
{/* 右侧详情抽屉 */}
2026-05-13 09:06:50 +08:00
<Drawer
title="告警详情"
open={drawerVisible}
onClose={() => setDrawerVisible(false)}
width={650}
destroyOnClose
2026-05-07 14:13:26 +08:00
>
2026-05-13 09:06:50 +08:00
<Descriptions column={1} bordered size="small" loading={detailLoading}>
<Descriptions.Item label="告警编号">{currentAlarm?.alarmNo || '-'}</Descriptions.Item>
<Descriptions.Item label="告警标题">{currentAlarm?.alarmTitle || '-'}</Descriptions.Item>
<Descriptions.Item label="告警内容">{currentAlarm?.alarmContent || '-'}</Descriptions.Item>
<Descriptions.Item label="设备ID">{currentAlarm?.deviceId || '-'}</Descriptions.Item>
<Descriptions.Item label="设备名称">{currentAlarm?.deviceName || '-'}</Descriptions.Item>
<Descriptions.Item label="告警时间">
{currentAlarm?.alarmTime ? dayjs(currentAlarm.alarmTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
</Descriptions.Item>
<Descriptions.Item label="告警级别">
<Tag color={levelColor[currentAlarm?.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
{levelText[currentAlarm?.alarmLevel]}
</Tag>
</Descriptions.Item>
<Descriptions.Item label="处理状态">
2026-05-13 09:28:34 +08:00
{(() => {
const s = currentAlarm?.handleStatus;
let color = 'default';
if (s === 1) color = 'orange';
if (s === 2) color = 'blue';
if (s === 3) color = 'green';
if (s === 4) color = 'default';
return <Tag color={color}>{handleStatusMap[s] || '-'}</Tag>;
})()}
2026-05-13 09:06:50 +08:00
</Descriptions.Item>
<Descriptions.Item label="处理人">{currentAlarm?.handleUserName || '-'}</Descriptions.Item>
<Descriptions.Item label="处理时间">
{currentAlarm?.handleTime ? dayjs(currentAlarm.handleTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
</Descriptions.Item>
<Descriptions.Item label="处理备注">{currentAlarm?.handleRemark || '-'}</Descriptions.Item>
<Descriptions.Item label="根因分析">{currentAlarm?.rootCause || '-'}</Descriptions.Item>
<Descriptions.Item label="推荐处理措施">
{currentAlarm?.handleSuggestion || '-'}
</Descriptions.Item>
</Descriptions>
{/* 通知订阅模块 */}
2026-05-07 14:13:26 +08:00
{currentAlarm && (
2026-05-13 09:06:50 +08:00
<Card size="small" title="通知订阅" style={{ marginTop: 16 }}>
<Space style={{ width: '100%' }}>
<Space>
<span>短信:</span>
2026-05-13 09:06:50 +08:00
<Tag color={currentAlarm.notifySms === 1 ? 'green' : 'default'}>
{currentAlarm.notifySms === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
<Space>
<span>钉钉:</span>
2026-05-13 09:06:50 +08:00
<Tag color={currentAlarm.notifyTelegram === 1 ? 'green' : 'default'}>
{currentAlarm.notifyTelegram === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
<Space>
<span>邮件:</span>
<Tag color={currentAlarm.notifyEmail === 1 ? 'green' : 'default'}>
{currentAlarm.notifyEmail === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
</Space>
</Card>
2026-05-07 14:13:26 +08:00
)}
2026-05-13 09:06:50 +08:00
</Drawer>
2026-05-07 14:13:26 +08:00
</div>
);
};
export default AlarmHistory;