Files
syops/frontend/src/composables/useReportLayoutStorage.ts
T

45 lines
1.2 KiB
TypeScript

/**
* useReportLayoutStorage.ts
* 用途:为报表页面提供统一的布局偏好持久化能力(按用户 + 表名隔离)
*/
const getCurrentAuthUser = () => {
let stored = localStorage.getItem('hxops_user');
if (!stored) stored = sessionStorage.getItem('hxops_user');
if (!stored) return null;
try {
const data = JSON.parse(stored);
return data.user || data;
} catch {
return null;
}
};
const getCurrentIdentity = () => {
const user = getCurrentAuthUser();
return user?.id ? `id_${user.id}` : `u_${user?.username || 'guest'}`;
};
const getReportLayoutStorageKey = (table: string) => {
return `hxops_report_layout_v2_${getCurrentIdentity()}_${table}`;
};
export const loadReportLayoutPref = <T = any>(table: string): T | null => {
try {
const raw = localStorage.getItem(getReportLayoutStorageKey(table));
if (!raw) return null;
return JSON.parse(raw) as T;
} catch {
return null;
}
};
export const saveReportLayoutPref = (table: string, payload: unknown) => {
try {
localStorage.setItem(getReportLayoutStorageKey(table), JSON.stringify(payload));
} catch {
// Ignore storage write failure to avoid blocking core report flow.
}
};