来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
118 lines
3.5 KiB
TypeScript
118 lines
3.5 KiB
TypeScript
import type { ExportFormat } from './documentExport';
|
|
import { getUiPreferenceApi, setUiPreferenceApi } from './workflowApi';
|
|
import { readOperatingBranchCode } from './operatingBranch';
|
|
import { UI_PREF_KEYS } from './uiPreferenceKeys';
|
|
|
|
export type SavedExportPreferences = {
|
|
format?: ExportFormat;
|
|
expandItems?: boolean;
|
|
selectedColumnIds?: string[];
|
|
exportCustomerCode?: string;
|
|
printTitle?: string;
|
|
};
|
|
|
|
const STORAGE_KEY = 'canteen.export.preferences.v1';
|
|
|
|
type ExportPreferenceStore = Record<string, Record<string, SavedExportPreferences>>;
|
|
|
|
function readStore(): ExportPreferenceStore {
|
|
try {
|
|
const raw = localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) return {};
|
|
const parsed = JSON.parse(raw) as ExportPreferenceStore;
|
|
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function writeStore(store: ExportPreferenceStore): void {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(store));
|
|
} catch {
|
|
// ignore quota / private mode
|
|
}
|
|
}
|
|
|
|
function asUserExportMap(raw: unknown): Record<string, SavedExportPreferences> {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {};
|
|
return raw as Record<string, SavedExportPreferences>;
|
|
}
|
|
|
|
export function loadExportPreferences(
|
|
userId: string | null | undefined,
|
|
exportKey: string
|
|
): SavedExportPreferences | null {
|
|
if (!userId || !exportKey) return null;
|
|
const prefs = readStore()[userId]?.[exportKey];
|
|
if (!prefs || typeof prefs !== 'object') return null;
|
|
return prefs;
|
|
}
|
|
|
|
export function saveExportPreferences(
|
|
userId: string | null | undefined,
|
|
exportKey: string,
|
|
prefs: SavedExportPreferences
|
|
): void {
|
|
if (!userId || !exportKey) return;
|
|
const store = readStore();
|
|
if (!store[userId]) store[userId] = {};
|
|
store[userId][exportKey] = prefs;
|
|
writeStore(store);
|
|
// 只提交本 key;服务端 merge 保留其他导出偏好
|
|
void setUiPreferenceApi({
|
|
key: UI_PREF_KEYS.EXPORT,
|
|
value: { [exportKey]: prefs },
|
|
operatingBranchCode: readOperatingBranchCode(),
|
|
})
|
|
.then((data) => {
|
|
const remote = asUserExportMap(data.value);
|
|
if (!Object.keys(remote).length) return;
|
|
const next = readStore();
|
|
next[userId] = { ...(next[userId] || {}), ...remote };
|
|
writeStore(next);
|
|
})
|
|
.catch(() => {
|
|
/* keep local */
|
|
});
|
|
}
|
|
|
|
/** Pull server user export map into local cache for the given user. */
|
|
export async function hydrateExportPreferencesFromServer(
|
|
userId: string | null | undefined
|
|
): Promise<void> {
|
|
if (!userId) return;
|
|
try {
|
|
const data = await getUiPreferenceApi(UI_PREF_KEYS.EXPORT, readOperatingBranchCode());
|
|
const remote = asUserExportMap(data.value);
|
|
const store = readStore();
|
|
const local = store[userId] || {};
|
|
if (!Object.keys(remote).length) {
|
|
if (Object.keys(local).length) {
|
|
await setUiPreferenceApi({
|
|
key: UI_PREF_KEYS.EXPORT,
|
|
value: local,
|
|
operatingBranchCode: readOperatingBranchCode(),
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
// 服务端权威;本地仅补尚未上报的 key
|
|
store[userId] = { ...local, ...remote };
|
|
writeStore(store);
|
|
} catch {
|
|
/* keep local */
|
|
}
|
|
}
|
|
|
|
export function resolveSavedColumnIds(
|
|
savedIds: string[] | undefined,
|
|
availableIds: string[],
|
|
fallbackIds: string[]
|
|
): string[] {
|
|
if (!savedIds?.length) return fallbackIds;
|
|
const available = new Set(availableIds);
|
|
const valid = savedIds.filter((id) => available.has(id));
|
|
return valid.length ? valid : fallbackIds;
|
|
}
|