multipleTab.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. import type { RouteLocationNormalized, RouteLocationRaw, Router } from 'vue-router';
  2. import { toRaw, unref } from 'vue';
  3. import { defineStore } from 'pinia';
  4. import { store } from '/@/store';
  5. import { PAGE_NOT_FOUND_NAME_404 } from '/@/router/constant';
  6. import { useGo, useRedo } from '/@/hooks/web/usePage';
  7. import { Persistent } from '/@/utils/cache/persistent';
  8. import { PageEnum } from '/@/enums/pageEnum';
  9. import { PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE } from '/@/router/routes/basic';
  10. import { getRawRoute } from '/@/utils';
  11. import { MULTIPLE_TABS_KEY } from '/@/enums/cacheEnum';
  12. import projectSetting from '/@/settings/projectSetting';
  13. import { useUserStore } from '/@/store/modules/user';
  14. import type { LocationQueryRaw, RouteParamsRaw } from 'vue-router';
  15. import { getMenus } from '/@/router/menus';
  16. export interface MultipleTabState {
  17. cacheTabList: Set<string>;
  18. tabList: RouteLocationNormalized[];
  19. lastDragEndIndex: number;
  20. redirectPageParam: null | redirectPageParamType;
  21. }
  22. interface redirectPageParamType {
  23. redirect_type: string;
  24. name?: string;
  25. path?: string;
  26. query: LocationQueryRaw;
  27. params?: RouteParamsRaw;
  28. }
  29. function handleGotoPage(router: Router, path?) {
  30. const go = useGo(router);
  31. // update-begin--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  32. go(path || unref(router.currentRoute).path, true);
  33. // update-end--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  34. }
  35. const getToTarget = (tabItem: RouteLocationNormalized) => {
  36. const { params, path, query } = tabItem;
  37. return {
  38. params: params || {},
  39. path,
  40. query: query || {},
  41. };
  42. };
  43. /**
  44. * 2024-06-05
  45. * liaozhiyang
  46. * 关闭的tab中是否包含当前页面
  47. */
  48. const closeTabContainCurrentRoute = (router, pathList) => {
  49. const { currentRoute } = router;
  50. const getCurrentTab = () => {
  51. const route = unref(currentRoute);
  52. const tabStore = useMultipleTabStore();
  53. return tabStore.getTabList.find((item) => item.path === route.path)!;
  54. };
  55. const currentTab = getCurrentTab();
  56. if (currentTab) {
  57. return pathList.includes(currentTab.path);
  58. }
  59. return false;
  60. };
  61. /**
  62. * 2025-05-08
  63. * liaozhiyang
  64. * 【issues/8216】online生成的菜单sql 自动带上组件名称
  65. * */
  66. function getMatchingRoute(menus, path) {
  67. for (let i = 0, len = menus.length; i < len; i++) {
  68. const item = menus[i];
  69. if (item.path === path && !item.redirect && !item.paramPath) {
  70. return item;
  71. } else if (item.children?.length) {
  72. const result = getMatchingRoute(item.children, path);
  73. if (result) {
  74. return result;
  75. }
  76. }
  77. }
  78. return null;
  79. }
  80. const cacheTab = projectSetting.multiTabsSetting.cache;
  81. export const useMultipleTabStore = defineStore({
  82. id: 'app-multiple-tab',
  83. state: (): MultipleTabState => ({
  84. // Tabs that need to be cached
  85. cacheTabList: new Set(),
  86. // multiple tab list
  87. tabList: cacheTab ? Persistent.getLocal(MULTIPLE_TABS_KEY) || [] : [],
  88. // Index of the last moved tab
  89. lastDragEndIndex: 0,
  90. // 重定向时存储的路由参数
  91. redirectPageParam: null,
  92. }),
  93. getters: {
  94. getTabList(): RouteLocationNormalized[] {
  95. return this.tabList;
  96. },
  97. getCachedTabList(): string[] {
  98. return Array.from(this.cacheTabList);
  99. },
  100. getLastDragEndIndex(): number {
  101. return this.lastDragEndIndex;
  102. },
  103. },
  104. actions: {
  105. /**
  106. * Update the cache according to the currently opened tabs
  107. */
  108. async updateCacheTab() {
  109. const cacheMap: Set<string> = new Set();
  110. const allMenus = await getMenus();
  111. for (const tab of this.tabList) {
  112. const item = getRawRoute(tab);
  113. // Ignore the cache
  114. const needCache = !item.meta?.ignoreKeepAlive;
  115. if (!needCache) {
  116. continue;
  117. }
  118. // update-begin--author:liaozhiyang---date:20240308---for:【QQYUN-12348】online生成的菜单sql 自动带上组件名称
  119. if (
  120. [
  121. 'OnlineAutoList',
  122. 'DefaultOnlineList',
  123. 'CgformErpList',
  124. 'OnlCgformInnerTableList',
  125. 'OnlCgformTabList',
  126. 'OnlCgReportList',
  127. 'GraphreportAutoChart',
  128. 'AutoDesformDataList',
  129. ].includes(item.name as string) &&
  130. allMenus?.length
  131. ) {
  132. const route = getMatchingRoute(allMenus, item.path);
  133. if (route?.meta?.keepAlive) {
  134. // 如果keepAlive为true,则添加到缓存中
  135. } else {
  136. continue;
  137. }
  138. }
  139. // update-end--author:liaozhiyang---date:20240308---for:【QQYUN-12348】online生成的菜单sql 自动带上组件名称
  140. const name = item.name as string;
  141. cacheMap.add(name);
  142. }
  143. this.cacheTabList = cacheMap;
  144. },
  145. /**
  146. * Refresh tabs
  147. */
  148. async refreshPage(router: Router) {
  149. const { currentRoute } = router;
  150. const route = unref(currentRoute);
  151. const name = route.name;
  152. const findTab = this.getCachedTabList.find((item) => item === name);
  153. if (findTab) {
  154. this.cacheTabList.delete(findTab);
  155. }
  156. const redo = useRedo(router);
  157. await redo();
  158. },
  159. /**
  160. * 修改设计模式
  161. * changeDesign
  162. */
  163. async changeDesign(router: Router) {
  164. const { currentRoute } = router;
  165. const route = unref(currentRoute);
  166. const name = route.name;
  167. const findTab = this.getCachedTabList.find((item) => item === name);
  168. if (findTab) {
  169. this.cacheTabList.delete(findTab);
  170. }
  171. const redo = useRedo(router, { isDesign: true });
  172. await redo();
  173. },
  174. clearCacheTabs(): void {
  175. this.cacheTabList = new Set();
  176. },
  177. resetState(): void {
  178. this.tabList = [];
  179. this.clearCacheTabs();
  180. },
  181. goToPage(router: Router) {
  182. const go = useGo(router);
  183. const len = this.tabList.length;
  184. const { path } = unref(router.currentRoute);
  185. let toPath: PageEnum | string = PageEnum.BASE_HOME;
  186. if (len > 0) {
  187. const page = this.tabList[len - 1];
  188. const p = page.fullPath || page.path;
  189. if (p) {
  190. toPath = p;
  191. }
  192. }
  193. // Jump to the current page and report an error
  194. path !== toPath && go(toPath as PageEnum, true);
  195. },
  196. async addTab(route: RouteLocationNormalized) {
  197. const { path, name, fullPath, params, query, meta } = getRawRoute(route);
  198. // update-begin--author:liaozhiyang---date:202401127---for:【issues/7500】vue-router4.5.0版本路由name:PageNotFound同名导致登录进不去
  199. // 404 The page does not need to add a tab
  200. if (
  201. path === PageEnum.ERROR_PAGE ||
  202. path === PageEnum.BASE_LOGIN ||
  203. !name ||
  204. [REDIRECT_ROUTE.name, PAGE_NOT_FOUND_NAME_404].includes(name as string)
  205. ) {
  206. return;
  207. }
  208. // update-end--author:liaozhiyang---date:202401127---for:【issues/7500】vue-router4.5.0版本路由name:PageNotFound同名导致登录进不去
  209. let updateIndex = -1;
  210. // Existing pages, do not add tabs repeatedly
  211. const tabHasExits = this.tabList.some((tab, index) => {
  212. updateIndex = index;
  213. return (tab.fullPath || tab.path) === (fullPath || path);
  214. });
  215. // If the tab already exists, perform the update operation
  216. if (tabHasExits) {
  217. const curTab = toRaw(this.tabList)[updateIndex];
  218. if (!curTab) {
  219. return;
  220. }
  221. curTab.params = params || curTab.params;
  222. curTab.query = query || curTab.query;
  223. curTab.fullPath = fullPath || curTab.fullPath;
  224. this.tabList.splice(updateIndex, 1, curTab);
  225. } else {
  226. // Add tab
  227. // 获取动态路由打开数,超过 0 即代表需要控制打开数
  228. const dynamicLevel = meta?.dynamicLevel ?? -1;
  229. if (dynamicLevel > 0) {
  230. // 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了
  231. // 首先获取到真实的路由,使用配置方式减少计算开销.
  232. // const realName: string = path.match(/(\S*)\//)![1];
  233. const realPath = meta?.realPath ?? '';
  234. // 获取到已经打开的动态路由数, 判断是否大于某一个值
  235. if (this.tabList.filter((e) => e.meta?.realPath ?? '' === realPath).length >= dynamicLevel) {
  236. // 关闭第一个
  237. const index = this.tabList.findIndex((item) => item.meta.realPath === realPath);
  238. index !== -1 && this.tabList.splice(index, 1);
  239. }
  240. }
  241. this.tabList.push(route);
  242. }
  243. this.updateCacheTab();
  244. cacheTab && Persistent.setLocal(MULTIPLE_TABS_KEY, this.tabList);
  245. },
  246. async closeTab(tab: RouteLocationNormalized, router: Router) {
  247. const close = (route: RouteLocationNormalized) => {
  248. const { fullPath, meta: { affix } = {} } = route;
  249. if (affix) {
  250. return;
  251. }
  252. const index = this.tabList.findIndex((item) => item.fullPath === fullPath);
  253. index !== -1 && this.tabList.splice(index, 1);
  254. };
  255. const { currentRoute, replace } = router;
  256. const { path } = unref(currentRoute);
  257. if (path !== tab.path) {
  258. // Closed is not the activation tab
  259. close(tab);
  260. this.updateCacheTab();
  261. return;
  262. }
  263. // Closed is activated atb
  264. let toTarget: RouteLocationRaw = {};
  265. const index = this.tabList.findIndex((item) => item.path === path);
  266. // If the current is the leftmost tab
  267. if (index === 0) {
  268. // There is only one tab, then jump to the homepage, otherwise jump to the right tab
  269. if (this.tabList.length === 1) {
  270. const userStore = useUserStore();
  271. toTarget = userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
  272. } else {
  273. // Jump to the right tab
  274. const page = this.tabList[index + 1];
  275. toTarget = getToTarget(page);
  276. }
  277. } else {
  278. // Close the current tab
  279. const page = this.tabList[index - 1];
  280. toTarget = getToTarget(page);
  281. }
  282. close(currentRoute.value);
  283. await replace(toTarget);
  284. },
  285. // Close according to key
  286. async closeTabByKey(key: string, router: Router) {
  287. const index = this.tabList.findIndex((item) => (item.fullPath || item.path) === key);
  288. if (index !== -1) {
  289. await this.closeTab(this.tabList[index], router);
  290. const { currentRoute, replace } = router;
  291. // 检查当前路由是否存在于tabList中
  292. const isActivated = this.tabList.findIndex((item) => {
  293. return item.fullPath === currentRoute.value.fullPath;
  294. });
  295. // 如果当前路由不存在于TabList中,尝试切换到其它路由
  296. if (isActivated === -1) {
  297. let pageIndex;
  298. if (index > 0) {
  299. pageIndex = index - 1;
  300. } else if (index < this.tabList.length - 1) {
  301. pageIndex = index + 1;
  302. } else {
  303. pageIndex = -1;
  304. }
  305. if (pageIndex >= 0) {
  306. const page = this.tabList[index - 1];
  307. const toTarget = getToTarget(page);
  308. await replace(toTarget);
  309. }
  310. }
  311. }
  312. },
  313. // Sort the tabs
  314. async sortTabs(oldIndex: number, newIndex: number) {
  315. const currentTab = this.tabList[oldIndex];
  316. this.tabList.splice(oldIndex, 1);
  317. this.tabList.splice(newIndex, 0, currentTab);
  318. this.lastDragEndIndex = this.lastDragEndIndex + 1;
  319. },
  320. // Close the tab on the right and jump
  321. async closeLeftTabs(route: RouteLocationNormalized, router: Router) {
  322. const index = this.tabList.findIndex((item) => item.path === route.path);
  323. let isCloseCurrentTab = false;
  324. if (index > 0) {
  325. const leftTabs = this.tabList.slice(0, index);
  326. const pathList: string[] = [];
  327. for (const item of leftTabs) {
  328. const affix = item?.meta?.affix ?? false;
  329. if (!affix) {
  330. pathList.push(item.fullPath);
  331. }
  332. }
  333. // update-begin--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  334. isCloseCurrentTab = closeTabContainCurrentRoute(router, pathList);
  335. // update-end--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  336. this.bulkCloseTabs(pathList);
  337. }
  338. this.updateCacheTab();
  339. // update-begin--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  340. if (isCloseCurrentTab) {
  341. handleGotoPage(router, route.path);
  342. } else {
  343. handleGotoPage(router);
  344. }
  345. // update-end--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  346. },
  347. // Close the tab on the left and jump
  348. async closeRightTabs(route: RouteLocationNormalized, router: Router) {
  349. const index = this.tabList.findIndex((item) => item.fullPath === route.fullPath);
  350. let isCloseCurrentTab = false;
  351. if (index >= 0 && index < this.tabList.length - 1) {
  352. const rightTabs = this.tabList.slice(index + 1, this.tabList.length);
  353. const pathList: string[] = [];
  354. for (const item of rightTabs) {
  355. const affix = item?.meta?.affix ?? false;
  356. if (!affix) {
  357. pathList.push(item.fullPath);
  358. }
  359. }
  360. // update-begin--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  361. isCloseCurrentTab = closeTabContainCurrentRoute(router, pathList);
  362. // update-end--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  363. this.bulkCloseTabs(pathList);
  364. }
  365. this.updateCacheTab();
  366. // update-begin--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  367. if (isCloseCurrentTab) {
  368. handleGotoPage(router, route.path);
  369. } else {
  370. handleGotoPage(router);
  371. }
  372. // update-end--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  373. },
  374. async closeAllTab(router: Router) {
  375. this.tabList = this.tabList.filter((item) => item?.meta?.affix ?? false);
  376. this.clearCacheTabs();
  377. this.goToPage(router);
  378. },
  379. /**
  380. * Close other tabs
  381. */
  382. async closeOtherTabs(route: RouteLocationNormalized, router: Router) {
  383. const closePathList = this.tabList.map((item) => item.fullPath);
  384. let isCloseCurrentTab = false;
  385. const pathList: string[] = [];
  386. for (const path of closePathList) {
  387. if (path !== route.fullPath) {
  388. const closeItem = this.tabList.find((item) => item.path === path);
  389. if (!closeItem) {
  390. continue;
  391. }
  392. const affix = closeItem?.meta?.affix ?? false;
  393. if (!affix) {
  394. pathList.push(closeItem.fullPath);
  395. }
  396. }
  397. }
  398. isCloseCurrentTab = closeTabContainCurrentRoute(router, pathList);
  399. this.bulkCloseTabs(pathList);
  400. this.updateCacheTab();
  401. // update-begin--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  402. if (isCloseCurrentTab) {
  403. handleGotoPage(router, route.path);
  404. } else {
  405. handleGotoPage(router);
  406. }
  407. // update-end--author:liaozhiyang---date:20240605---for:【TV360X-732】非当前页右键关闭左侧、关闭右侧、关闭其它功能正常使用
  408. },
  409. /**
  410. * Close tabs in bulk
  411. */
  412. async bulkCloseTabs(pathList: string[]) {
  413. this.tabList = this.tabList.filter((item) => !pathList.includes(item.fullPath));
  414. },
  415. /**
  416. * Set tab's title
  417. */
  418. async setTabTitle(title: string, route: RouteLocationNormalized) {
  419. const findTab = this.getTabList.find((item) => item === route);
  420. if (findTab) {
  421. findTab.meta.title = title;
  422. await this.updateCacheTab();
  423. }
  424. },
  425. /**
  426. * replace tab's path
  427. * **/
  428. async updateTabPath(fullPath: string, route: RouteLocationNormalized) {
  429. const findTab = this.getTabList.find((item) => item === route);
  430. if (findTab) {
  431. findTab.fullPath = fullPath;
  432. findTab.path = fullPath;
  433. await this.updateCacheTab();
  434. }
  435. },
  436. setRedirectPageParam(data) {
  437. this.redirectPageParam = data;
  438. },
  439. getRedirectPageParam() {
  440. return this.redirectPageParam;
  441. },
  442. },
  443. });
  444. // Need to be used outside the setup
  445. export function useMultipleTabWithOutStore() {
  446. return useMultipleTabStore(store);
  447. }