ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type { Router } from 'vue-router';
 
import { LOGIN_PATH } from '@vben/constants';
import { preferences } from '@vben/preferences';
import { useAccessStore, useUserStore } from '@vben/stores';
import { startProgress, stopProgress } from '@vben/utils';
 
import { addCommonMenu } from '#/api/system/collectMenu';
import { accessRoutes, coreRouteNames } from '#/router/routes';
import { useAuthStore } from '#/store';
import { getJnpfAppEnCode, getRealJnpfAppEnCode } from '#/utils/jnpf';
 
import { generateAccess } from './access';
 
/**
 * 通用守卫配置
 * @param router
 */
function setupCommonGuard(router: Router) {
  // 记录已经加载的页面
  const loadedPaths = new Set<string>();
 
  router.beforeEach((to) => {
    to.meta.loaded = loadedPaths.has(to.path);
 
    // 页面加载进度条
    if (!to.meta.loaded && preferences.transition.progress) {
      startProgress();
    }
    return true;
  });
 
  router.afterEach(async (to) => {
    const modelId = to.meta?.modelId || '';
    if (modelId) await addCommonMenu(modelId);
    // 记录页面是否加载,如果已经加载,后续的页面切换动画等效果不在重复执行
    loadedPaths.add(to.path);
 
    // 关闭页面加载进度条
    if (preferences.transition.progress) {
      stopProgress();
    }
  });
}
 
/**
 * 权限访问守卫配置
 * @param router
 */
function setupAccessGuard(router: Router) {
  router.beforeEach(async (to, from) => {
    const accessStore = useAccessStore();
    const userStore = useUserStore();
    const authStore = useAuthStore();
    const appEnCode = getJnpfAppEnCode();
 
    if (['/reportPreview', '/workFlowDetail'].includes(to.path) && to.query.token && accessStore.accessToken != to.query.token) {
      accessStore.setAccessToken(to.query.token as string);
      return { ...to, replace: true };
    }
 
    // 基本路由,这些路由不需要进入权限拦截
    if (coreRouteNames.includes(to.name as string)) {
      if (to.path === LOGIN_PATH && accessStore.accessToken) {
        return decodeURIComponent((to.query?.redirect as string) || preferences.app.defaultHomePath);
      }
      return true;
    }
 
    // accessToken 检查
    if (!accessStore.accessToken) {
      // 明确声明忽略权限访问权限,则可以访问
      if (to.meta.ignoreAccess) {
        return true;
      }
 
      // 没有访问权限,跳转登录页面
      if (to.fullPath !== LOGIN_PATH) {
        return {
          path: LOGIN_PATH,
          // 如不需要,直接删除 query
          query: to.fullPath === preferences.app.defaultHomePath ? {} : { redirect: encodeURIComponent(to.fullPath) },
          // 携带当前跳转的页面,登录后重新跳转该页面
          replace: true,
        };
      }
      return to;
    }
 
    // 是否已经生成过动态路由
    if (accessStore.isAccessChecked) {
      if ((['teamwork', 'workFlow'].includes(appEnCode) || appEnCode !== getRealJnpfAppEnCode()) && to.path === preferences.app.defaultHomePath) {
        let path = '/404';
        if (accessStore.backMenus.length && accessStore.backMenus[0]?.children?.length) {
          path = `/${accessStore.backMenus[0].children[0]?.urlAddress}`;
        }
        return {
          ...router.resolve(path),
          replace: true,
        };
      }
      return true;
    }
 
    // 生成路由表
    // 当前登录用户拥有的角色标识列表
    !userStore.userInfo && (await authStore.fetchUserInfo());
 
    // 生成菜单和路由
    const { accessibleMenus, accessibleRoutes } = await generateAccess({
      router,
      routes: accessRoutes,
      backMenus: accessStore.backMenus,
    });
 
    // 保存菜单信息和路由信息
    accessStore.setAccessMenus(accessibleMenus);
    accessStore.setAccessRoutes(accessibleRoutes);
    accessStore.setIsAccessChecked(true);
    const redirectPath = (from.query.redirect ?? (to.path === preferences.app.defaultHomePath ? preferences.app.defaultHomePath : to.fullPath)) as string;
 
    if ((['teamwork', 'workFlow'].includes(appEnCode) || appEnCode !== getRealJnpfAppEnCode()) && to.path === preferences.app.defaultHomePath) {
      let path = '/404';
      if (accessStore.backMenus.length && accessStore.backMenus[0]?.children?.length) {
        path = `/${accessStore.backMenus[0].children[0]?.urlAddress}`;
      }
      return {
        ...router.resolve(path),
        replace: true,
      };
    }
 
    return {
      ...router.resolve(decodeURIComponent(redirectPath)),
      replace: true,
    };
  });
}
 
/**
 * 项目守卫配置
 * @param router
 */
function createRouterGuard(router: Router) {
  /** 通用 */
  setupCommonGuard(router);
  /** 权限访问 */
  setupAccessGuard(router);
}
 
export { createRouterGuard };