Files
gofly-platform-services/zlt-web/react-web-next/src/pages/device/Wayline/components/WaylineTable.tsx
T

104 lines
3.3 KiB
TypeScript

import React, { forwardRef, useImperativeHandle, useRef } from 'react';
import type { ActionType } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import type { ProColumns } from '@ant-design/pro-components';
import { Space, message } from 'antd';
import { DownloadOutlined } from '@ant-design/icons';
import { deviceApi } from '@/services/device';
import { downloadByUrl } from '@/utils/download';
export interface WaylineTableRef {
reload: () => void;
}
interface WaylineTableProps {
params?: Record<string, any>;
onEdit?: (record: any) => void;
onDelete?: (record: any) => void;
}
const WaylineTable = forwardRef<WaylineTableRef, WaylineTableProps>(({ params, onEdit, onDelete }, ref) => {
const actionRef = useRef<ActionType>();
useImperativeHandle(ref, () => ({ reload: () => actionRef.current?.reload() }));
/**
* 处理航线文件下载
* 先通过waylineId获取航线信息(包含fileUrl),再使用fileUrl下载
*/
const handleDownload = async (record: any) => {
try {
if (!record.waylineId) {
message.error('航线ID缺失');
return;
}
const hide = message.loading('获取航线信息中...');
try {
// 通过waylineId获取航线完整信息,包括fileUrl
const waylineInfo = await deviceApi.getWaylineInfoByWaylineId(record.waylineId);
hide();
if (!waylineInfo) {
message.error('无法获取航线信息');
return;
}
const fileUrl = waylineInfo.fileUrl;
if (!fileUrl) {
message.error('航线文件URL不存在');
return;
}
// 使用fileUrl直接下载
const fileName = record.name || `wayline_${record.waylineId}`;
downloadByUrl(fileUrl, fileName);
message.success('开始下载航线文件');
} catch (error) {
hide();
console.error('获取航线信息失败:', error);
message.error('获取航线信息失败');
}
} catch (error) {
message.error('下载失败');
console.error('下载航线文件错误:', error);
}
};
const columns: ProColumns<any>[] = [
{ dataIndex: 'index', valueType: 'indexBorder', width: 48 },
{ title: '航线名称', dataIndex: 'name' },
{ title: '航线ID', dataIndex: 'waylineId' },
{ title: '空间ID', dataIndex: 'workspaceId' },
{ title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime', width: 180 },
{ title: '文件签名', dataIndex: 'sign', ellipsis: true, copyable: true },
{
title: '操作', key: 'action', valueType: 'option',
render: (_, record) => (
<Space>
<a onClick={() => onEdit?.(record)}>编辑</a>
<a onClick={() => handleDownload(record)} title="下载航线文件">
<DownloadOutlined /> 下载
</a>
<a style={{ color: '#1677ff' }} onClick={() => onDelete?.(record)}>删除</a>
</Space>
)
},
];
return (
<ProTable<any>
rowKey="id"
headerTitle="航线列表"
actionRef={actionRef}
request={(p) => deviceApi.wayline({ ...p, ...params })}
columns={columns}
search={false}
pagination={{ showSizeChanger: true, showQuickJumper: true }}
/>
);
});
export default WaylineTable;