keepAlive.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { defineStore } from "pinia";
  2. import { KeepAliveState } from "@/stores/interface";
  3. import { cloneDeep } from "lodash-es";
  4. import { useTabsStore } from "@/stores/modules/tabs";
  5. export const useKeepAliveStore = defineStore({
  6. id: "geeker-keepAlive",
  7. state: (): KeepAliveState => ({
  8. keepAliveName: [],
  9. keepAliveList: []
  10. }),
  11. getters: {
  12. getKeepAliveList(): any[] {
  13. return this.keepAliveList;
  14. }
  15. },
  16. actions: {
  17. setKeepAliveList(keepAliveList: any) {
  18. this.keepAliveList = keepAliveList;
  19. console.log("getKeepAliveName routeItem setKeepAliveList :>> ", JSON.stringify(this.keepAliveList));
  20. console.log("getKeepAliveName routeItem setKeepAliveList :>> ", this.keepAliveList);
  21. },
  22. // Add KeepAliveName
  23. async addKeepAliveName(route: any) {
  24. !this.keepAliveName.includes(route.name) && this.keepAliveName.push(route.name);
  25. let routeItem = this.keepAliveList.find((item: any) => item.fullPath == route.fullPath);
  26. if (!routeItem) {
  27. routeItem = {
  28. fullPath: route.fullPath,
  29. times: 0
  30. };
  31. this.keepAliveList.push(routeItem);
  32. }
  33. },
  34. // Remove KeepAliveName
  35. removeKeepAliveName(route: any) {
  36. this.keepAliveName = this.keepAliveName.filter(item => item !== route.name);
  37. let index = this.keepAliveList.findIndex(item => item.fullPath === route.path);
  38. if (index > -1) {
  39. // this.keepAliveList.splice(index, 1);
  40. this.keepAliveList[index].times = this.keepAliveList[index].times + 1;
  41. }
  42. console.log("getKeepAliveName routeItem removeKeepAliveName :>> ", JSON.stringify(this.keepAliveList));
  43. console.log("getKeepAliveName routeItem removeKeepAliveName :>> ", this.keepAliveList);
  44. console.log("getKeepAliveName routeItem keepAliveName :>> ", this.keepAliveName);
  45. },
  46. // Set KeepAliveName
  47. async setKeepAliveName(keepAliveName: string[] = []) {
  48. this.keepAliveName = keepAliveName;
  49. },
  50. async updateKeepAliveName(route: any) {
  51. this.addKeepAliveName(route);
  52. console.log("updateKeepAliveName route :>> ", route, this.keepAliveList);
  53. this.keepAliveList.forEach((item: any) => {
  54. console.log("updateKeepAliveName item :>> ", item.fullPath, route.fullPath, item.fullPath == route.fullPath);
  55. if (item.fullPath == route.fullPath) {
  56. item.times++;
  57. }
  58. });
  59. },
  60. getKeepAliveName(name: string) {
  61. let routeItem = this.keepAliveList.find((item: any) => item.fullPath == name);
  62. if (!routeItem) {
  63. routeItem = {
  64. fullPath: name,
  65. times: 0
  66. };
  67. this.keepAliveList.push(routeItem);
  68. }
  69. return routeItem;
  70. },
  71. computedKeepAliveName(to, from) {
  72. const tabStore = useTabsStore();
  73. const tabsParams = {
  74. icon: to.meta.icon as string,
  75. title: to.meta.title as string,
  76. path: to.fullPath,
  77. name: to.name as string,
  78. close: !to.meta.isAffix
  79. };
  80. console.log("tabStore :>> ", tabStore, to);
  81. console.log("tabsParams router:>> ", tabsParams);
  82. let isReplace = false;
  83. // console.log(
  84. // 'tabsParams.path.indexOf("/edit?") > -1 , :>> ',
  85. // tabsParams.path.indexOf("/edit?") > -1,
  86. // tabsParams.path.indexOf("/edit/") > -1
  87. // );
  88. // console.log(
  89. // 'tabsParams.path.indexOf("/detail?") > -1 , :>> ',
  90. // tabsParams.path.indexOf("/detail?") > -1,
  91. // tabsParams.path.indexOf("/detail/") > -1
  92. // );
  93. console.log(
  94. 'to.fullPath.split("?")[0] != from.fullPath.split("?")[0] :>> ',
  95. to.fullPath.split("?")[0],
  96. from.fullPath.split("?")[0],
  97. to.fullPath.split("?")[0] != from.fullPath.split("?")[0]
  98. );
  99. // if (from.meta?.activeMenu == to.meta?.activeMenu && to.fullPath.split("?")[0] != from.fullPath.split("?")[0]) {
  100. if (tabsParams.path.indexOf("/edit?") > -1 || tabsParams.path.indexOf("/edit/") > -1) {
  101. let key = "/edit?";
  102. let name = "";
  103. let _path = tabsParams.path.replace(key, "/detail?");
  104. // let _path = from.fullPath;
  105. // 使用路由参数的地址,忽略查询参数后,查找相同地址
  106. let _path2 = tabsParams.path.replace("/edit/", "/detail/");
  107. let hasPath1 = to.fullPath.indexOf("/edit?") > -1;
  108. let hasPath2 = to.fullPath.indexOf("/edit/") > -1;
  109. let tabsMenuList = tabStore.tabsMenuList;
  110. console.log("tabsMenuList :>> ", JSON.stringify(tabsMenuList));
  111. for (let i = 0; i < tabsMenuList.length; i++) {
  112. if (
  113. (hasPath1 && tabsMenuList[i].path == _path) ||
  114. (hasPath2 && tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0])
  115. ) {
  116. console.log(
  117. "computedKeepAliveName edit ====>",
  118. i,
  119. tabsMenuList[i].path,
  120. _path,
  121. tabsMenuList[i].path == _path,
  122. hasPath2,
  123. tabsMenuList[i].path.split("?")[0],
  124. _path2.split("?")[0],
  125. tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0],
  126. tabsMenuList[i]
  127. );
  128. name = tabsMenuList[i].name;
  129. tabsMenuList[i] = tabsParams;
  130. isReplace = true;
  131. break;
  132. }
  133. }
  134. console.log("keilll name edit:>> ", name);
  135. tabStore.setTabs(tabsMenuList);
  136. name && this.updateKeepAliveName(to);
  137. // to.meta.isKeepAlive && this.updateKeepAliveName(route);
  138. } else if (tabsParams.path.indexOf("/detail?") > -1 || tabsParams.path.indexOf("/detail/") > -1) {
  139. let key = "/detail?";
  140. let name = "";
  141. let _path = tabsParams.path.replace(key, "/edit?");
  142. // let _path = from.fullPath;
  143. // 使用路由参数的地址,忽略查询参数后,查找相同地址
  144. let _path2 = tabsParams.path.replace("/detail/", "/edit/");
  145. let hasPath1 = to.fullPath.indexOf("/detail?") > -1;
  146. let hasPath2 = to.fullPath.indexOf("/detail/") > -1;
  147. let tabsMenuList = tabStore.tabsMenuList;
  148. console.log("tabsMenuList :>> ", JSON.stringify(tabsMenuList));
  149. for (let i = 0; i < tabsMenuList.length; i++) {
  150. if (
  151. (hasPath1 &&
  152. (tabsMenuList[i].path == _path ||
  153. (tabsMenuList[i].path == from.fullPath && from.fullPath.indexOf("/copy?") > -1))) ||
  154. (hasPath2 && tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0])
  155. ) {
  156. console.log(
  157. "computedKeepAliveName detail ====>",
  158. tabsMenuList[i].path,
  159. _path,
  160. tabsMenuList[i].path == _path,
  161. hasPath2,
  162. tabsMenuList[i].path.split("?")[0],
  163. _path2.split("?")[0],
  164. tabsMenuList[i].path.split("?")[0] == _path2.split("?")[0],
  165. tabsMenuList[i]
  166. );
  167. name = tabsMenuList[i].name;
  168. tabsMenuList[i] = tabsParams;
  169. isReplace = true;
  170. break;
  171. }
  172. }
  173. tabStore.setTabs(tabsMenuList);
  174. console.log("keilll name detail:>> ", name);
  175. name && this.updateKeepAliveName(to);
  176. // to.meta.isKeepAlive && this.addKeepAliveName(to);
  177. console.log("add this.keepAliveName :>> ", this.keepAliveName);
  178. }
  179. // }
  180. !isReplace && tabStore.addTabs(tabsParams);
  181. !isReplace && to.meta.isKeepAlive && this.addKeepAliveName(to);
  182. }
  183. }
  184. });