import { fetchEnterpriseInfo } from '@/api/all/login'; import { useSidebarStore } from '@/stores/modules/side-bar'; interface EnterpriseInfo { id: number; name: string; update_name_quota: number; used_update_name_count: number; sub_account_quota: number; used_sub_account_count: number; permissions: string[]; } interface EnterpriseState { enterpriseInfo: EnterpriseInfo | null; } export const useEnterpriseStore = defineStore('enterprise', { state: (): EnterpriseState => ({ enterpriseInfo: (() => { const stored = localStorage.getItem('enterpriseInfo'); if (stored) { try { return JSON.parse(stored) as EnterpriseInfo; } catch { return null; } } return null; })(), }), actions: { setEnterpriseInfo(enterpriseInfo: EnterpriseInfo) { this.enterpriseInfo = enterpriseInfo; localStorage.setItem('enterpriseInfo', JSON.stringify(enterpriseInfo)); }, clearEnterpriseInfo() { this.enterpriseInfo = null; localStorage.removeItem('enterpriseInfo'); }, setEnterpriseName(name: string) { if (this.enterpriseInfo) { this.enterpriseInfo.name = name; } }, incUsedUpdateNameCount() { if (this.enterpriseInfo) { this.enterpriseInfo.used_update_name_count++; } }, incUsedSubAccountCount() { if (this.enterpriseInfo) { this.enterpriseInfo.used_sub_account_count++; } }, getEnterpriseInfo(): EnterpriseInfo | null { return this.enterpriseInfo; }, async updateEnterpriseInfo() { const sidebarStore = useSidebarStore(); const res = await fetchEnterpriseInfo(this.enterpriseInfo!.id); const { code, data } = res; if (code === 200) { this.setEnterpriseInfo(data); sidebarStore.getNavbarMenuList(); } }, }, });