permission.js
4.2 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
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
/*
* @Description:
* @Autor: renchao
* @LastEditTime: 2023-06-26 10:18:09
*/
import Vue from 'vue'
import axios from 'axios'
import store from "./store";
import router from "./router";
import { getMenuInfo } from "@/api/user";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import getPageTitle from "@/utils/get-page-title";
import getTheme from "@/utils/theme";
import Cookies from "js-cookie";
import { getToken, getUrlParam, setToken } from "@/utils/util";
NProgress.configure({ showSpinner: false });
router.beforeEach(async (to, from, next) => {
getTheme()
NProgress.start();
document.title = getPageTitle(to.meta.title);
let hasAddDict = store.state.dict.addDict;
let hasUser = store.state.user.hasUser;
let hasAddRoute = store.state.permission.addRoutes;
// cas操作
const token = getToken()
let locationUrl = window.location.origin + window.location.pathname;
function casValidate (ticket) {
axios.get(window._config.services.management + "/management/cas/validate", {
params: {
ticket: ticket,
service: locationUrl,
},
}).then(async (res) => {
if (res.data.status === 1) {
setToken(res.data.content.accessToken)
window.location.href = localStorage.getItem('sjsb-location') + '#' + localStorage.getItem('hash')
} else {
alert(res.data.message)
}
}).catch((e) => {
console.log(e);
});
}
async function permission () {
if (!hasUser) {
store.dispatch("user/getUserInfo");
if (!hasAddDict) {
await store.dispatch("dict/generateDic");
}
}
if (hasAddRoute) {
next();
} else {
//请求菜单
const { result: getMenuData } = (await getMenuInfo(Vue.prototype.BASE_API.CODE)) || [];
const accessRoutes = await store.dispatch(
"permission/generateRoutes",
getMenuData
);
let path = JSON.parse(getMenuData[0].metadata)?.path + JSON.parse(getMenuData[0].children[0].metadata)?.path
router.addRoutes([
...accessRoutes,
{ path: "*", redirect: "/404", hidden: true }
]);
const routeTo = Cookies.get("routerTo");
if (routeTo && routeTo !== "/") {
next({ ...to, replace: true });
} else {
next(path)
}
}
}
if (window._config.casEnable === true) {
if (!token) {
let ticket = getUrlParam("ticket");
if (ticket) {
casValidate(ticket)
} else {
localStorage.setItem("location", window.location.href)
window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl);
}
} else {
permission()
}
} else {
// 使用自定义页面实现单点登录
if (!token) {
let ticket = getUrlParam('ticket');
if (ticket) {
casValidate(ticket)
} else {
if (to.path === '/login') {
if (getUrlParam('_flag') === '1') {
next();
return
} else {
//todo: loginUrl 需要业务系统根据登录页面路由地址获取,这里只是简写
localStorage.setItem('sjsb-location', locationUrl)
localStorage.setItem('hash', to.fullPath)
window.location.href = window._config.services.management + `/management/cas/status?loginUrl=${window._config.baseUrl}/sjsb/&hash=/login&`
return
}
}
localStorage.setItem('sjsb-location', locationUrl)
localStorage.setItem('hash', to.fullPath)
//todo: loginUrl 需要业务系统根据登录页面路由地址获取,这里只是简写
window.location.href = window._config.services.management + `/management/cas/status?loginUrl=${window._config.baseUrl}/sjsb/&hash=/login`
}
} else {
if (to.path === '/login') {
const redirectUrl = getUrlParam('redirectUrl');
if (redirectUrl && redirectUrl !== '') {
window.location.href = redirectUrl
return
} else {
permission()
next('/');
return
}
}
}
}
NProgress.done()
});
router.afterEach((to) => {
// 解决刷新页面报404问题
Cookies.set("routerTo", to.fullPath);
NProgress.done();
})