import auth from "@/plugins/auth"; import router, { constantRoutes, dynamicRoutes } from "@/router"; import { getRouters } from "@/api/menu"; import Layout from "@/layout/index"; import ParentView from "@/components/ParentView"; import InnerLink from "@/layout/components/InnerLink"; const permission = { state: { routes: [], addRoutes: [], defaultRoutes: [], topbarRouters: [], sidebarRouters: [], }, mutations: { SET_ROUTES: (state, routes) => { state.addRoutes = routes; state.routes = constantRoutes.concat(routes); }, SET_DEFAULT_ROUTES: (state, routes) => { state.defaultRoutes = constantRoutes.concat(routes); }, SET_TOPBAR_ROUTES: (state, routes) => { state.topbarRouters = routes; }, SET_SIDEBAR_ROUTERS: (state, routes) => { state.sidebarRouters = routes; }, }, actions: { // 生成路由 GenerateRoutes({ commit }) { return new Promise((resolve) => { // 向后端请求路由数据 let data = [ { alwaysShow: true, component: "Layout", meta: { icon: "build", link: null, noCache: false, title: "销售管理", }, hidden: false, name: "Sales", path: "/sales", redirect: "noRedirect", children: [ { meta: { icon: "build", link: null, noCache: false, title: "销售出库单", }, hidden: false, name: "SalesOUt", path: "salesOUt", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "销售出库单维护", }, component: () => import("@/views/sell/sellOut/sellOutBill.vue"), hidden: false, name: "SellOutBill", path: "sellOutBill", }, { meta: { icon: "build", link: null, noCache: false, title: "销售出库单", }, component: () => import("@/views/sell/sellOut/sellOutBillEdit.vue"), hidden: false, name: "SellOutBillEdit", path: "sellOutBillEdit", }, ], }, { meta: { icon: "build", link: null, noCache: false, title: "销售订单", }, hidden: false, name: "SeOrder", path: "seOrder", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "销售订单维护", }, component: () => import("@/views/sell/xsSeOrderBill/xsSeOrderBill.vue"), hidden: false, name: "XsSeOrderBill", path: "xsSeOrderBill", }, ], }, { meta: { icon: "build", link: null, noCache: false, title: "发货通知单", }, hidden: false, name: "SeOutStock", path: "seOutStock", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "发货通知单维护", }, component: () => import("@/views/sell/XsSeOutStockBill.vue"), hidden: false, name: "XsSeOutStockBill", path: "xsSeOutStockBill", }, ], }, { meta: { icon: "build", link: null, noCache: false, title: "退货通知单", }, hidden: false, name: "SeOutStockBack", path: "seOutStockBack", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "退货通知单维护", }, component: () => import("@/views/sell/XsSeOutStockBackBillList.vue"), hidden: false, name: "XsSeOutStockBackBillList", path: "xsSeOutStockBackBillList", }, ], }, ], }, { alwaysShow: true, component: "Layout", meta: { icon: "build", link: null, noCache: false, title: "基础建模", }, hidden: false, name: "Basic", path: "/basic", redirect: "noRedirect", children: [ { meta: { icon: "build", link: null, noCache: false, title: "生产资料", }, hidden: false, name: "Gy", path: "gy", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "生产资料列表", }, component: () => import("@/views/basic/gySource/index.vue"), hidden: false, name: "GySource", path: "gySource", }, ], }, { meta: { icon: "build", link: null, noCache: false, title: "公用资料", }, hidden: false, name: "Gy", path: "gy", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "仓库列表", }, component: () => import("@/views/basic/gyWarehouse/gyWarehouse.vue"), hidden: false, name: "GyWarehouse", path: "gyWarehouse", }, { meta: { icon: "build", link: null, noCache: false, title: "仓位列表", }, component: () => import("@/views/basic/gyStockPlace/gyStockPlace.vue"), hidden: false, name: "GyStockPlace", path: "gyStockPlace", }, { meta: { icon: "build", link: null, noCache: false, title: "职员列表", }, component: () => import("@/views/basic/gyEmployee/gyEmployee.vue"), hidden: false, name: "GyEmployee", path: "gyEmployee", }, ], }, ], }, { alwaysShow: true, component: "Layout", meta: { icon: "build", link: null, noCache: false, title: "采购管理", }, hidden: false, name: "Purchase", path: "/purchase", redirect: "noRedirect", children: [ { meta: { icon: "build", link: null, noCache: false, title: "采购订单", }, hidden: false, name: "PoOrder", path: "poOrder", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "采购订单列表", }, component: () => import("@/views/purchase/cgPoOrderBill/cgPoOrderBill"), hidden: false, name: "CgPoOrderBill", path: "cgPoOrderBill", }, ], }, { meta: { icon: "build", link: null, noCache: false, title: "收料通知单", }, hidden: false, name: "PoInStock", path: "PoInStock", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "收料通知单列表", }, component: () => import( "@/views/purchase/cgPoInStockBill/cgPoInStockBill.vue" ), hidden: false, name: "CgPoInStockBill", path: "cgPoInStockBill", }, ], }, { alwaysShow: true, component: "Layout", meta: { icon: "build", link: null, noCache: false, title: "系统管理", }, hidden: false, name: "System", path: "/system", redirect: "noRedirect", children: [ { meta: { icon: "build", link: null, noCache: false, title: "用户管理", }, hidden: false, name: "User", path: "user", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "用户列表", }, component: () => import("@/views/system/user/xtUserList.vue"), hidden: false, name: "XtUserList", path: "xtUserList", }, ], }, ], }, ], }, { alwaysShow: true, component: "Layout", meta: { icon: "build", link: null, noCache: false, title: "生产管理", }, hidden: false, name: "ICMO", path: "/icmo", redirect: "noRedirect", children: [ { meta: { icon: "build", link: null, noCache: false, title: "生产订单", }, hidden: false, name: "ScIcmoBill", path: "scIcmoBill", redirect: "noRedirect", component: "Layout", children: [ { meta: { icon: "build", link: null, noCache: false, title: "生产订单列表", }, component: () => import("@/views/ICMO/ScICMOBillList.vue"), hidden: false, name: "ScICMOBillList", path: "scICMOBillList", }, ], }, ], }, ]; const sdata = JSON.parse(JSON.stringify(data)); const rdata = JSON.parse(JSON.stringify(data)); const sidebarRoutes = filterAsyncRouter(sdata); const rewriteRoutes = filterAsyncRouter(rdata, false, true); const asyncRoutes = filterDynamicRoutes(dynamicRoutes); rewriteRoutes.push({ path: "*", redirect: "/404", hidden: true }); router.addRoutes(asyncRoutes); commit("SET_ROUTES", rewriteRoutes); commit("SET_SIDEBAR_ROUTERS", constantRoutes.concat(sidebarRoutes)); commit("SET_DEFAULT_ROUTES", sidebarRoutes); commit("SET_TOPBAR_ROUTES", sidebarRoutes); resolve(rewriteRoutes); }); }, }, }; // 遍历后台传来的路由字符串,转换为组件对象 function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) { return asyncRouterMap.filter((route) => { if (type && route.children) { route.children = filterChildren(route.children); } if (route.component) { // Layout ParentView 组件特殊处理 if (route.component === "Layout") { route.component = Layout; } else if (route.component === "ParentView") { route.component = ParentView; } else if (route.component === "InnerLink") { route.component = InnerLink; } else { route.component = loadView(route.component); } } if (route.children != null && route.children && route.children.length) { route.children = filterAsyncRouter(route.children, route, type); } else { delete route["children"]; delete route["redirect"]; } return true; }); } function filterChildren(childrenMap, lastRouter = false) { var children = []; childrenMap.forEach((el) => { el.path = lastRouter ? lastRouter.path + "/" + el.path : el.path; if (el.children && el.children.length && el.component === "ParentView") { children = children.concat(filterChildren(el.children, el)); } else { children.push(el); } }); return children; } // 动态路由遍历,验证是否具备权限 export function filterDynamicRoutes(routes) { const res = []; routes.forEach((route) => { if (route.permissions) { if (auth.hasPermiOr(route.permissions)) { res.push(route); } } else if (route.roles) { if (auth.hasRoleOr(route.roles)) { res.push(route); } } }); return res; } export const loadView = (view) => { if (process.env.NODE_ENV === "development") { return (resolve) => require([`@/views/${view}`], resolve); } else { // 使用 import 实现生产环境的路由懒加载 return () => import(`@/views/${view}`); } }; export default permission;