import Vue from "vue"
|
import Router from "vue-router"
|
// import type from "@/router/deployCode"
|
|
import productRouter from "./product/index.js" // 产品
|
import operateRouter from "./operate/index.js" // 操作
|
import warehouseManageRouter from "./warehouseManage/index.js" // 仓库管理
|
import reportRouter from "./report/index.js" // 报表
|
import {getMenuTreeByRole} from "@/api/menus/index"
|
import store from '@/store/index.js';
|
|
Vue.use(Router)
|
const login = (resolve) => require(["@/views/other/login/index"], resolve)
|
const overview = (resolve) => require(["@/views/overview/index"], resolve) // 概述
|
const productManage = (resolve) => require(["@/views/productManage/index"], resolve) // 产品
|
const operate = (resolve) => require(["@/views/operate/index"], resolve) // 操作
|
const warehouseManage = (resolve) => require(["@/views/warehouseManage/index"], resolve) // 仓库管理
|
const reportForm = (resolve) => require(["@/views/reportForm/index"], resolve) // 报表
|
const noData = (resolve) => require(["@/views/NoData/index"], resolve)
|
|
export const routes = [
|
// 无权限数据页面
|
{
|
path: "noData",
|
name: "noData",
|
meta: {
|
title: "",
|
auth: true,
|
},
|
component: noData,
|
},
|
{
|
path: "overview", // 概述
|
name: "overview",
|
component: overview,
|
meta: {
|
title: "概述",
|
isAllways: true
|
}
|
},
|
{
|
path: "productManage", // 产品
|
name: "productManage",
|
component: productManage,
|
children: productRouter,
|
meta: {
|
title: "产品",
|
isAllways: true
|
}
|
},
|
{
|
path: "operate", // 操作
|
name: "operate",
|
component: operate,
|
children: operateRouter,
|
meta: {
|
title: "操作",
|
isAllways: true
|
}
|
},
|
{
|
path: "warehouseManage", // 仓库管理
|
name: "warehouseManage",
|
component: warehouseManage,
|
children: warehouseManageRouter,
|
meta: {
|
title: "仓库管理",
|
isAllways: true
|
}
|
},
|
{
|
path: "reportForm", // 报表
|
name: "reportForm",
|
component: reportForm,
|
children: reportRouter,
|
meta: {
|
title: "报表",
|
isAllways: true
|
}
|
}
|
]
|
export const constantRoutes = [
|
{
|
path: "/",
|
component: () => import("@/components/layout/index"),
|
name: "Index",
|
meta: {
|
title: "概述",
|
isAllways: true,
|
insIndex: true
|
},
|
redirect: {
|
name: "overview"
|
},
|
children: routes
|
},
|
{
|
path: "/login",
|
component: login,
|
meta: {
|
isLogin: true,
|
title: "登录"
|
}
|
},
|
{
|
path: "*",
|
component: () => import("@/views/other/error/404"),
|
meta: {
|
title: "404"
|
}
|
}
|
]
|
// 导出路由 在 main.js 里使用
|
const createRouter = () =>
|
new Router({
|
mode: "history",
|
// base: window.getServerJson.context,
|
scrollBehavior: () => ({ y: 0 }),
|
routes: constantRoutes
|
})
|
|
const router = createRouter()
|
|
let isSkip = false;
|
async function hasPermission(routePath) {
|
isSkip = false;
|
try {
|
const res = await getMenuTreeByRole();
|
const newPath = {
|
path: "/noData"
|
};
|
const foundObject = res.data.list.find(obj => obj.systemType === 3);
|
if (foundObject) {
|
// 存储进vuex
|
store.commit('setMenus', foundObject.menus);
|
foundObject.menus.forEach(item => {
|
const nextPath = item.children.find(obj => obj.path === routePath);
|
if (nextPath) {
|
newPath.path = nextPath.path;
|
isSkip = true;
|
}
|
});
|
} else {
|
newPath.path = '/noData';
|
}
|
return newPath;
|
} catch (error) {
|
return { path: "/noData" };
|
}
|
}
|
router.beforeEach(async (to, from, next) => {
|
try {
|
const result = await hasPermission(to.path);
|
console.log(result,"result")
|
next();
|
if (!isSkip) {
|
if(to.path==="/overview/overviewList"||to.path==="/overview/previewExcel"||to.path==="/operate/inventoryAdjustmentHistory"){
|
next();
|
}else{
|
next('/noData')
|
}
|
}
|
} catch (error) {
|
console.error('Error in navigation guard:', error);
|
next();
|
}
|
});
|
|
|
router.afterEach((to, from, next) => {
|
if ((to.path === "/overview/overviewList" || to.path === "/productManage/productList") && to.params.name) {
|
console.log(from, next)
|
to.meta.title = to.params.name
|
// document.title = to.meta.title
|
} else if (to.name === "inboundOutboundDetail") {
|
to.meta.title = "入库明细报表"
|
// document.title = to.meta.title
|
}
|
})
|
// router.beforeEach((to, from, next) => {
|
// must call `next`
|
// console.log(to, from)
|
// if (to.path === "/custom/salesLead") {
|
// next()
|
// } else {
|
// if (to.meta.requireAuth) {
|
// next({
|
// path: "/login",
|
// query: { redirect: to.fullPath }
|
// })
|
// } else {
|
// next()
|
// }
|
// }
|
// })
|
|
export default router
|