import { acceptHMRUpdate, defineStore } from 'pinia'; import { MOCK_SAAS_TENANTS } from '#/views/food-labeling/shared/mock-platform-data'; /** 当前登录身份(前端 Mock;联调后由后端 JWT / 用户信息注入) */ export type SaasActorType = 'platform' | 'tenant_admin' | 'tenant_user'; interface SaasContextState { actorType: SaasActorType; tenantId: string | null; tenantName: string | null; /** 当前租户已开通的菜单 key(公司管理员及其下属授权的上限) */ tenantMenuKeys: string[]; } /** * SAAS 权限上下文:平台管理员 vs 公司(租户)管理员 */ export const useSaasContextStore = defineStore('food-saas-context', { actions: { /** 模拟以某公司管理员身份进入系统 */ impersonateTenantAdmin(tenantId: string) { const tenant = MOCK_SAAS_TENANTS.find((t) => t.id === tenantId); if (!tenant) { return; } this.actorType = 'tenant_admin'; this.tenantId = tenant.id; this.tenantName = tenant.companyName; this.tenantMenuKeys = [...tenant.menuPermissionKeys]; }, resetToPlatformAdmin() { this.actorType = 'platform'; this.tenantId = null; this.tenantName = null; this.tenantMenuKeys = []; }, setTenantMenuKeys(keys: string[]) { this.tenantMenuKeys = keys; const tenant = MOCK_SAAS_TENANTS.find((t) => t.id === this.tenantId); if (tenant) { tenant.menuPermissionKeys = [...keys]; } }, }, getters: { isPlatformAdmin: (state) => state.actorType === 'platform', isTenantAdmin: (state) => state.actorType === 'tenant_admin', /** 角色/用户配置菜单权限时的可选上限 */ allowedMenuKeys(state): string[] { if (state.actorType === 'platform') { return []; } return state.tenantMenuKeys; }, }, state: (): SaasContextState => ({ actorType: 'platform', tenantId: null, tenantName: null, tenantMenuKeys: [], }), }); const hot = import.meta.hot; if (hot) { hot.accept(acceptHMRUpdate(useSaasContextStore, hot)); }