import { defineStore } from 'pinia'; import { store } from '@/store'; import { IParam } from '@/core'; interface AppState { /** * 页面描述集合 * * @type {any[]} * @memberof AppState */ pageMetas: any[]; /** * 历史路径集合 * * @type {string[]} * @memberof AppState */ historyPathList: string[]; /** * 面板屑数据 * * @type {any[]} * @memberof AppState */ breadcrumbs: any[]; /** * 默认视图标识 * * @type {string} * @memberof AppState */ defaultViewCodeName: string; /** * 应用数据 * * @type {*} * @memberof AppState */ appData: any; /** * 本地应用数据 * * @type {*} * @memberof AppState */ localData: any; /** * 组织数据集合 * * @type {IParam} * @memberof AppState */ orgDataMap: Map; /** * 部门数据集合 * * @type {IParam} * @memberof AppState */ depDataMap: Map; /** * 部门人员数据集合 * * @type {IParam} * @memberof AppState */ deptPersonDataMap: Map; } const useAppStore = defineStore({ id: 'app', state: (): AppState => ({ pageMetas: [], historyPathList: [], breadcrumbs: [], defaultViewCodeName: '', appData: {}, localData: {}, orgDataMap: new Map(), depDataMap: new Map(), deptPersonDataMap: new Map(), }), getters: { getPageMetas(): any[] { return this.pageMetas; }, getPageMeta(): any { return (fullPath: string) => { if (fullPath) { const index = this.pageMetas.findIndex((pageMeta: IParam) => Object.is(pageMeta.fullPath, fullPath) ); if (index >= 0) { return this.pageMetas[index]; } } }; }, getAppData(): any { if (!this.appData) { this.appData = {}; } const result: any = JSON.parse(JSON.stringify(this.appData)); const copyContext: any = result.context ? result.context : {}; if (this.localData && Object.keys(this.localData).length > 0) { Object.assign(copyContext, this.localData); } else if (localStorage.getItem('localData')) { try { Object.assign( copyContext, JSON.parse(localStorage.getItem('localData') as string) ); } catch (error) { console.warn(error); } } Object.assign(result, { context: copyContext }); return result; }, /** * 获取组织数据 * * @return {*} */ getOrgData() { return (srfkey: string): IParam[] | undefined => { return this.orgDataMap.get(srfkey); }; }, /** * 获取部门数据 * * @return {*} */ getDepData() { return (srfkey: string): IParam[] | undefined => { return this.depDataMap.get(srfkey); }; }, /** * 获取部门人员数据 * * @return {*} */ getDeptPersonData() { return (srfkey: string): IParam[] | undefined => { return this.deptPersonDataMap.get(srfkey); }; }, getBreadcrumbs(): any[] { return this.breadcrumbs; } }, actions: { addAppData(appData: any) { this.appData = appData; }, addOrgData(srfkey: string, orgData: IParam[]) { this.orgDataMap.set(srfkey, orgData); }, addDepData(srfkey: string, depData: IParam[]) { this.depDataMap.set(srfkey, depData); }, addDeptPersonData(srfkey: string, deptPersonData: IParam[]) { this.deptPersonDataMap.set(srfkey, deptPersonData); }, clearAppData() { this.appData = {}; this.breadcrumbs = []; this.defaultViewCodeName = ''; }, addPage(page: any) { if (!page) { return; } const inputPageMeta = page.meta; if (inputPageMeta && Object.keys(inputPageMeta).length > 0) { if (inputPageMeta.startPage) { const appName = App.getAppConfig().appName; window.sessionStorage.setItem(appName, page.fullPath); } else { const pageMeta: any = {}; Object.assign(pageMeta, inputPageMeta, { info: null, fullPath: page.fullPath, }); const index = this.pageMetas.findIndex((pageMeta: any) => Object.is(pageMeta.fullPath, page.fullPath) ); if (index < 0) { this.pageMetas.push(pageMeta); } else { const index2 = this.historyPathList.indexOf(page.fullPath); if (index2 >= 0) { this.historyPathList.splice(index2, 1); } } this.historyPathList.push(page.fullPath); // 面板屑数据 if (this.defaultViewCodeName) { const defaultView = this.breadcrumbs[0]; if (defaultView && defaultView.isRoute && Object.is(pageMeta.tag, this.defaultViewCodeName)) { this.breadcrumbs = [pageMeta]; } else { this.addBreadcrumbs(pageMeta); } } else { this.addBreadcrumbs(pageMeta) } } } }, addLocalData(localData: any) { Object.assign(this.localData, localData); localStorage.setItem('localData', JSON.stringify(this.localData)); }, setPage({ fullPath, caption, info, }: { fullPath: string; caption: string; info?: string; }) { if (fullPath) { const index = this.pageMetas.findIndex((pageMeta: IParam) => Object.is(pageMeta.fullPath, fullPath) ); if (index >= 0) { this.pageMetas[index].caption = caption; this.pageMetas[index].info = info; } } }, // 删除导航页面 deletePage(fullPath: string) { let delPage: any = null; if (fullPath) { const index = this.pageMetas.findIndex((pageMeta: IParam) => Object.is(pageMeta.fullPath, fullPath) ); if (index >= 0) { delPage = this.pageMetas[index]; this.pageMetas.splice(index, 1); } } const index = this.historyPathList.findIndex((path: any) => Object.is(path, delPage?.fullPath) ); if (index >= 0) { this.historyPathList.splice(index, 1); } }, closeAllPage() { if (this.pageMetas.length > 0) { this.pageMetas = []; this.historyPathList = []; } }, closeOtherPage() { if (this.historyPathList.length > 0) { const curPath = this.historyPathList[this.historyPathList.length - 1]; const index = this.pageMetas.findIndex((pageMeta: IParam) => Object.is(pageMeta.fullPath, curPath) ); if (index >= 0) { const pageMeta = this.pageMetas[index]; this.pageMetas.splice(0, this.pageMetas.length, pageMeta); this.historyPathList.splice( 0, this.historyPathList.length, pageMeta.fullPath ); } } }, setDefaultViewCodeName(codeName: string) { if (codeName) { this.defaultViewCodeName = codeName; } }, removeBreadcrumbsFirst() { if (this.defaultViewCodeName && this.breadcrumbs.length > 0) { this.breadcrumbs.splice(1, this.breadcrumbs.length); } else { this.breadcrumbs = []; } }, unshiftBreadcrumbs(view: IParam) { if (view) { this.breadcrumbs.unshift(view); } }, addBreadcrumbs(pageMeta: IParam) { const breadcrumbIndex = this.breadcrumbs.findIndex((breadcrumb: any) => Object.is(breadcrumb.tag, pageMeta.tag) ); if (breadcrumbIndex < 0) { this.breadcrumbs.push(pageMeta); } else { this.breadcrumbs.splice(breadcrumbIndex + 1, this.breadcrumbs.length); } } }, }); export function useAppStoreWithOut() { return useAppStore(store); }