import { defineStore } from 'pinia' import { PLATFORM_TABS } from '@/config/platform-scope' import type { PlatformTabId } from '@/config/platform-scope' const STORAGE_KEY = 'admin-platform-tab' const DEFAULT_TAB: PlatformTabId = 'amazon' function isKnownTab(id: unknown): id is PlatformTabId { return PLATFORM_TABS.some((tab) => tab.id === id) } /** 读取持久化偏好;无存储/值非法/存储不可用一律回落亚马逊(隐私模式不阻断交互)。 */ function readStoredTab(): PlatformTabId { try { const raw = typeof localStorage === 'undefined' ? null : localStorage.getItem(STORAGE_KEY) return isKnownTab(raw) ? raw : DEFAULT_TAB } catch { return DEFAULT_TAB } } function writeStoredTab(id: PlatformTabId): void { try { if (typeof localStorage !== 'undefined') localStorage.setItem(STORAGE_KEY, id) } catch { // localStorage 不可用(隐私模式等)时忽略 } } /** 侧边栏平台 tab(亚马逊/京东)状态:纯显示分组不参与鉴权(2026-11-16 用户定案)。 * 菜单可见性仍由 session.menuTree(后端按权限裁剪)决定,这里只记「当前看哪一侧」。 */ export const usePlatformTabStore = defineStore('platform-tab', { state: () => ({ active: readStoredTab(), }), actions: { setActive(id: PlatformTabId) { this.active = id writeStoredTab(id) }, /** 平台锁定(2026-11-18 用户口径「非站点超管只看自己平台侧」):允许集收敛时把 active * 归位——localStorage 残留的另一侧(超管在同一浏览器切过 tab)自动回落本人平台; * 允许集为空(会话未就位)不动。 */ syncToAllowed(allowed: readonly PlatformTabId[]) { if (allowed.length > 0 && !allowed.includes(this.active)) { this.setActive(allowed[0]) } }, }, })