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.
139 lines
3.3 KiB
139 lines
3.3 KiB
2 years ago
|
import router from "./router";
|
||
|
import store from "./store";
|
||
|
import NProgress from "nprogress"; // progress bar
|
||
|
import "nprogress/nprogress.css"; // progress bar style
|
||
|
import getPageTitle from "@/utils/get-page-title";
|
||
|
|
||
|
import Layout from "@/layout";
|
||
|
|
||
|
import serve from "api/login.js";
|
||
|
|
||
|
NProgress.configure({ showSpinner: false }); // NProgress Configuration
|
||
|
|
||
|
const whiteList = ["/login"]; // no redirect whitelist
|
||
|
|
||
|
const asyncRoutes = [
|
||
|
{
|
||
|
component: "Layout",
|
||
|
hidden: false,
|
||
|
menuType: "C",
|
||
|
meta: { title: "", icon: "" },
|
||
|
path: "/",
|
||
|
children: [
|
||
|
{
|
||
|
component: "refineryInfo/index",
|
||
|
hidden: false,
|
||
|
menuType: "C",
|
||
|
meta: { title: "炼厂信息", icon: "iconfenpei3" },
|
||
|
name: "refineryInfo",
|
||
|
path: "refineryInfo",
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
{
|
||
|
component: "Layout",
|
||
|
hidden: false,
|
||
|
menuType: "C",
|
||
|
meta: { title: "", icon: "" },
|
||
|
path: "/",
|
||
|
children: [
|
||
|
{
|
||
|
component: "product/index",
|
||
|
hidden: false,
|
||
|
menuType: "C",
|
||
|
meta: { title: "产品列表", icon: "iconfenpei3" },
|
||
|
name: "Product",
|
||
|
path: "product",
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
{
|
||
|
component: "Layout",
|
||
|
hidden: false,
|
||
|
menuType: "C",
|
||
|
meta: { title: "", icon: "" },
|
||
|
path: "/",
|
||
|
children: [
|
||
|
{
|
||
|
component: "order/index",
|
||
|
hidden: false,
|
||
|
menuType: "C",
|
||
|
meta: { title: "订单管理", icon: "iconfenpei3" },
|
||
|
name: "Order",
|
||
|
path: "order",
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
];
|
||
|
|
||
|
router.beforeEach(async (to, from, next) => {
|
||
|
NProgress.start();
|
||
|
document.title = getPageTitle(to.meta.title);
|
||
|
|
||
|
const hasToken = localStorage.getItem("token");
|
||
|
// const hasToken = 1;
|
||
|
if (hasToken) {
|
||
|
if (to.path === "/login") {
|
||
|
next({ path: "/" });
|
||
|
NProgress.done();
|
||
|
} else {
|
||
|
const hasAuth = store.getters.auth && store.getters.auth.length;
|
||
|
if (hasAuth) {
|
||
|
next();
|
||
|
} else {
|
||
|
try {
|
||
|
// let infoRes = await serve.getUserInfo();
|
||
|
// infoRes.data.authList = [1];
|
||
|
let infoRes = {
|
||
|
data: {
|
||
|
authList: [1],
|
||
|
},
|
||
|
};
|
||
|
store.dispatch("user/info", infoRes.data);
|
||
|
|
||
|
// let routerRes = await serve.getRouter();
|
||
|
let routerRes = { data: asyncRoutes };
|
||
|
let realRouter = filterAsyncRouter(routerRes.data);
|
||
|
store.dispatch("permission/generateRoutes", realRouter);
|
||
|
router.addRoutes(realRouter);
|
||
|
next({ ...to, replace: true });
|
||
|
} catch (err) {
|
||
|
console.log("catch");
|
||
|
next("/login");
|
||
|
NProgress.done();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
if (whiteList.includes(to.path)) {
|
||
|
next();
|
||
|
} else {
|
||
|
next("/login");
|
||
|
NProgress.done();
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
router.afterEach(() => {
|
||
|
NProgress.done();
|
||
|
});
|
||
|
|
||
|
function filterAsyncRouter(routers) {
|
||
|
return routers.map((route) => {
|
||
|
if (route.component) {
|
||
|
if (route.component === "Layout") {
|
||
|
route.component = Layout;
|
||
|
} else {
|
||
|
route.component = lazyLoad(route.component);
|
||
|
}
|
||
|
}
|
||
|
if (route.children && route.children.length) {
|
||
|
route.children = filterAsyncRouter(route.children);
|
||
|
}
|
||
|
return route;
|
||
|
});
|
||
|
}
|
||
|
function lazyLoad(path) {
|
||
|
return (resolve) => require([`@/views/${path}`], resolve);
|
||
|
}
|