提交 | 用户 | 时间
|
314507
|
1 |
import router from './router' |
H |
2 |
import type { RouteRecordRaw } from 'vue-router' |
|
3 |
import { isRelogin } from '@/config/axios/service' |
|
4 |
import { getAccessToken } from '@/utils/auth' |
|
5 |
import { useTitle } from '@/hooks/web/useTitle' |
|
6 |
import { useNProgress } from '@/hooks/web/useNProgress' |
|
7 |
import { usePageLoading } from '@/hooks/web/usePageLoading' |
|
8 |
import { useUserStoreWithOut } from '@/store/modules/user' |
|
9 |
import { usePermissionStoreWithOut } from '@/store/modules/permission' |
|
10 |
|
|
11 |
const { start, done } = useNProgress() |
|
12 |
|
|
13 |
const { loadStart, loadDone } = usePageLoading() |
|
14 |
|
|
15 |
const parseURL = ( |
|
16 |
url: string | null | undefined |
|
17 |
): { basePath: string; paramsObject: { [key: string]: string } } => { |
|
18 |
// 如果输入为 null 或 undefined,返回空字符串和空对象 |
|
19 |
if (url == null) { |
|
20 |
return { basePath: '', paramsObject: {} } |
|
21 |
} |
|
22 |
|
|
23 |
// 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数 |
|
24 |
const questionMarkIndex = url.indexOf('?') |
|
25 |
let basePath = url |
|
26 |
const paramsObject: { [key: string]: string } = {} |
|
27 |
|
|
28 |
// 如果找到了问号,说明有查询参数 |
|
29 |
if (questionMarkIndex !== -1) { |
|
30 |
// 获取 basePath |
|
31 |
basePath = url.substring(0, questionMarkIndex) |
|
32 |
|
|
33 |
// 从 URL 中获取查询字符串部分 |
|
34 |
const queryString = url.substring(questionMarkIndex + 1) |
|
35 |
|
|
36 |
// 使用 URLSearchParams 遍历参数 |
|
37 |
const searchParams = new URLSearchParams(queryString) |
|
38 |
searchParams.forEach((value, key) => { |
|
39 |
// 封装进 paramsObject 对象 |
|
40 |
paramsObject[key] = value |
|
41 |
}) |
|
42 |
} |
|
43 |
|
|
44 |
// 返回 basePath 和 paramsObject |
|
45 |
return { basePath, paramsObject } |
|
46 |
} |
|
47 |
|
|
48 |
// 路由不重定向白名单 |
|
49 |
const whiteList = [ |
|
50 |
'/login', |
8e4ab7
|
51 |
'/callback', |
314507
|
52 |
'/social-login', |
H |
53 |
'/auth-redirect', |
|
54 |
'/bind', |
|
55 |
'/register', |
|
56 |
'/oauthLogin/gitee' |
|
57 |
] |
|
58 |
|
|
59 |
// 路由加载前 |
|
60 |
router.beforeEach(async (to, from, next) => { |
|
61 |
start() |
|
62 |
loadStart() |
|
63 |
if (getAccessToken()) { |
|
64 |
if (to.path === '/login') { |
|
65 |
next({ path: '/' }) |
|
66 |
} else { |
|
67 |
const userStore = useUserStoreWithOut() |
|
68 |
const permissionStore = usePermissionStoreWithOut() |
|
69 |
// if (!dictStore.getIsSetDict) { |
|
70 |
// await dictStore.setDictMap() |
|
71 |
// } |
|
72 |
if (!userStore.getIsSetUser) { |
|
73 |
isRelogin.show = true |
|
74 |
await userStore.setUserInfoAction() |
|
75 |
isRelogin.show = false |
|
76 |
// 后端过滤菜单 |
|
77 |
await permissionStore.generateRoutes() |
|
78 |
permissionStore.getAddRouters.forEach((route) => { |
|
79 |
router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表 |
|
80 |
}) |
|
81 |
const redirectPath = from.query.redirect || to.path |
|
82 |
// 修复跳转时不带参数的问题 |
|
83 |
const redirect = decodeURIComponent(redirectPath as string) |
|
84 |
const { paramsObject: query } = parseURL(redirect) |
|
85 |
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query } |
|
86 |
next(nextData) |
|
87 |
} else { |
|
88 |
next() |
|
89 |
} |
|
90 |
} |
|
91 |
} else { |
|
92 |
if (whiteList.indexOf(to.path) !== -1) { |
|
93 |
next() |
|
94 |
} else { |
|
95 |
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 |
|
96 |
} |
|
97 |
} |
|
98 |
}) |
|
99 |
|
|
100 |
router.afterEach((to) => { |
|
101 |
useTitle(to?.meta?.title as string) |
|
102 |
done() // 结束Progress |
|
103 |
loadDone() |
|
104 |
}) |