Blame view

src/router/guard/permissionGuard.ts 3.31 KB
陈文彬 authored
1
2
import type { Router, RouteRecordRaw } from 'vue-router';
3
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
vben authored
4
陈文彬 authored
5
import { PageEnum } from '/@/enums/pageEnum';
6
import { useUserStoreWithOut } from '/@/store/modules/user';
vben authored
7
8
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
陈文彬 authored
9
10
11
import { RootRoute } from '/@/router/routes';
陈文彬 authored
12
13
const LOGIN_PATH = PageEnum.BASE_LOGIN;
14
15
const ROOT_PATH = RootRoute.path;
陈文彬 authored
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;
陈文彬 authored
30
31
    }
32
33
    const token = userStore.getToken;
陈文彬 authored
34
35
    // Whitelist can be directly entered
    if (whitePathList.includes(to.path as PageEnum)) {
36
37
38
39
40
41
42
43
44
45
      if (to.path === LOGIN_PATH && token) {
        const isSessionTimeout = userStore.getSessionTimeout;
        try {
          await userStore.afterLoginAction();
          if (!isSessionTimeout) {
            next((to.query?.redirect as string) || '/');
            return;
          }
        } catch {}
      }
46
      next();
陈文彬 authored
47
48
49
50
51
52
      return;
    }

    // token does not exist
    if (!token) {
      // You can access without permission. You need to set the routing meta.ignoreAuth to true
53
      if (to.meta.ignoreAuth) {
54
        next();
陈文彬 authored
55
56
        return;
      }
57
陈文彬 authored
58
      // redirect login page
Vben authored
59
      const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
60
61
62
63
64
65
66
67
68
        path: LOGIN_PATH,
        replace: true,
      };
      if (to.path) {
        redirectData.query = {
          ...redirectData.query,
          redirect: to.path,
        };
      }
69
70
      next(redirectData);
      return;
陈文彬 authored
71
    }
72
73
74
75
76
77
78
79
80
81
82
    // 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;
    }
83
84
    // get userinfo while last fetch time is empty
    if (userStore.getLastUpdateTime === 0) {
85
86
87
88
89
90
      try {
        await userStore.getUserInfoAction();
      } catch (err) {
        next();
        return;
      }
91
92
    }
Vben authored
93
    if (permissionStore.getIsDynamicAddedRoute) {
94
      next();
陈文彬 authored
95
96
      return;
    }
97
陈文彬 authored
98
    const routes = await permissionStore.buildRoutesAction();
vben authored
99
陈文彬 authored
100
    routes.forEach((route) => {
Vben authored
101
      router.addRoute(route as unknown as RouteRecordRaw);
陈文彬 authored
102
103
    });
104
105
    router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
Vben authored
106
    permissionStore.setDynamicAddedRoute(true);
107
108
109

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