/** * 租户信息 store */ import { defineStore } from 'pinia'; import { formatMenus, toTreeData, formatTreeData } from 'ele-admin-pro'; import type { MenuItem } from 'ele-admin-pro'; import { Tenant } from "@/api/tennat/model"; import {useUserStore} from "@/store/modules/user"; import {getSiteInfo} from "@/api/cms/website"; import {ArrangeCategory} from "@/api/cms/category/model"; import {Website} from "@/api/cms/website/model"; export interface UserState { config: any | null; menus: MenuItem[] | null | undefined; siteInfo: Website | null; categoryList: ArrangeCategory[] | null | undefined; } export const useTenantStore = defineStore({ id: 'tenant', state: (): UserState => ({ // 配置信息 config: null, // 当前登录用户的菜单 menus: null, // 企业信息 siteInfo: null, // 文章分类 categoryList: null }), getters: {}, actions: { /** * 请求用户信息、权限、角色、菜单 */ async fetchTenantInfo() { const siteInfo = await getSiteInfo().catch(() => void 0); if (!siteInfo) { return {}; } const { categoryList,config } = siteInfo; this.siteInfo = siteInfo; localStorage.setItem('tenantId', String(siteInfo.tenantId)); localStorage.setItem('tenantName', String(siteInfo.websiteName)); // 缓存配置 if(config){ localStorage.setItem('LICENSE_CODE',config.LICENSE_CODE) localStorage.setItem('MAP_KEY',config.MAP_KEY) } // 用户菜单, 过滤掉按钮类型并转为 children 形式 const { menus, homePath } = formatMenus( toTreeData({ data: categoryList ?.map((d) => { // 新闻列表 if(d.type == 0){ d.path = `/article/${d.categoryId}` } // 链接 if(d.type == 2){ d.path = `${d.path}` } return { ...d, name: d.title, path: d.path, component: d.component }; }), idField: 'categoryId', parentIdField: 'parentId' }) ); this.menus = menus; console.log(menus); const userStore = useUserStore(); // 更新网站菜单数据 userStore.setMenu(menus); return { menus, homePath }; }, /** * 更新租户信息 */ setInfo(value: Tenant) { if (value.config) { this.config = value.config; } }, /** * 更新菜单的 badge */ setMenuBadge(path: string, value?: number | string, color?: string) { this.menus = formatTreeData(this.menus, (m) => { if (path === m.path) { return { ...m, meta: { ...m.meta, badge: value, badgeColor: color } }; } return m; }); } } });