Browse Source

覆盖路由原始代码文件

lcl 7 months ago
parent
commit
7f6c975343
1 changed files with 25 additions and 4 deletions
  1. 25 4
      src/router/index.ts

+ 25 - 4
src/router/index.ts

@@ -25,7 +25,7 @@ import { buildHierarchyTree } from "@/utils/tree";
 import { isUrl, openLink, storageSession } from "@pureadmin/utils";
 
 import remainingRouter from "./modules/remaining";
-import { ShakeDownDecrypt } from "@/utils/CryptoJS";
+import { ShakeDownDecrypt, ShakeDownEncrypt } from "@/utils/CryptoJS";
 
 /** 自动导入全部静态路由,无需再手动引入!匹配 src/router/modules 目录(任何嵌套级别)中具有 .ts 扩展名的所有文件,除了 remaining.ts 文件
  * 如何匹配所有文件请看:https://github.com/mrmlnc/fast-glob#basic-syntax
@@ -107,8 +107,8 @@ router.beforeEach((to: toRouteType, _from, next) => {
     }
   }
   const userInfo =
-    storageSession().getItem<DataInfo<number>>(sessionKey) &&
-    ShakeDownDecrypt(storageSession().getItem<DataInfo<number>>(sessionKey));
+    storageSession().getItem(sessionKey) &&
+    ShakeDownDecrypt(storageSession().getItem(sessionKey));
   // NProgress.start();
   const externalLink = isUrl(to?.name as string);
   if (!externalLink) {
@@ -135,6 +135,15 @@ router.beforeEach((to: toRouteType, _from, next) => {
         openLink(to?.name as string);
         // NProgress.done();
       } else {
+        if (Object.keys(to?.query).length > 0) {
+          storageSession().setItem(to?.path, ShakeDownEncrypt(to?.query));
+        }
+        const routerQuery = setRouterQuery(to.path);
+        if (Object.keys(routerQuery).length > 0) {
+          to.query = routerQuery;
+          to.fullPath = forMatterRouterLink(to.path, routerQuery);
+          to.href = forMatterRouterLink(to.path, routerQuery);
+        }
         toCorrectRoute();
       }
     } else {
@@ -176,7 +185,19 @@ router.beforeEach((to: toRouteType, _from, next) => {
   }
 });
 
-router.afterEach(() => {
+function setRouterQuery(toUrl: string) {
+  return storageSession().getItem(toUrl)
+    ? ShakeDownDecrypt(storageSession().getItem(toUrl))
+    : {};
+}
+function forMatterRouterLink(url: string, object: object) {
+  url += "?";
+  for (const iterator in object) {
+    url += `${iterator}=${object[iterator]}`;
+  }
+  return url;
+}
+router.afterEach(to => {
   // NProgress.done();
 });
 export default router;