permissionGuard.ts
1.72 KB
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
import { RuntimeEnvironment } from "@/enums/external/envEnum";
import { PageEnum } from "@/enums/pageEnum";
import { useUserStoreWithOut } from "@/store/external/module/user";
import { RouteLocationRaw, Router } from "vue-router";
const whitePathList: string[] = [PageEnum.BASE_LOGIN];
const isIndependenceMode = import.meta.env.MODE === RuntimeEnvironment.INDEPENDENCE
const toIotPlatformLogin = () => {
const { origin } = window.location
window.location.replace(`${origin}/login?redirect=${import.meta.env.VITE_GLOB_PUBLIC_PATH}`)
}
export function createPermissionGuard(router: Router) {
const userStore = useUserStoreWithOut()
router.beforeEach((to, from, next) => {
if (from.path === '/' &&
to.path === PageEnum.BASE_HOME &&
userStore.getUserInfo.homePath &&
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
) {
console.log(userStore.getUserInfo.homePath)
next(userStore.getUserInfo.homePath)
return
}
const token = userStore.getJwtToken
if (whitePathList.includes(to.path)) {
if (to.path === PageEnum.BASE_LOGIN && token) {
const isSessionTimeout = userStore.getSessionTimeout
try {
if (!isSessionTimeout) {
next('/')
}
} catch (error) {
console.error(error)
}
}
else {
if (isIndependenceMode) {
toIotPlatformLogin()
return
}
next()
return
}
}
if (!token) {
const redirectData = {
path: PageEnum.BASE_LOGIN,
redirect: true
} as RouteLocationRaw
if (isIndependenceMode) {
toIotPlatformLogin()
return
}
next(redirectData)
} else {
next()
}
})
}