From 066b9ab327c5d7364374ad1f06ef1c969a02f0a4 Mon Sep 17 00:00:00 2001 From: zlwu <704541015@qq.com> Date: Wed, 2 Dec 2026 20:28:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(web-admin):=20=E8=B4=A8=E6=A3=80=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E9=99=84=E4=BB=B6=E5=8C=85=E5=90=AB=E5=B7=A5=E5=8D=95?= =?UTF-8?q?=E7=BA=A7=E9=99=84=E4=BB=B6=20(#186)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 导出 zip 增加 {工单文件夹}/工单附件/ 路径,与任务附件一并打包;仅工单附件也可导出。 Co-authored-by: Cursor --- ...qc-export-work-order-attachments-design.md | 19 +++ .../qc-work-order-attachment-export.test.ts | 151 ++++++++++++++++++ .../utils/qc-work-order-attachment-export.ts | 30 ++++ 3 files changed, 200 insertions(+) create mode 100644 docs/superpowers/specs/2026-12-02-qc-export-work-order-attachments-design.md create mode 100644 web-admin/src/utils/qc-work-order-attachment-export.test.ts diff --git a/docs/superpowers/specs/2026-12-02-qc-export-work-order-attachments-design.md b/docs/superpowers/specs/2026-12-02-qc-export-work-order-attachments-design.md new file mode 100644 index 00000000..b270185f --- /dev/null +++ b/docs/superpowers/specs/2026-12-02-qc-export-work-order-attachments-design.md @@ -0,0 +1,19 @@ +# 质检导出附件包含工单级附件 + +关联 Gitea #186。 + +## 目标 + +质检工单「导出附件」在保留任务附件的同时,打包工单级 `attachments`。 + +## 设计 + +- 仅改 `web-admin`;列表已返回 `attachments`,下载走现有接口。 +- ZIP 路径:`{工单文件夹}/工单附件/{文件名}`。 +- 仅有工单附件也可导出;两者皆无仍提示暂无附件。 + +## 验收 + +1. 有工单附件时 zip 含 `工单附件/` 目录。 +2. 任务附件结构不变。 +3. 仅工单附件可导出;皆无提示「所选工单暂无附件」。 diff --git a/web-admin/src/utils/qc-work-order-attachment-export.test.ts b/web-admin/src/utils/qc-work-order-attachment-export.test.ts new file mode 100644 index 00000000..713a3ef2 --- /dev/null +++ b/web-admin/src/utils/qc-work-order-attachment-export.test.ts @@ -0,0 +1,151 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert/strict' +import { register } from 'node:module' +import { pathToFileURL } from 'node:url' + +// 隔离附件导出模块的 HTTP / 打包依赖,只测 zip 条目构建。 +const emptyStub = + 'data:text/javascript,' + + encodeURIComponent(` + export function fetchAttachmentDownloadUrl() { return Promise.resolve(null) } + export async function downloadAttachmentFile() {} + export function attachmentFullUrl(url) { return String(url || '') } + export default class JSZip { file() {} async generateAsync() { return new Blob() } } + export const saveAs = () => {} + `) + +const hookSource = ` +const emptyStub = ${JSON.stringify(emptyStub)} +export async function resolve(specifier, context, nextResolve) { + const base = String(specifier).split('?')[0] + if ( + base === '@/api/business' || + base.endsWith('/api/business') || + base === '@/utils/download-attachment' || + base.endsWith('/utils/download-attachment') || + base === '@/composables/useAttachmentActions' || + base.endsWith('/composables/useAttachmentActions') || + base === 'jszip' || + base === 'file-saver' + ) { + return { shortCircuit: true, url: emptyStub } + } + return nextResolve(specifier, context) +} +` + +register('data:text/javascript,' + encodeURIComponent(hookSource), pathToFileURL('./')) + +const { buildQcWorkOrderZipEntries } = await import('./qc-work-order-attachment-export') + +type WorkOrderLike = { + id: string + workOrderNo: string + workOrderType?: string + labStorageCode?: string + sampleCode?: string + attachments?: string +} + +type BatchLike = { + id: string + batchNo: string + batchName: string + assigneeUserIds: string[] + tasks?: Array<{ + id: string + taskNo?: string + itemName?: string + taskType?: string + taskStatus?: string + attachmentFiles?: Array<{ fileUrl: string; fileName?: string }> + }> +} + +function wo(partial: WorkOrderLike): WorkOrderLike { + return { workOrderType: 'inbound', ...partial } +} + +describe('buildQcWorkOrderZipEntries — work order attachments', () => { + it('includes work-order-level attachments under 工单附件/', () => { + const workOrders = [ + wo({ + id: 'wo-1', + workOrderNo: 'QWO-001', + labStorageCode: 'LAB01', + attachments: 'files/a.pdf,files/b.png' + }) + ] + const batchMap: Record = { 'wo-1': [] } + + const entries = buildQcWorkOrderZipEntries(workOrders as any, batchMap as any) + + assert.equal(entries.length, 2) + assert.ok(entries.every((e) => e.zipPath.includes('/工单附件/'))) + assert.ok(entries.some((e) => e.fileUrl === 'files/a.pdf' && e.zipPath.endsWith('/a.pdf'))) + assert.ok(entries.some((e) => e.fileUrl === 'files/b.png' && e.zipPath.endsWith('/b.png'))) + }) + + it('exports when only work-order attachments exist (no task files)', () => { + const workOrders = [ + wo({ + id: 'wo-2', + workOrderNo: 'QWO-002', + labStorageCode: 'LAB02', + attachments: 'minio/only-wo.docx' + }) + ] + const entries = buildQcWorkOrderZipEntries(workOrders as any, { 'wo-2': [] }) + assert.equal(entries.length, 1) + assert.match(entries[0].zipPath, /\/工单附件\/only-wo\.docx$/) + }) + + it('keeps task attachments and adds work-order attachments together', () => { + const workOrders = [ + wo({ + id: 'wo-3', + workOrderNo: 'QWO-003', + labStorageCode: 'LAB03', + attachments: 'wo/report.pdf' + }) + ] + const batchMap: Record = { + 'wo-3': [ + { + id: 'b1', + batchNo: 'B1', + batchName: '批次A', + assigneeUserIds: [], + tasks: [ + { + id: 't1', + taskNo: 'T1', + itemName: '活率', + taskType: 'sysjc', + taskStatus: 'ywc', + attachmentFiles: [{ fileUrl: 'task/result.pdf', fileName: 'result.pdf' }] + } + ] + } + ] + } + + const entries = buildQcWorkOrderZipEntries(workOrders as any, batchMap as any) + assert.equal(entries.length, 2) + assert.ok(entries.some((e) => e.zipPath.includes('/工单附件/') && e.fileUrl === 'wo/report.pdf')) + assert.ok( + entries.some( + (e) => + e.fileUrl === 'task/result.pdf' && + e.zipPath.includes('批次A') && + !e.zipPath.includes('/工单附件/') + ) + ) + }) + + it('returns empty when neither work-order nor task attachments exist', () => { + const workOrders = [wo({ id: 'wo-4', workOrderNo: 'QWO-004', attachments: '' })] + const entries = buildQcWorkOrderZipEntries(workOrders as any, { 'wo-4': [] }) + assert.equal(entries.length, 0) + }) +}) diff --git a/web-admin/src/utils/qc-work-order-attachment-export.ts b/web-admin/src/utils/qc-work-order-attachment-export.ts index d8664267..01d83132 100644 --- a/web-admin/src/utils/qc-work-order-attachment-export.ts +++ b/web-admin/src/utils/qc-work-order-attachment-export.ts @@ -92,6 +92,20 @@ function taskFolderName( return ensureUniqueZipEntryName(base, used) } +function parseCommaSeparatedAttachmentFiles(urls: string) { + return String(urls || '') + .split(',') + .map((u, i) => { + const fileUrl = u.trim() + if (!fileUrl) return null + const fileName = fileUrl.includes('/') + ? fileUrl.split('/').pop() || `附件${i + 1}` + : fileUrl + return { fileUrl, fileName } + }) + .filter(Boolean) as Array<{ fileUrl: string; fileName: string }> +} + export function buildQcWorkOrderZipEntries( workOrders: QcWorkOrder[], batchMap: Record @@ -101,6 +115,22 @@ export function buildQcWorkOrderZipEntries( for (const wo of workOrders) { const woFolder = workOrderFolderName(wo, usedWorkOrderFolders) + const workOrderFiles = parseCommaSeparatedAttachmentFiles(wo.attachments || '') + if (workOrderFiles.length) { + const usedWorkOrderFileNames = new Set() + const contextHint = [wo.workOrderNo, '工单附件'].filter(Boolean).join(' · ') + workOrderFiles.forEach((file, fileIndex) => { + const fileName = resolveAttachmentFileName(file, fileIndex) + const uniqueName = ensureUniqueZipEntryName(fileName, usedWorkOrderFileNames) + entries.push({ + zipPath: `${woFolder}/工单附件/${uniqueName}`, + fileUrl: file.fileUrl, + fileName, + contextHint + }) + }) + } + const batches = batchMap[wo.id] || [] const usedBatchFolders = new Set()