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

298 lines
11 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
2026-05-07 14:13:26 +08:00
} from 'antd';
2026-05-13 09:06:50 +08:00
import { EyeOutlined, NotificationOutlined } 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;
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({
alarmLevel: '',
handleStatus: '',
});
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) => {
2026-05-13 09:06:50 +08:00
setQueryParams(values);
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') : '-'
},
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 }}
>
<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>
<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>
<svg t="1778634297838" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9839" width="16" height="16"><path d="M761.984 1009.28H253.696C138.368 1009.28 44.8 915.712 44.8 800.384V292.096C44.8 176.768 138.368 83.2 253.696 83.2h508.288c115.328 0 208.896 93.568 208.896 208.896v508.288c0 115.328-93.568 208.896-208.896 208.896z" fill="#00E560" p-id="9840"></path><path d="M507.776 265.856c-72.576 0-139.776 20.352-193.536 54.528-54.272 34.432-94.592 83.2-113.408 139.264-7.552 22.528-11.648 46.464-11.648 71.04 0 92.16 56.448 172.928 142.208 220.544 7.68 4.224 11.648 13.44 8.32 21.632-6.4 15.616-14.592 30.592-24.32 44.672-3.072 4.352 0.768 10.368 6.016 8.96 29.44-7.808 56.96-19.072 81.92-33.28 9.088-5.248 19.456-6.912 29.696-4.864 23.936 4.736 49.024 7.296 74.88 7.296 176 0 318.592-118.656 318.592-264.832 0-146.304-142.72-264.96-318.72-264.96z" fill="#FFFFFF" p-id="9841"></path></svg>
<span>短信</span>
<Tag color={currentAlarm.notifySms === 1 ? 'green' : 'default'}>
{currentAlarm.notifySms === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
<Space>
<svg t="1778634173765" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8764" width="16" height="16"><path d="M512.003 79C272.855 79 79 272.855 79 512.003 79 751.145 272.855 945 512.003 945 751.145 945 945 751.145 945 512.003 945 272.855 751.145 79 512.003 79z m200.075 375.014c-0.867 3.764-3.117 9.347-6.234 16.012h0.087l-0.347 0.648c-18.183 38.86-65.631 115.108-65.631 115.108l-0.215-0.52-13.856 24.147h66.8L565.063 779l29.002-115.368h-52.598l18.27-76.29c-14.76 3.55-32.253 8.436-52.945 15.1 0 0-27.967 16.36-80.607-31.5 0 0-35.501-31.29-14.891-39.078 8.744-3.33 42.466-7.573 69.004-11.122 35.93-4.845 57.965-7.441 57.965-7.441s-110.607 1.643-136.841-2.468c-26.237-4.11-59.525-47.905-66.626-86.377 0 0-10.953-21.117 23.595-11.122 34.547 10 177.535 38.95 177.535 38.95s-185.933-56.992-198.36-70.929c-12.381-13.846-36.406-75.902-33.289-113.981 0 0 1.343-9.521 11.127-6.926 0 0 137.49 62.75 231.475 97.152 94.028 34.403 175.76 51.885 165.2 96.414z" fill="#3AA2EB" p-id="8765"></path></svg>
<span>钉钉</span>
<Tag color={currentAlarm.notifyTelegram === 1 ? 'green' : 'default'}>
{currentAlarm.notifyTelegram === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
<Space>
<svg t="1778634149974" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7716" width="16" height="16"><path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#608BE9" p-id="7717"></path><path d="M192 302m32 0l576 0q32 0 32 32l0 356q0 32-32 32l-576 0q-32 0-32-32l0-356q0-32 32-32Z" fill="#EAEDF5" p-id="7718"></path><path d="M224 722h576c17.673 0 32-14.327 32-32v-58C660.96 493.333 554.294 424 512 424c-42.294 0-148.96 69.333-320 208v58c0 17.673 14.327 32 32 32z" fill="#CCDAF7" p-id="7719"></path><path d="M224 302h576c17.673 0 32 14.327 32 32v58C651.35 517.333 544.683 580 512 580c-32.683 0-139.35-62.667-320-188v-58c0-17.673 14.327-32 32-32z" fill="#FFFFFF" p-id="7720"></path></svg>
<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;