feat: 航线管理页面优化
This commit is contained in:
@@ -25,7 +25,9 @@
|
|||||||
"Bash(git commit:*)",
|
"Bash(git commit:*)",
|
||||||
"Read(//f/**)",
|
"Read(//f/**)",
|
||||||
"Bash(git checkout:*)",
|
"Bash(git checkout:*)",
|
||||||
"Bash(git diff:*)"
|
"Bash(git diff:*)",
|
||||||
|
"Bash(grep:*)",
|
||||||
|
"Bash(npm run tsc:*)"
|
||||||
],
|
],
|
||||||
"deny": [],
|
"deny": [],
|
||||||
"ask": []
|
"ask": []
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
"moment": "^2.29.0",
|
"moment": "^2.29.0",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
|
"spark-md5": "^3.0.2",
|
||||||
"umi": "^3.5.0",
|
"umi": "^3.5.0",
|
||||||
"xlsx": "^0.18.5"
|
"xlsx": "^0.18.5"
|
||||||
},
|
},
|
||||||
@@ -35,6 +36,7 @@
|
|||||||
"@types/file-saver": "^2.0.7",
|
"@types/file-saver": "^2.0.7",
|
||||||
"@types/react": "^17.0.0",
|
"@types/react": "^17.0.0",
|
||||||
"@types/react-dom": "^17.0.0",
|
"@types/react-dom": "^17.0.0",
|
||||||
|
"@types/spark-md5": "^3.0.5",
|
||||||
"@umijs/preset-react": "^2.1.0",
|
"@umijs/preset-react": "^2.1.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"typescript": "^4.5.0"
|
"typescript": "^4.5.0"
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import React, { useEffect, useRef, useState } from 'react';
|
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 { Button, Upload, message } from 'antd';
|
||||||
import { UploadOutlined } from '@ant-design/icons';
|
import { UploadOutlined } from '@ant-design/icons';
|
||||||
import { deviceApi } from '@/services/device';
|
import { deviceApi } from '@/services/device';
|
||||||
|
import { filesApi } from '@/services/files';
|
||||||
|
import SparkMD5 from 'spark-md5';
|
||||||
|
|
||||||
export type WaylineFormValues = {
|
export type WaylineFormValues = {
|
||||||
id?: number;
|
id?: number;
|
||||||
name?: string;
|
name?: string;
|
||||||
workspaceId?: string;
|
|
||||||
waylineId?: string;
|
waylineId?: string;
|
||||||
sign?: string;
|
sign?: string;
|
||||||
|
workspaceId?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface WaylineFormProps {
|
interface WaylineFormProps {
|
||||||
@@ -18,22 +20,36 @@ interface WaylineFormProps {
|
|||||||
onVisibleChange: (v: boolean) => void;
|
onVisibleChange: (v: boolean) => void;
|
||||||
onFinish: (values: WaylineFormValues) => Promise<boolean>;
|
onFinish: (values: WaylineFormValues) => Promise<boolean>;
|
||||||
initialValues?: Partial<WaylineFormValues>;
|
initialValues?: Partial<WaylineFormValues>;
|
||||||
workspaceOptions?: Array<{ label: string; value: string }>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 简单上传到文件服务,返回 id/url/path
|
/**
|
||||||
async function uploadWaylineFile(file: File) {
|
* 计算文件的 MD5 值
|
||||||
const formData = new FormData();
|
*/
|
||||||
formData.append('file', file);
|
async function calculateFileMD5(file: File): Promise<string> {
|
||||||
const resp = await fetch('/api-file/files-anon', { method: 'POST', body: formData });
|
return new Promise((resolve, reject) => {
|
||||||
const data = await resp.json();
|
const reader = new FileReader();
|
||||||
if (!data || !data.id) throw new Error('上传失败');
|
const spark = new SparkMD5.ArrayBuffer();
|
||||||
return data as { id: string; url: string; path: string };
|
|
||||||
|
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 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);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,13 +66,14 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
|||||||
id: data.id,
|
id: data.id,
|
||||||
name: data.name,
|
name: data.name,
|
||||||
waylineId: data.waylineId,
|
waylineId: data.waylineId,
|
||||||
workspaceId: data.workspaceId,
|
|
||||||
sign: data.sign,
|
sign: data.sign,
|
||||||
});
|
});
|
||||||
// 如果有fileUrl,设置文件信息
|
// 如果有fileUrl,设置文件信息
|
||||||
if (data.fileUrl) {
|
if (data.fileUrl) {
|
||||||
setFileInfo({
|
setFileInfo({
|
||||||
id: data.waylineId || data.id?.toString() || '',
|
id: data.waylineId || data.id?.toString() || '',
|
||||||
|
name: data.name || 'wayline_file',
|
||||||
|
md5: data.sign || '',
|
||||||
url: data.fileUrl,
|
url: data.fileUrl,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -78,7 +95,6 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
|||||||
title={title}
|
title={title}
|
||||||
visible={visible}
|
visible={visible}
|
||||||
formRef={formRef}
|
formRef={formRef}
|
||||||
loading={loading}
|
|
||||||
onVisibleChange={(v) => {
|
onVisibleChange={(v) => {
|
||||||
onVisibleChange?.(v);
|
onVisibleChange?.(v);
|
||||||
if (!v) {
|
if (!v) {
|
||||||
@@ -93,32 +109,17 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
|||||||
}
|
}
|
||||||
const payload: WaylineFormValues = {
|
const payload: WaylineFormValues = {
|
||||||
...initialValues,
|
...initialValues,
|
||||||
...values,
|
name: fileInfo?.name || values.name, // 优先使用上传的文件名
|
||||||
waylineId: values.waylineId || fileInfo?.id,
|
waylineId: values.waylineId || fileInfo?.id,
|
||||||
|
sign: fileInfo?.md5 || values.sign,
|
||||||
|
workspaceId: '1', // 工作区默认为 1
|
||||||
};
|
};
|
||||||
return onFinish(payload);
|
return onFinish(payload);
|
||||||
}}
|
}}
|
||||||
modalProps={{ destroyOnClose: true }}
|
modalProps={{ destroyOnClose: true, confirmLoading: loading }}
|
||||||
>
|
>
|
||||||
<ProForm.Group>
|
{/* 上传文件放在第一位 */}
|
||||||
<ProFormText
|
<div style={{ marginBottom: 12, fontWeight: 500 }}>航线文件</div>
|
||||||
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>
|
|
||||||
<Upload
|
<Upload
|
||||||
maxCount={1}
|
maxCount={1}
|
||||||
accept=".kmz,.kml"
|
accept=".kmz,.kml"
|
||||||
@@ -126,13 +127,29 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
|||||||
customRequest={async ({ file, onSuccess, onError }) => {
|
customRequest={async ({ file, onSuccess, onError }) => {
|
||||||
try {
|
try {
|
||||||
if (file instanceof File) {
|
if (file instanceof File) {
|
||||||
const r = await uploadWaylineFile(file);
|
// 计算文件 MD5
|
||||||
setFileInfo({ id: r.id, url: r.url });
|
const md5 = await calculateFileMD5(file);
|
||||||
formRef.current?.setFieldsValue({ waylineId: r.id });
|
|
||||||
|
// 上传文件
|
||||||
|
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('上传成功');
|
message.success('上传成功');
|
||||||
onSuccess?.('ok');
|
onSuccess?.('ok');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('上传错误:', err);
|
||||||
message.error('上传失败');
|
message.error('上传失败');
|
||||||
onError?.(err as any);
|
onError?.(err as any);
|
||||||
}
|
}
|
||||||
@@ -141,8 +158,24 @@ const WaylineForm: React.FC<WaylineFormProps> = ({ title, visible, onVisibleChan
|
|||||||
<Button icon={<UploadOutlined />}>{fileInfo ? '重新上传' : '上传文件'}</Button>
|
<Button icon={<UploadOutlined />}>{fileInfo ? '重新上传' : '上传文件'}</Button>
|
||||||
</Upload>
|
</Upload>
|
||||||
{fileInfo && (
|
{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>
|
</ModalForm>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -57,14 +57,6 @@ export const deviceApi = {
|
|||||||
const res = await request(`/api-device/wayline/url/${id}`, { method: 'GET' });
|
const res = await request(`/api-device/wayline/url/${id}`, { method: 'GET' });
|
||||||
return (res as any).data || '';
|
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 }) => {
|
deviceList: async (params: { page?: number; limit?: number; [k:string]: any }) => {
|
||||||
const res = await request('/api-device/device', { method: 'GET', params });
|
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 };
|
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' });
|
const res = await request(`/api-file/files/${id}`, { method: 'DELETE' });
|
||||||
return (res as any).data || res;
|
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