Explorar el Código

修改重定向

wangfumin hace 2 semanas
padre
commit
9da1453a08
Se han modificado 1 ficheros con 30 adiciones y 2 borrados
  1. 30 2
      src/request/index.ts

+ 30 - 2
src/request/index.ts

@@ -99,7 +99,22 @@ axios.interceptors.request.use(async (config) => {
     const offline = isOfflineMode();
     if (!offline) {
       if (!token && !~notLoginUrls.indexOf(config.url) && !shareBypassLogin) {
-        const redirect = window.location.href;
+        const buildRedirect = (): string => {
+          try {
+            const current = router.currentRoute?.value;
+            // 如果已经在登录页且已有 redirect 参数,避免递归叠加
+            const isLogin = current?.name === RouteName.login;
+            const existing = (current?.query?.redirect as string | undefined);
+            if (isLogin && existing) {
+              return existing;
+            }
+          } catch {}
+          const { origin, pathname, search, hash } = window.location;
+          // 为了在 hash 路由中作为查询参数安全传递,双重编码 hash
+          const safeHash = hash ? encodeURIComponent(encodeURIComponent(hash)) : "";
+          return origin + pathname + (search || "") + safeHash;
+        };
+        const redirect = buildRedirect();
         console.log(redirect, 'redirect')
         router.replace({ name: RouteName.login, query: { redirect } });
         throw "用户未登录";
@@ -159,7 +174,20 @@ const responseInterceptor = (res: AxiosResponse<any, any>) => {
         errMsg === "token已经失效,请重新登录"
       ) {
         getAuth().clear();
-        const redirect = window.location.href;
+        const buildRedirect = (): string => {
+          try {
+            const current = router.currentRoute?.value;
+            const isLogin = current?.name === RouteName.login;
+            const existing = (current?.query?.redirect as string | undefined);
+            if (isLogin && existing) {
+              return existing;
+            }
+          } catch {}
+          const { origin, pathname, search, hash } = window.location;
+          const safeHash = hash ? encodeURIComponent(encodeURIComponent(hash)) : "";
+          return origin + pathname + (search || "") + safeHash;
+        };
+        const redirect = buildRedirect();
         router.replace({ name: RouteName.login, query: { redirect } });
       }
     }