From 46c0976fdfebfc32bcc8ef2b1595e7ecd0ad98ed Mon Sep 17 00:00:00 2001 From: duzhichao <1098974448@qq.com> Date: Tue, 4 Nov 2025 14:47:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=88=AA=E7=BA=BF=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/settings.local.json | 4 +- zlt-web/react-web-next/package.json | 2 + .../device/Wayline/components/WaylineForm.tsx | 113 +++++++++++------- zlt-web/react-web-next/src/services/device.ts | 8 -- zlt-web/react-web-next/src/services/files.ts | 31 +++++ 5 files changed, 109 insertions(+), 49 deletions(-) diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 405339eb..5711bb90 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -25,7 +25,9 @@ "Bash(git commit:*)", "Read(//f/**)", "Bash(git checkout:*)", - "Bash(git diff:*)" + "Bash(git diff:*)", + "Bash(grep:*)", + "Bash(npm run tsc:*)" ], "deny": [], "ask": [] diff --git a/zlt-web/react-web-next/package.json b/zlt-web/react-web-next/package.json index da8550f3..717adb24 100644 --- a/zlt-web/react-web-next/package.json +++ b/zlt-web/react-web-next/package.json @@ -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" diff --git a/zlt-web/react-web-next/src/pages/device/Wayline/components/WaylineForm.tsx b/zlt-web/react-web-next/src/pages/device/Wayline/components/WaylineForm.tsx index 01b723ed..7d8468d7 100644 --- a/zlt-web/react-web-next/src/pages/device/Wayline/components/WaylineForm.tsx +++ b/zlt-web/react-web-next/src/pages/device/Wayline/components/WaylineForm.tsx @@ -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; initialValues?: Partial; - 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 { + 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 = ({ title, visible, onVisibleChange, onFinish, initialValues, workspaceOptions }) => { +const WaylineForm: React.FC = ({ title, visible, onVisibleChange, onFinish, initialValues }) => { const formRef = useRef(); - 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 = ({ 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 = ({ 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 = ({ 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 }} > - - - - - -
航线文件
+ {/* 上传文件放在第一位 */} +
航线文件
= ({ 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 = ({ title, visible, onVisibleChan {fileInfo && ( -
已上传:{fileInfo.id}
+
+
已上传文件:
+
{fileInfo.name}
+
MD5: {fileInfo.md5}
+
)} + + + + ); }; diff --git a/zlt-web/react-web-next/src/services/device.ts b/zlt-web/react-web-next/src/services/device.ts index 575357e0..295547d5 100644 --- a/zlt-web/react-web-next/src/services/device.ts +++ b/zlt-web/react-web-next/src/services/device.ts @@ -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 }; diff --git a/zlt-web/react-web-next/src/services/files.ts b/zlt-web/react-web-next/src/services/files.ts index 97652cbd..15be4253 100644 --- a/zlt-web/react-web-next/src/services/files.ts +++ b/zlt-web/react-web-next/src/services/files.ts @@ -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; + }; + }, };