import { defineStore } from "pinia"; import { KeepAliveState } from "@/stores/interface"; import { cloneDeep } from "lodash-es"; import { useTabsStore } from "@/stores/modules/tabs"; export const useKeepAliveStore = defineStore({ id: "geeker-keepAlive", state: (): KeepAliveState => ({ keepAliveName: [], keepAliveList: [] }), getters: { getKeepAliveList(): any[] { return this.keepAliveList; } }, actions: { setKeepAliveList(keepAliveList: any) { this.keepAliveList = keepAliveList; console.log("getKeepAliveName routeItem setKeepAliveList :>> ", JSON.stringify(this.keepAliveList)); console.log("getKeepAliveName routeItem setKeepAliveList :>> ", this.keepAliveList); }, // Add KeepAliveName async addKeepAliveName(route: any) { !this.keepAliveName.includes(route.name) && this.keepAliveName.push(route.name); let routeItem = this.keepAliveList.find((item: any) => item.fullPath == route.fullPath); if (!routeItem) { routeItem = { fullPath: route.fullPath, times: 0 }; this.keepAliveList.push(routeItem); } }, // Remove KeepAliveName removeKeepAliveName(route: any) { this.keepAliveName = this.keepAliveName.filter(item => item !== route.name); let index = this.keepAliveList.findIndex(item => item.fullPath === route.path); if (index > -1) { // this.keepAliveList.splice(index, 1); this.keepAliveList[index].times = this.keepAliveList[index].times + 1; } console.log("getKeepAliveName routeItem removeKeepAliveName :>> ", JSON.stringify(this.keepAliveList)); console.log("getKeepAliveName routeItem removeKeepAliveName :>> ", this.keepAliveList); console.log("getKeepAliveName routeItem keepAliveName :>> ", this.keepAliveName); }, // Set KeepAliveName async setKeepAliveName(keepAliveName: string[] = []) { this.keepAliveName = keepAliveName; }, async updateKeepAliveName(route: any) { this.addKeepAliveName(route); console.log("updateKeepAliveName route :>> ", route, this.keepAliveList); this.keepAliveList.forEach((item: any) => { console.log("updateKeepAliveName item :>> ", item.fullPath, route.fullPath, item.fullPath == route.fullPath); if (item.fullPath == route.fullPath) { item.times++; } }); }, getKeepAliveName(name: string) { let routeItem = this.keepAliveList.find((item: any) => item.fullPath == name); if (!routeItem) { routeItem = { fullPath: name, times: 0 }; this.keepAliveList.push(routeItem); } return routeItem; }, computedKeepAliveName(to, from) { const tabStore = useTabsStore(); const tabsParams = { icon: to.meta.icon as string, title: to.meta.title as string, path: to.fullPath, name: to.name as string, close: !to.meta.isAffix }; console.log("tabStore :>> ", tabStore, to); console.log("tabsParams router:>> ", tabsParams); let isReplace = false; // console.log( // 'tabsParams.path.indexOf("/edit?") > -1 , :>> ', // tabsParams.path.indexOf("/edit?") > -1, // tabsParams.path.indexOf("/edit/") > -1 // ); // console.log( // 'tabsParams.path.indexOf("/detail?") > -1 , :>> ', // tabsParams.path.indexOf("/detail?") > -1, // tabsParams.path.indexOf("/detail/") > -1 // ); console.log( 'to.fullPath.split("?")[0] != from.fullPath.split("?")[0] :>> ', to.fullPath.split("?")[0], from.fullPath.split("?")[0], to.fullPath.split("?")[0] != from.fullPath.split("?")[0] ); // if (from.meta?.activeMenu == to.meta?.activeMenu && to.fullPath.split("?")[0] != from.fullPath.split("?")[0]) { if (tabsParams.path.indexOf("/edit?") > -1 || tabsParams.path.indexOf("/edit/") > -1) { let key = "/edit?"; let name = ""; let _path = tabsParams.path.replace(key, "/detail?"); // let _path = from.fullPath; // 使用路由参数的地址,忽略查询参数后,查找相同地址 let _path2 = tabsParams.path.replace("/edit/", "/detail/"); let hasPath1 = to.fullPath.indexOf("/edit?") > -1; let hasPath2 = to.fullPath.indexOf("/edit/") > -1; let tabsMenuList = tabStore.tabsMenuList; console.log("tabsMenuList :>> ", JSON.stringify(tabsMenuList)); for (let i = 0; i < tabsMenuList.length; i++) { if ( (hasPath1 && tabsMenuList[i].path == _path) || (hasPath2 && tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0]) ) { console.log( "computedKeepAliveName edit ====>", i, tabsMenuList[i].path, _path, tabsMenuList[i].path == _path, hasPath2, tabsMenuList[i].path.split("?")[0], _path2.split("?")[0], tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0], tabsMenuList[i] ); name = tabsMenuList[i].name; tabsMenuList[i] = tabsParams; isReplace = true; break; } } console.log("keilll name edit:>> ", name); tabStore.setTabs(tabsMenuList); name && this.updateKeepAliveName(to); // to.meta.isKeepAlive && this.updateKeepAliveName(route); } else if (tabsParams.path.indexOf("/detail?") > -1 || tabsParams.path.indexOf("/detail/") > -1) { let key = "/detail?"; let name = ""; let _path = tabsParams.path.replace(key, "/edit?"); // let _path = from.fullPath; // 使用路由参数的地址,忽略查询参数后,查找相同地址 let _path2 = tabsParams.path.replace("/detail/", "/edit/"); let hasPath1 = to.fullPath.indexOf("/detail?") > -1; let hasPath2 = to.fullPath.indexOf("/detail/") > -1; let tabsMenuList = tabStore.tabsMenuList; console.log("tabsMenuList :>> ", JSON.stringify(tabsMenuList)); for (let i = 0; i < tabsMenuList.length; i++) { if ( (hasPath1 && (tabsMenuList[i].path == _path || (tabsMenuList[i].path == from.fullPath && from.fullPath.indexOf("/copy?") > -1))) || (hasPath2 && tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0]) ) { console.log( "computedKeepAliveName detail ====>", tabsMenuList[i].path, _path, tabsMenuList[i].path == _path, hasPath2, tabsMenuList[i].path.split("?")[0], _path2.split("?")[0], tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0], tabsMenuList[i] ); name = tabsMenuList[i].name; tabsMenuList[i] = tabsParams; isReplace = true; break; } } tabStore.setTabs(tabsMenuList); console.log("keilll name detail:>> ", name); name && this.updateKeepAliveName(to); // to.meta.isKeepAlive && this.addKeepAliveName(to); console.log("add this.keepAliveName :>> ", this.keepAliveName); } // } !isReplace && tabStore.addTabs(tabsParams); !isReplace && to.meta.isKeepAlive && this.addKeepAliveName(to); } } });