feat: 优化web

This commit is contained in:
2026-04-23 18:58:13 +08:00
commit 544a2f3428
160 changed files with 27327 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
'use client';
import { create } from 'zustand';
import { registerTenantHeaderBridge } from '@/lib/api/client';
const KEY = 'smart_tenant_id';
type TenantState = {
/** 显式选择的租户;null 表示不额外传 X-Tenant-ID,由 Bearer 默认租户生效 */
tenantId: string | null;
setTenantId: (id: string | null) => void;
/** 从当前用户资料同步(登录后首次) */
hydrateFromUserTenant: (tenantId: string | undefined | null) => void;
};
function load(): string | null {
if (typeof window === 'undefined') {
return null;
}
try {
const raw = sessionStorage.getItem(KEY);
if (raw === '' || raw === 'null') {
return null;
}
return raw;
} catch {
return null;
}
}
export const useTenantStore = create<TenantState>((set, get) => ({
tenantId: typeof window !== 'undefined' ? load() : null,
setTenantId: (id) => {
set({ tenantId: id });
if (typeof window !== 'undefined') {
if (id) {
sessionStorage.setItem(KEY, id);
} else {
sessionStorage.removeItem(KEY);
}
}
},
hydrateFromUserTenant: (tid) => {
if (!tid) {
return;
}
const cur = get().tenantId;
if (cur == null || cur === '') {
set({ tenantId: tid });
if (typeof window !== 'undefined') {
sessionStorage.setItem(KEY, tid);
}
}
},
}));
registerTenantHeaderBridge(() => useTenantStore.getState().tenantId);