39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
/**
|
|
* 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();
|
|
}
|
|
|
|
/**
|
|
* 将 intake 编号列表转为建批重试指纹(排序去重后拼接)。
|
|
*/
|
|
export function computeNoticeIntakeNosFingerprint(intakeNos: string[]): string {
|
|
return [...new Set(intakeNos.map((item) => String(item || '').trim().toUpperCase()).filter(Boolean))].sort().join('|');
|
|
}
|