permissionGuard.ts 3.0 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
    }

    // Whitelist can be directly entered
    if (whitePathList.includes(to.path as PageEnum)) {
34
      next();
陈文彬 已提交
35 36 37
      return;
    }

V
Vben 已提交
38
    const token = userStore.getToken;
陈文彬 已提交
39 40 41 42

    // token does not exist
    if (!token) {
      // You can access without permission. You need to set the routing meta.ignoreAuth to true
43
      if (to.meta.ignoreAuth) {
44
        next();
陈文彬 已提交
45 46
        return;
      }
47

陈文彬 已提交
48
      // redirect login page
V
Vben 已提交
49
      const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
50 51 52 53 54 55 56 57 58
        path: LOGIN_PATH,
        replace: true,
      };
      if (to.path) {
        redirectData.query = {
          ...redirectData.query,
          redirect: to.path,
        };
      }
59 60
      next(redirectData);
      return;
陈文彬 已提交
61
    }
62

无木 已提交
63 64 65 66 67 68 69 70 71 72
    // 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;
    }

73 74
    // get userinfo while last fetch time is empty
    if (userStore.getLastUpdateTime === 0) {
75 76 77 78 79 80
      try {
        await userStore.getUserInfoAction();
      } catch (err) {
        next();
        return;
      }
81 82
    }

V
Vben 已提交
83
    if (permissionStore.getIsDynamicAddedRoute) {
84
      next();
陈文彬 已提交
85 86
      return;
    }
87

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

陈文彬 已提交
90
    routes.forEach((route) => {
V
Vben 已提交
91
      router.addRoute(route as unknown as RouteRecordRaw);
陈文彬 已提交
92 93
    });

94 95
    router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);

V
Vben 已提交
96
    permissionStore.setDynamicAddedRoute(true);
97 98 99

    if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
      // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
100
      next({ path: to.fullPath, replace: true, query: to.query });
101 102 103 104 105 106
    } 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);
    }
陈文彬 已提交
107 108
  });
}