feat(shipping-notice): 增加发货通知建单幂等性支持及内容哈希校验

This commit is contained in:
hugo committed 2026-07-14 00:28:52 +08:00
1 parent 0a9bd6415d
commit 143e054d86
39 files changed
+1750 -80

No files matched your search

@@ -0,0 +1,31 @@
/**
* features/chat/shipping/shippingNoticeBatchIdempotency.ts
* 用途:发货通知建批幂等键与文件集合指纹计算。
* 依赖:浏览器 Web Crypto API。
* 逻辑:useChatShippingUploadHandler 在重试时绑定「幂等键 + 文件指纹」,文件变化时生成新键。
*/
/**
* 将 File 列表转为与后端 notice_items 对齐的内容指纹(排序哈希后二次 SHA-256)。
*/
export async function computeNoticeFilesContentFingerprint(files: File[]): Promise<string> {
const hashes: string[] = [];
for (const file of files) {
if (!file) continue;
const buffer = await file.arrayBuffer();
const digest = await crypto.subtle.digest('SHA-256', buffer);
const hex = Array.from(new Uint8Array(digest))
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
hashes.push(hex.toLowerCase());
}
const uniqueHashes = [...new Set(hashes)].sort();
if (!uniqueHashes.length) return '';
const joined = uniqueHashes.join('|');
const encoded = new TextEncoder().encode(joined);
const fingerprintDigest = await crypto.subtle.digest('SHA-256', encoded);
return Array.from(new Uint8Array(fingerprintDigest))
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('')
.toLowerCase();
}