permission.js
2.11 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
/*
* @Description:
* @Autor: renchao
* @LastEditTime: 2023-03-24 17:28:37
*/
import Vue from 'vue'
import router from "./router";
import store from "./store";
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";
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;
if (to.path == "/sb" || to.path == "/jg") {
localStorage.removeItem("token");
next();
} else {
let code = Vue.prototype.BASE_API.CODE
//判断token是否存在
const hasToken = localStorage.getItem("token");
if (hasToken) {
//请求用户信息
if (!hasUser) {
store.dispatch("user/getUserInfo");
}
if (hasAddRoute) {
next();
} else {
//请求菜单
const { result: getMenuData } = (await getMenuInfo(code)) || [];
const accessRoutes = await store.dispatch(
"permission/generateRoutes",
getMenuData
);
router.addRoutes([
...accessRoutes,
{ path: "*", redirect: "/404", hidden: true },
]);
const routeTo = Cookies.get("routerTo");
if (!hasAddDict) {
await store.dispatch("dict/generateDic");
}
if (routeTo && routeTo !== "/" && routeTo !== "/sb" && routeTo !== "/jg") {
next({ ...to, replace: true });
} else {
next();
}
window.document.documentElement.setAttribute("data-theme", 'blue');
}
} else {
if (code == 'BDCSBPT') {
next('/sb');
} else {
next('/jg');
}
}
}
NProgress.done();
});
router.afterEach((to) => {
// 解决刷新页面报404问题
Cookies.set("routerTo", to.fullPath);
NProgress.done();
});