Files
SupplierSystem/src/utils/zipStore.batchReports.test.ts
T
Ubuntu 6d79848374
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(订单详情): 商品行多选 + 一键全选 → 打包下载所选商品的批次报告(ZIP)
需求原话:「订单详情这块 加一个多选按钮 … 也可以一键全选 选择后点击下载 生成一个文件夹
里面就是这些商品的批次报告」。

做法(纯前端,库/后端/状态链/权限码零改动):
- 明细表首列新增勾选框 + 表头全选(半选态),只有**确有批次报告**的行可勾;
- 选中后工具条出现「下载批次报告」:逐行取批次报告文件 → ZIP 下载,包内命名
  `<行序号>_<商品名>_<批次号>_<第几张>.<ext>`,ZIP 名 `<订单号>_批次报告.zip`;
- **浏览器不允许网页直接建文件夹** ⇒ 用 ZIP 承载(解压后即一个文件夹),复用既有
  `src/utils/zipStore.ts`(财务发票批量下载同一套),**未引入任何新依赖**;
- 文件走同源签名地址(`toSameOriginStorageUrl`),不直连 COS(跨域会失败);
- 换单清空勾选(防上一单行 id 串入);逐个 await 之间让出主线程。

数据侧:把原先内联在展示列里的批次报告 SKU 复核提取为 `pickBatchReportFilesForLine`,
**展示列与打包共用一处口径**(防两处漂移),并把这一份挂到订单行 `batch_report_files`。

证据:
- `npx vitest run src/utils/zipStore.batchReports.test.ts` —— 真实 `unzip -t` 报
  `No errors detected`,解压后文件名与字节逐一相等(中文名 + 二进制内容);
- `...OrderDetailDeliveryNotePanel.batchReportZip.contract.test.ts` 7 通过;
  `orderBatchReportDisplay.test.ts` 5 通过(提取后改钉共用函数,SKU 复核意图不变);
  面板/工具既有 19 通过;`npx tsc --noEmit` exit 0;
- 设计门禁与基线一致:8 通过 / 1 红(check-ai-cost 按设计恒红)/ 1 未执行(本机无 psql)。

新增界面文案 3 条(待批准):orders.detail.selectAllBatchReports=全选 /
downloadBatchReports=下载批次报告 / downloadBatchReportsFail=批次报告下载失败(三语同步)。
2026-10-16 14:28:33 +08:00

53 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { execFileSync } from 'node:child_process';
import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { buildZipStore, sanitizeZipEntryName } from './zipStore';
/**
* 「批量下载批次报告」真正依赖的是 **ZIP 能被解压工具认**(现有 `zipStore.test.ts` 只验了魔术字节)。
*
* 这里用真实 `unzip` 做端到端校验:`unzip -t` 必须报无错,解压出来的**文件名与内容**必须逐字节一致
* —— 中文名(UTF-8 标志位)、CRC32、大小三个字段任一处写错,都会在这里直接红。
*/
describe('批次报告 ZIP · 真实解压校验', () => {
it('buildZipStore 产出的包能被 unzip 校验并原样解出(含中文名与二进制内容)', async () => {
const seq = (n: number) => n;
const entries = [
{
name: `${seq(1)}_${sanitizeZipEntryName('苹果')}_${sanitizeZipEntryName('20261014-01')}_1.jpg`,
// 模拟 JPEG 二进制(含 0x00/0xFF,CRC 写错会立刻被 unzip -t 抓到)
data: new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46]),
},
{
name: `${seq(2)}_${sanitizeZipEntryName('精瘦肉')}_${sanitizeZipEntryName('20261015-02')}_1.pdf`,
data: new TextEncoder().encode('%PDF-1.4 批次报告内容\n'),
},
];
const dir = mkdtempSync(join(tmpdir(), 'batch-reports-zip-'));
try {
const zipPath = join(dir, 'br002-order_批次报告.zip');
const blob = buildZipStore(entries);
writeFileSync(zipPath, Buffer.from(await blob.arrayBuffer()));
// ① unzip -t:校验每个条目的 CRC 与结构
const testOut = execFileSync('unzip', ['-t', zipPath], { encoding: 'utf8' });
expect(testOut).toContain('No errors detected');
// ② 真实解压,逐字节比对内容与文件名
const outDir = join(dir, 'out');
execFileSync('unzip', ['-qq', '-o', zipPath, '-d', outDir]);
const extracted = readdirSync(outDir).sort();
expect(extracted).toEqual(entries.map((e) => e.name).sort());
for (const entry of entries) {
const written = readFileSync(join(outDir, entry.name));
expect(new Uint8Array(written)).toEqual(entry.data);
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});