import type { TenantContext } from '~/types' /** * 获取当前租户上下文 * 服务端从 event.context.tenant 获取(由 Server Middleware 设置) * 客户端从 SSR 注入的 payload 获取 */ export function useTenant() { const ctx = useState('tenant-context', () => ({ tenantId: useRuntimeConfig().public.tenantId as string, appId: useRuntimeConfig().public.appId as string, templateId: useRuntimeConfig().public.templateId as string, source: 'env' })) // SSR 时从服务端请求头获取 if (import.meta.server) { const event = useRequestEvent() if (event?.context?.tenant) { ctx.value = event.context.tenant as TenantContext } } return ctx }