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

338 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, 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-07-20 13:56:10 +08:00
import { alarmTypeMap } from '@/constants.js';
2026-08-12 11:29:44 +08:00
import { useTranslation } from 'react-i18next';
import { t } from 'i18next';
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 alarmLevelConfig = {
2026-08-12 11:29:44 +08:00
1: { description: t('constants.critical'), color: '#FF4D4F' },
2: { description: t('constants.important'), color: '#FA8C16' },
3: { description: t('constants.general'), color: '#FADB14' },
4: { description: t('constants.info'), color: '#1890FF' }
2026-05-13 09:28:34 +08:00
};
const handleStatusMap = {
2026-08-12 11:29:44 +08:00
1: t('constants.pending'),
2026-08-14 09:02:52 +08:00
2: t('alerts.processing'),
2026-08-12 11:29:44 +08:00
3: t('constants.closed'),
4: t('constants.ignored')
2026-05-13 09:28:34 +08:00
};
// 自动生成颜色/文字映射
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 = () => {
2026-08-12 11:29:44 +08:00
const { t } = useTranslation();
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-08-14 09:02:52 +08:00
message.error(t('common.loadFailed'));
} 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 {
2026-08-14 09:02:52 +08:00
message.error(t('common.loadDetailFailed'));
}
} catch (err) {
2026-08-14 09:02:52 +08:00
message.error(t('common.requestError'));
2026-05-13 09:06:50 +08:00
} 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 || '',
2026-07-20 13:56:10 +08:00
alarmType: values.alarmType || '',
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-08-14 09:02:52 +08:00
{ title: t('alerts.alarmNo'), dataIndex: 'alarmNo', width: 180 },
{
2026-08-12 11:29:44 +08:00
title: t('alerts.alertTime'),
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-08-14 09:02:52 +08:00
{ title: t('common.deviceId'), dataIndex: 'deviceId', width: 160 },
2026-08-12 11:29:44 +08:00
{ title: t('devices.deviceName'), dataIndex: 'deviceName', ellipsis: true },
2026-07-20 13:56:10 +08:00
{
2026-08-12 11:29:44 +08:00
title: t('alerts.alertType'),
2026-07-20 13:56:10 +08:00
dataIndex: 'alarmType',
width: 130,
render: (t) => alarmTypeMap[t] || '-'
},
2026-05-07 14:13:26 +08:00
{
2026-08-12 11:29:44 +08:00
title: t('alerts.alertLevel'),
width: 100,
render: (r) => (
<Tag color={levelColor[r.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
{levelText[r.alarmLevel]}
</Tag>
)
},
2026-08-14 09:02:52 +08:00
{ title: t('alerts.alarmTitle'), dataIndex: 'alarmTitle', ellipsis: true, width: 180 },
{ title: t('alerts.alarmContent'), dataIndex: 'alarmContent', ellipsis: true },
2026-05-07 14:13:26 +08:00
{
2026-08-12 11:29:44 +08:00
title: t('alerts.handleStatus'),
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
},
{
2026-08-12 11:29:44 +08:00
title: t('roles.actions'),
width: 120,
2026-05-13 09:06:50 +08:00
fixed: 'right',
render: (_, r) => (
<Button type="link" icon={<EyeOutlined />} onClick={() => showDetail(r.id)}>
2026-08-14 09:02:52 +08:00
{t('common.viewDetail')}
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' }}>
2026-08-14 09:02:52 +08:00
<Card title={t('alerts.alarmHistory')} style={{ borderRadius: 8 }}>
<Form
form={form}
layout="inline"
onFinish={handleSearch}
style={{ marginBottom: 16, flexWrap: 'wrap', gap: 16 }}
>
{/* 设备ID */}
2026-08-14 09:02:52 +08:00
<Form.Item name="deviceId" label={t('common.deviceId')}>
<Input placeholder={t('common.pleaseInputDeviceId')} style={{ width: 160 }} allowClear />
</Form.Item>
2026-08-12 11:29:44 +08:00
<Form.Item name="alarmLevel" label={t('alerts.alertLevel')}>
<Select placeholder={t('common.all')} style={{ width: 130 }} allowClear>
<Option value={1}>{t('constants.critical')}</Option>
<Option value={2}>{t('constants.important')}</Option>
<Option value={3}>{t('constants.general')}</Option>
<Option value={4}>{t('constants.info')}</Option>
</Select>
</Form.Item>
2026-08-12 11:29:44 +08:00
<Form.Item name="alarmType" label={t('alerts.alertType')}>
<Select placeholder={t('common.all')} style={{ width: 150 }} allowClear>
<Option value="UAV_FAULT">{t('constants.uavFault')}</Option>
<Option value="MOWER_FAULT">{t('constants.mowerFault')}</Option>
<Option value="OTHER_DEVICE_FAULT">{t('constants.otherDeviceFault')}</Option>
<Option value="SERVER_FAILURE">{t('constants.serverFailure')}</Option>
<Option value="SYSTEM_ERROR">{t('constants.systemError')}</Option>
2026-07-20 13:56:10 +08:00
</Select>
</Form.Item>
2026-08-12 11:29:44 +08:00
<Form.Item name="handleStatus" label={t('alerts.handleStatus')}>
<Select placeholder={t('common.all')} style={{ width: 130 }} allowClear>
<Option value={1}>{t('constants.pending')}</Option>
<Option value={2}>{t('constants.closed')}</Option>
</Select>
</Form.Item>
{/* 时间范围 */}
2026-08-14 09:02:52 +08:00
<Form.Item name="timeRange" label={t('common.timeRange')}>
<RangePicker
showTime
format="YYYY-MM-DD HH:mm:ss"
style={{ width: 350 }}
/>
</Form.Item>
<Form.Item>
<Space>
2026-08-12 11:29:44 +08:00
<Button type="primary" htmlType="submit">{t('common.search2')}</Button>
<Button htmlType="reset">{t('common.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
2026-08-14 09:02:52 +08:00
showTotal={(total) => `${t('common.total')} ${total} ${t('common.items')}`}
/>
</div>
</Card>
2026-05-07 14:13:26 +08:00
{/* 右侧详情抽屉 */}
2026-05-13 09:06:50 +08:00
<Drawer
2026-08-14 09:02:52 +08:00
title={t('alerts.alertDetail')}
2026-05-13 09:06:50 +08:00
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}>
2026-08-14 09:02:52 +08:00
<Descriptions.Item label={t('alerts.alarmNo')}>{currentAlarm?.alarmNo || '-'}</Descriptions.Item>
<Descriptions.Item label={t('alerts.alarmTitle')}>{currentAlarm?.alarmTitle || '-'}</Descriptions.Item>
<Descriptions.Item label={t('alerts.alarmContent')}>{currentAlarm?.alarmContent || '-'}</Descriptions.Item>
<Descriptions.Item label={t('common.deviceId')}>{currentAlarm?.deviceId || '-'}</Descriptions.Item>
2026-08-12 11:29:44 +08:00
<Descriptions.Item label={t('devices.deviceName')}>{currentAlarm?.deviceName || '-'}</Descriptions.Item>
<Descriptions.Item label={t('alerts.alertTime')}>
2026-05-13 09:06:50 +08:00
{currentAlarm?.alarmTime ? dayjs(currentAlarm.alarmTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
</Descriptions.Item>
2026-08-12 11:29:44 +08:00
<Descriptions.Item label={t('alerts.alertType')}>{alarmTypeMap[currentAlarm?.alarmType] || '-'}</Descriptions.Item>
<Descriptions.Item label={t('alerts.alertLevel')}>
2026-05-13 09:06:50 +08:00
<Tag color={levelColor[currentAlarm?.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
{levelText[currentAlarm?.alarmLevel]}
</Tag>
</Descriptions.Item>
2026-08-12 11:29:44 +08:00
<Descriptions.Item label={t('alerts.handleStatus')}>
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>
2026-08-12 11:29:44 +08:00
<Descriptions.Item label={t('alerts.handler')}>{currentAlarm?.handleUserName || '-'}</Descriptions.Item>
<Descriptions.Item label={t('alerts.handleTime')}>
2026-05-13 09:06:50 +08:00
{currentAlarm?.handleTime ? dayjs(currentAlarm.handleTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
</Descriptions.Item>
2026-08-12 11:29:44 +08:00
<Descriptions.Item label={t('alerts.handleRemark')}>{currentAlarm?.handleRemark || '-'}</Descriptions.Item>
2026-08-14 09:02:52 +08:00
<Descriptions.Item label={t('alerts.rootCause')}>{currentAlarm?.rootCause || '-'}</Descriptions.Item>
<Descriptions.Item label={t('alerts.recommendedAction')}>
2026-05-13 09:06:50 +08:00
{currentAlarm?.handleSuggestion || '-'}
</Descriptions.Item>
</Descriptions>
{/* 通知订阅模块 */}
2026-05-07 14:13:26 +08:00
{currentAlarm && (
2026-08-14 09:02:52 +08:00
<Card size="small" title={t('alerts.notifySubscription')} style={{ marginTop: 16 }}>
<Space style={{ width: '100%' }}>
<Space>
<span>{t('alerts.sms')}:</span>
<Tag color={currentAlarm.notifySms === 1 ? 'green' : 'default'}>
{currentAlarm.notifySms === 1 ? t('common.enabled') : t('constants.closed')}
</Tag>
</Space>
<Space>
<span>{t('alerts.dingTalk')}:</span>
<Tag color={currentAlarm.notifyTelegram === 1 ? 'green' : 'default'}>
{currentAlarm.notifyTelegram === 1 ? t('common.enabled') : t('constants.closed')}
</Tag>
</Space>
<Space>
<span>{t('alerts.email')}:</span>
<Tag color={currentAlarm.notifyEmail === 1 ? 'green' : 'default'}>
{currentAlarm.notifyEmail === 1 ? t('common.enabled') : t('constants.closed')}
</Tag>
2026-05-13 09:06:50 +08:00
</Space>
2026-08-14 09:02:52 +08:00
</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;