xachary
2024-02-27 ab55cbf99bd9891f10546176e203eb4ea8cafaa4
src/router/guard/permissionGuard.ts
@@ -38,7 +38,7 @@
        try {
          await userStore.afterLoginAction();
          if (!isSessionTimeout) {
            next((to.query?.redirect as string) || '/');
            next(decodeURIComponent((to.query?.redirect as string) || '/'));
            return;
          }
        } catch {
@@ -71,16 +71,6 @@
      return;
    }
    // Jump to the 404 page after processing the login
    if (
      from.path === LOGIN_PATH &&
      to.name === PAGE_NOT_FOUND_ROUTE.name &&
      to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
    ) {
      next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
      return;
    }
    // get userinfo while last fetch time is empty
    if (userStore.getLastUpdateTime === 0) {
      try {
@@ -91,29 +81,54 @@
      }
    }
    if (permissionStore.getIsDynamicAddedRoute) {
      next();
    // 动态路由加载(首次)
    if (!permissionStore.getIsDynamicAddedRoute) {
      const routes = await permissionStore.buildRoutesAction();
      [...routes, PAGE_NOT_FOUND_ROUTE].forEach((route) => {
        router.addRoute(route as unknown as RouteRecordRaw);
      });
      // 记录动态路由加载完成
      permissionStore.setDynamicAddedRoute(true);
      // 现在的to动态路由加载之前的,可能为PAGE_NOT_FOUND_ROUTE(例如,登陆后,刷新的时候)
      // 此处应当重定向到fullPath,否则会加载404页面内容
      next({ path: to.fullPath, replace: true, query: to.query });
      return;
    }
    const routes = await permissionStore.buildRoutesAction();
    routes.forEach((route) => {
      router.addRoute(route as unknown as RouteRecordRaw);
    });
    router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
    permissionStore.setDynamicAddedRoute(true);
    if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
      // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
      next({ path: to.fullPath, replace: true, query: to.query });
      // 遇到不存在页面,后续逻辑不再处理redirect(阻止下面else逻辑)
      from.query.redirect = '';
      if (
        from.path === LOGIN_PATH &&
        to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
      ) {
        // 登陆重定向不存在路由,转去“首页”
        next({ path: userStore.getUserInfo.homePath || PageEnum.BASE_HOME, replace: true });
      } else {
        // 正常前往“404”页面
        next();
      }
    } else if (from.query.redirect) {
      // 存在redirect
      const redirect = decodeURIComponent((from.query.redirect as string) || '');
      // 只处理一次 from.query.redirect
      // 也避免某场景(指向路由定义了 redirect)下的死循环
      from.query.redirect = '';
      if (redirect === to.fullPath) {
        // 已经被redirect
        next();
      } else {
        // 指向redirect
        next({ path: redirect, replace: true });
      }
      next();
    } else {
      const redirectPath = (from.query.redirect || to.path) as string;
      const redirect = decodeURIComponent(redirectPath);
      const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
      next(nextData);
      // 正常访问
      next();
    }
  });
}