permissionGuard.ts 3.3 KB
Newer Older
陈文彬 已提交
1 2
import type { Router, RouteRecordRaw } from 'vue-router';

3
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
V
vben 已提交
4

陈文彬 已提交
5
import { PageEnum } from '/@/enums/pageEnum';
6
import { useUserStoreWithOut } from '/@/store/modules/user';
V
vben 已提交
7

8
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
陈文彬 已提交
9

无木 已提交
10 11
import { RootRoute } from '/@/router/routes';

陈文彬 已提交
12 13
const LOGIN_PATH = PageEnum.BASE_LOGIN;

无木 已提交
14 15
const ROOT_PATH = RootRoute.path;

陈文彬 已提交
16 17 18
const whitePathList: PageEnum[] = [LOGIN_PATH];

export function createPermissionGuard(router: Router) {
19 20
  const userStore = useUserStoreWithOut();
  const permissionStore = usePermissionStoreWithOut();
21
  router.beforeEach(async (to, from, next) => {
无木 已提交
22 23 24 25 26 27
    if (
      from.path === ROOT_PATH &&
      to.path === PageEnum.BASE_HOME &&
      userStore.getUserInfo.homePath &&
      userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
    ) {
28 29
      next(userStore.getUserInfo.homePath);
      return;
陈文彬 已提交
30 31
    }

32 33
    const token = userStore.getToken;

陈文彬 已提交
34 35
    // Whitelist can be directly entered
    if (whitePathList.includes(to.path as PageEnum)) {
36 37 38 39 40 41 42 43
      if (to.path === LOGIN_PATH && token) {
        const isSessionTimeout = userStore.getSessionTimeout;
        try {
          await userStore.afterLoginAction();
          if (!isSessionTimeout) {
            next((to.query?.redirect as string) || '/');
            return;
          }
V
vben 已提交
44 45 46
        } catch {
          //
        }
47
      }
48
      next();
陈文彬 已提交
49 50
      return;
    }
51
    // token or user does not exist
陈文彬 已提交
52 53
    if (!token) {
      // You can access without permission. You need to set the routing meta.ignoreAuth to true
54
      if (to.meta.ignoreAuth) {
55
        next();
陈文彬 已提交
56 57
        return;
      }
58

陈文彬 已提交
59
      // redirect login page
V
Vben 已提交
60
      const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
61 62 63 64 65 66 67 68 69
        path: LOGIN_PATH,
        replace: true,
      };
      if (to.path) {
        redirectData.query = {
          ...redirectData.query,
          redirect: to.path,
        };
      }
70 71
      next(redirectData);
      return;
陈文彬 已提交
72
    }
73

无木 已提交
74 75 76 77 78 79 80 81 82 83
    // 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;
    }

84 85
    // get userinfo while last fetch time is empty
    if (userStore.getLastUpdateTime === 0) {
86 87 88 89 90 91
      try {
        await userStore.getUserInfoAction();
      } catch (err) {
        next();
        return;
      }
92 93
    }

V
Vben 已提交
94
    if (permissionStore.getIsDynamicAddedRoute) {
95
      next();
陈文彬 已提交
96 97
      return;
    }
98

陈文彬 已提交
99
    const routes = await permissionStore.buildRoutesAction();
V
vben 已提交
100

陈文彬 已提交
101
    routes.forEach((route) => {
V
Vben 已提交
102
      router.addRoute(route as unknown as RouteRecordRaw);
陈文彬 已提交
103 104
    });

105 106
    router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);

V
Vben 已提交
107
    permissionStore.setDynamicAddedRoute(true);
108 109 110

    if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
      // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
111
      next({ path: to.fullPath, replace: true, query: to.query });
112 113 114 115 116 117
    } 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);
    }
陈文彬 已提交
118 119
  });
}