feat: 航线管理页面优化
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
"moment": "^2.29.0",
|
||||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
"spark-md5": "^3.0.2",
|
||||
"umi": "^3.5.0",
|
||||
"xlsx": "^0.18.5"
|
||||
},
|
||||
@@ -35,6 +36,7 @@
|
||||
"@types/file-saver": "^2.0.7",
|
||||
"@types/react": "^17.0.0",
|
||||
"@types/react-dom": "^17.0.0",
|
||||
"@types/spark-md5": "^3.0.5",
|
||||
"@umijs/preset-react": "^2.1.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"typescript": "^4.5.0"
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { ModalForm, ProForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
|
||||
import { ModalForm, ProForm, ProFormText } from '@ant-design/pro-components';
|
||||
import { Button, Upload, message } from 'antd';
|
||||
import { UploadOutlined } from '@ant-design/icons';
|
||||
import { deviceApi } from '@/services/device';
|
||||
import { filesApi } from '@/services/files';
|
||||
import SparkMD5 from 'spark-md5';
|
||||
|
||||
export type WaylineFormValues = {
|
||||
id?: number;
|
||||
name?: string;
|
||||
workspaceId?: string;
|
||||
waylineId?: string;
|
||||
sign?: string;
|
||||
workspaceId?: string;
|
||||
};
|
||||
|
||||
interface WaylineFormProps {
|
||||
@@ -18,22 +20,36 @@ interface WaylineFormProps {
|
||||
onVisibleChange: (v: boolean) => void;
|
||||
onFinish: (values: WaylineFormValues) => Promise<boolean>;
|
||||
initialValues?: Partial<WaylineFormValues>;
|
||||
workspaceOptions?: Array<{ label: string; value: string }>;
|
||||
}
|
||||
|
||||
// 简单上传到文件服务,返回 id/url/path
|
||||
async function uploadWaylineFile(file: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
const resp = await fetch('/api-file/files-anon', { method: 'POST', body: formData });
|
||||
const data = await resp.json();
|
||||
if (!data || !data.id) throw new Error('上传失败');
|
||||
return data as { id: string; url: string; path: string };
|
||||
/**
|
||||
* 计算文件的 MD5 值
|
||||
*/
|
||||
async function calculateFileMD5(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
const spark = new SparkMD5.ArrayBuffer();
|
||||
|
||||
reader.onload = (e) => {
|
||||
try {
|
||||
spark.append(e.target?.result as ArrayBuffer);
|
||||
resolve(spark.end());
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
|
||||
reader.onerror = () => {
|
||||
reject(new Error('文件读取失败'));
|
||||
};
|
||||
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
|
||||
const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChange, onFinish, initialValues, workspaceOptions }) => {
|
||||
const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChange, onFinish, initialValues }) => {
|
||||
const formRef = useRef<any>();
|
||||
const [fileInfo, setFileInfo] = useState<{ id: string; url: string } | undefined>();
|
||||
const [fileInfo, setFileInfo] = useState<{ id: string; name: string; md5: string; url: string } | undefined>();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
/**
|
||||
@@ -50,13 +66,14 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
waylineId: data.waylineId,
|
||||
workspaceId: data.workspaceId,
|
||||
sign: data.sign,
|
||||
});
|
||||
// 如果有fileUrl,设置文件信息
|
||||
if (data.fileUrl) {
|
||||
setFileInfo({
|
||||
id: data.waylineId || data.id?.toString() || '',
|
||||
name: data.name || 'wayline_file',
|
||||
md5: data.sign || '',
|
||||
url: data.fileUrl,
|
||||
});
|
||||
}
|
||||
@@ -78,7 +95,6 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
||||
title={title}
|
||||
visible={visible}
|
||||
formRef={formRef}
|
||||
loading={loading}
|
||||
onVisibleChange={(v) => {
|
||||
onVisibleChange?.(v);
|
||||
if (!v) {
|
||||
@@ -93,32 +109,17 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
||||
}
|
||||
const payload: WaylineFormValues = {
|
||||
...initialValues,
|
||||
...values,
|
||||
name: fileInfo?.name || values.name, // 优先使用上传的文件名
|
||||
waylineId: values.waylineId || fileInfo?.id,
|
||||
sign: fileInfo?.md5 || values.sign,
|
||||
workspaceId: '1', // 工作区默认为 1
|
||||
};
|
||||
return onFinish(payload);
|
||||
}}
|
||||
modalProps={{ destroyOnClose: true }}
|
||||
modalProps={{ destroyOnClose: true, confirmLoading: loading }}
|
||||
>
|
||||
<ProForm.Group>
|
||||
<ProFormText
|
||||
name="name"
|
||||
label="航线名称"
|
||||
rules={[{ required: true, message: '请输入航线名称' }]}
|
||||
width="md"
|
||||
/>
|
||||
<ProFormSelect
|
||||
name="workspaceId"
|
||||
label="工作区"
|
||||
rules={[{ required: true, message: '请选择工作区' }]}
|
||||
options={workspaceOptions}
|
||||
width="md"
|
||||
/>
|
||||
<ProFormText name="waylineId" hidden />
|
||||
<ProFormText name="sign" hidden />
|
||||
</ProForm.Group>
|
||||
|
||||
<div style={{ marginBottom: 12 }}>航线文件</div>
|
||||
{/* 上传文件放在第一位 */}
|
||||
<div style={{ marginBottom: 12, fontWeight: 500 }}>航线文件</div>
|
||||
<Upload
|
||||
maxCount={1}
|
||||
accept=".kmz,.kml"
|
||||
@@ -126,13 +127,29 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
||||
customRequest={async ({ file, onSuccess, onError }) => {
|
||||
try {
|
||||
if (file instanceof File) {
|
||||
const r = await uploadWaylineFile(file);
|
||||
setFileInfo({ id: r.id, url: r.url });
|
||||
formRef.current?.setFieldsValue({ waylineId: r.id });
|
||||
// 计算文件 MD5
|
||||
const md5 = await calculateFileMD5(file);
|
||||
|
||||
// 上传文件
|
||||
const fileData = await filesApi.uploadFile(file);
|
||||
|
||||
setFileInfo({
|
||||
id: fileData.id,
|
||||
name: fileData.name,
|
||||
md5: md5,
|
||||
url: fileData.url,
|
||||
});
|
||||
|
||||
formRef.current?.setFieldsValue({
|
||||
waylineId: fileData.id,
|
||||
name: fileData.name, // 自动填充返回的文件名到航线名称
|
||||
sign: md5,
|
||||
});
|
||||
message.success('上传成功');
|
||||
onSuccess?.('ok');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('上传错误:', err);
|
||||
message.error('上传失败');
|
||||
onError?.(err as any);
|
||||
}
|
||||
@@ -141,8 +158,24 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
||||
<Button icon={<UploadOutlined />}>{fileInfo ? '重新上传' : '上传文件'}</Button>
|
||||
</Upload>
|
||||
{fileInfo && (
|
||||
<div style={{ marginTop: 8, color: '#666' }}>已上传:{fileInfo.id}</div>
|
||||
<div style={{ marginTop: 8, padding: '8px 12px', backgroundColor: '#f5f5f5', borderRadius: 4 }}>
|
||||
<div style={{ color: '#666', fontSize: 12 }}>已上传文件:</div>
|
||||
<div style={{ color: '#1677ff', fontWeight: 500, marginTop: 4 }}>{fileInfo.name}</div>
|
||||
<div style={{ color: '#999', fontSize: 12, marginTop: 4 }}>MD5: {fileInfo.md5}</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<ProForm.Group style={{ marginTop: 24 }}>
|
||||
<ProFormText
|
||||
name="name"
|
||||
label="航线名称"
|
||||
placeholder="请输入航线名称或使用上传的文件名"
|
||||
rules={[{ required: true, message: '请输入航线名称' }]}
|
||||
width="md"
|
||||
/>
|
||||
<ProFormText name="waylineId" hidden />
|
||||
<ProFormText name="sign" hidden />
|
||||
</ProForm.Group>
|
||||
</ModalForm>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -57,14 +57,6 @@ export const deviceApi = {
|
||||
const res = await request(`/api-device/wayline/url/${id}`, { method: 'GET' });
|
||||
return (res as any).data || '';
|
||||
},
|
||||
/**
|
||||
* 下载航线文件
|
||||
* @param id 航线文件ID
|
||||
* @returns 下载URL
|
||||
*/
|
||||
downloadWaylineFile: (id: number) => {
|
||||
return `/api-device/wayline/download/${id}`;
|
||||
},
|
||||
deviceList: async (params: { page?: number; limit?: number; [k:string]: any }) => {
|
||||
const res = await request('/api-device/device', { method: 'GET', params });
|
||||
return { data: (res as any).data, total: (res as any).count } as { data: DeviceItem[]; total: number };
|
||||
|
||||
@@ -13,5 +13,36 @@ export const filesApi = {
|
||||
const res = await request(`/api-file/files/${id}`, { method: 'DELETE' });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
/**
|
||||
* 上传文件通用接口
|
||||
* @param file 上传的文件
|
||||
* @returns 文件完整信息对象
|
||||
*/
|
||||
uploadFile: async (file: File) => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const resp = await request('/api-file/files-anon', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
const data = (resp as any);
|
||||
if (!data || !data.id) throw new Error('上传失败');
|
||||
|
||||
return data as {
|
||||
id: string;
|
||||
name: string;
|
||||
isImg: boolean;
|
||||
contentType: string;
|
||||
size: number;
|
||||
path: string;
|
||||
url: string;
|
||||
source: string;
|
||||
storageType: string | null;
|
||||
createTime: string;
|
||||
updateTime: string;
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user