You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.1 KiB
79 lines
2.1 KiB
import router from "./router";
|
|
import store from "./store";
|
|
import { Message } from "element-ui";
|
|
import NProgress from "nprogress";
|
|
import "nprogress/nprogress.css";
|
|
import { getToken } from "@/utils/auth";
|
|
import { isPathMatch } from "@/utils/validate";
|
|
import { isRelogin } from "@/utils/request";
|
|
|
|
NProgress.configure({ showSpinner: false });
|
|
|
|
const whiteList = ["/login", "/register"];
|
|
|
|
const isWhiteList = (path) => {
|
|
return whiteList.some((pattern) => isPathMatch(pattern, path));
|
|
};
|
|
|
|
const hasHomeRoute = (routes) => {
|
|
return Array.isArray(routes) && routes.some((route) => route && route.path === "/" && route.redirect === "/index");
|
|
};
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
NProgress.start();
|
|
if (getToken()) {
|
|
if (to.meta.title) {
|
|
store.dispatch("settings/setTitle", to.meta.title);
|
|
}
|
|
if (to.path === "/login") {
|
|
next({ path: "/" });
|
|
NProgress.done();
|
|
return;
|
|
}
|
|
if (isWhiteList(to.path)) {
|
|
next();
|
|
return;
|
|
}
|
|
if (store.getters.roles.length === 0) {
|
|
isRelogin.show = true;
|
|
store.dispatch("GetInfo").then(() => {
|
|
isRelogin.show = false;
|
|
store.dispatch("GenerateRoutes").then((accessRoutes) => {
|
|
router.addRoutes(accessRoutes);
|
|
if (!hasHomeRoute(accessRoutes) && to.path !== "/no-permission") {
|
|
next({ path: "/no-permission", replace: true });
|
|
return;
|
|
}
|
|
next({ ...to, replace: true });
|
|
});
|
|
}).catch((err) => {
|
|
const errorText = (err && err.message) || String(err || "GetInfo failed");
|
|
store
|
|
.dispatch("LogOut")
|
|
.catch(() => {})
|
|
.finally(() => {
|
|
Message.error(errorText);
|
|
next({ path: "/login", replace: true });
|
|
});
|
|
});
|
|
return;
|
|
}
|
|
next();
|
|
return;
|
|
}
|
|
|
|
if (isWhiteList(to.path)) {
|
|
next();
|
|
} else {
|
|
if (to.path === "/no-permission") {
|
|
next("/login");
|
|
} else {
|
|
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
|
}
|
|
NProgress.done();
|
|
}
|
|
});
|
|
|
|
router.afterEach(() => {
|
|
NProgress.done();
|
|
});
|
|
|