来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
131 lines
5.5 KiB
TypeScript
131 lines
5.5 KiB
TypeScript
/**
|
||
* 「客户 → 上次用的配送单版式」记忆(**本机浏览器** localStorage)。
|
||
*
|
||
* 需求(开发者):给 A 客户选了 C 模板并点了打印,下次给 A 打印配送单就默认 C;
|
||
* 多客户混合打印时取**第一个客户**的记录;没有记录则回落到模板自带的默认(`isDefault`)。
|
||
*
|
||
* 只记「版式」,不记「商户版/账户版」类型;记录时机 = 点「打印配送单」。
|
||
* 与纸张/热敏打印机设置一样放在本机:换浏览器或换电脑就不带过去了。
|
||
*/
|
||
const STORAGE_KEY = 'delivery_note_customer_template_v1';
|
||
|
||
/** 上次打印时用过的「打印选项」子集(商品列 / 页眉信息 / 版式等,见 printTemplates 的定义) */
|
||
export interface DeliveryTemplatePreferenceOptions {
|
||
selectedColumnIds?: string[];
|
||
selectedMetaFields?: string[];
|
||
/** DeliveryNoteLayoutOptions 的浅拷贝(版式开关、合计口径、单据样式…) */
|
||
layout?: Record<string, unknown>;
|
||
}
|
||
|
||
export interface DeliveryTemplatePreference {
|
||
/** 旧版模板 id(PrintTemplateConfig.id) */
|
||
templateId?: string;
|
||
/** AI HTML 模板 id(DeliveryHtmlTemplate.id) */
|
||
htmlTemplateId?: string;
|
||
/** 上次这套勾选(商品列/页眉信息/版式)—— 弹窗里的勾选本身不回写模板,所以按客户记在本机 */
|
||
options?: DeliveryTemplatePreferenceOptions;
|
||
}
|
||
|
||
function normalizeStringList(value: unknown): string[] | undefined {
|
||
if (!Array.isArray(value)) return undefined;
|
||
// 只接受字符串(localStorage 可能被手改成任意类型)
|
||
const list = value
|
||
.filter((item): item is string => typeof item === 'string')
|
||
.map((item) => item.trim())
|
||
.filter(Boolean);
|
||
return list.length ? [...new Set(list)] : undefined;
|
||
}
|
||
|
||
function normalizeOptions(value: unknown): DeliveryTemplatePreferenceOptions | undefined {
|
||
if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined;
|
||
const row = value as Record<string, unknown>;
|
||
const selectedColumnIds = normalizeStringList(row.selectedColumnIds);
|
||
const selectedMetaFields = normalizeStringList(row.selectedMetaFields);
|
||
const layout =
|
||
row.layout && typeof row.layout === 'object' && !Array.isArray(row.layout)
|
||
? (row.layout as Record<string, unknown>)
|
||
: undefined;
|
||
if (!selectedColumnIds && !selectedMetaFields && !layout) return undefined;
|
||
return { selectedColumnIds, selectedMetaFields, layout };
|
||
}
|
||
|
||
export type DeliveryTemplatePreferenceMap = Record<string, DeliveryTemplatePreference>;
|
||
|
||
/** 把 localStorage 里读到的东西收成合法结构(坏数据一律丢弃,不抛错) */
|
||
export function normalizeTemplatePreferenceMap(value: unknown): DeliveryTemplatePreferenceMap {
|
||
if (!value || typeof value !== 'object' || Array.isArray(value)) return {};
|
||
const result: DeliveryTemplatePreferenceMap = {};
|
||
for (const [rawKey, rawValue] of Object.entries(value as Record<string, unknown>)) {
|
||
const customerCode = String(rawKey || '').trim();
|
||
if (!customerCode || !rawValue || typeof rawValue !== 'object' || Array.isArray(rawValue)) continue;
|
||
const row = rawValue as Record<string, unknown>;
|
||
const templateId = typeof row.templateId === 'string' && row.templateId.trim() ? row.templateId.trim() : undefined;
|
||
const htmlTemplateId =
|
||
typeof row.htmlTemplateId === 'string' && row.htmlTemplateId.trim() ? row.htmlTemplateId.trim() : undefined;
|
||
const options = normalizeOptions(row.options);
|
||
if (!templateId && !htmlTemplateId && !options) continue;
|
||
result[customerCode] = { templateId, htmlTemplateId, options };
|
||
}
|
||
return result;
|
||
}
|
||
|
||
export function readPreferenceFromMap(
|
||
map: DeliveryTemplatePreferenceMap | null | undefined,
|
||
customerCode: string | null | undefined
|
||
): DeliveryTemplatePreference | null {
|
||
const key = String(customerCode || '').trim();
|
||
if (!key) return null;
|
||
const found = map?.[key];
|
||
return found ? { ...found } : null;
|
||
}
|
||
|
||
/** 写入(返回新 map,不改原对象,也不会覆盖其它客户的记录) */
|
||
export function writePreferenceToMap(
|
||
map: DeliveryTemplatePreferenceMap | null | undefined,
|
||
customerCode: string | null | undefined,
|
||
preference: DeliveryTemplatePreference
|
||
): DeliveryTemplatePreferenceMap {
|
||
const key = String(customerCode || '').trim();
|
||
const next: DeliveryTemplatePreferenceMap = { ...(map || {}) };
|
||
if (!key) return next;
|
||
const templateId = preference.templateId?.trim() || undefined;
|
||
const htmlTemplateId = preference.htmlTemplateId?.trim() || undefined;
|
||
const options = normalizeOptions(preference.options);
|
||
if (!templateId && !htmlTemplateId && !options) {
|
||
delete next[key];
|
||
return next;
|
||
}
|
||
next[key] = { templateId, htmlTemplateId, options };
|
||
return next;
|
||
}
|
||
|
||
export function loadTemplatePreferenceMap(): DeliveryTemplatePreferenceMap {
|
||
try {
|
||
const raw = localStorage.getItem(STORAGE_KEY);
|
||
if (!raw) return {};
|
||
return normalizeTemplatePreferenceMap(JSON.parse(raw));
|
||
} catch {
|
||
return {};
|
||
}
|
||
}
|
||
|
||
export function saveTemplatePreference(
|
||
customerCode: string | null | undefined,
|
||
preference: DeliveryTemplatePreference
|
||
): void {
|
||
const key = String(customerCode || '').trim();
|
||
if (!key) return;
|
||
try {
|
||
const next = writePreferenceToMap(loadTemplatePreferenceMap(), key, preference);
|
||
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
||
} catch {
|
||
/* 隐私模式等写不进去时静默忽略,不影响打印 */
|
||
}
|
||
}
|
||
|
||
export function readTemplatePreference(
|
||
customerCode: string | null | undefined
|
||
): DeliveryTemplatePreference | null {
|
||
return readPreferenceFromMap(loadTemplatePreferenceMap(), customerCode);
|
||
}
|