import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Home.vue' import { getCurrentInfo } from '../service/getData' Vue.use(VueRouter); const staticRouter = [ { path: '/RelieveAdd', name: 'RelieveAdd', component:() => import(/* webpackChunkName: "DocApprovalHistory" */ '../views/report/components/RelieveAdd.vue'), }, { path: 'objectDetail', name: 'ObjectDetail', component: () => import(/* webpackChunkName: "DocApprovalHistory" */ '../views/report/components/ObjectDetail.vue'), }, { path: 'docDetail', name: 'DocDetail', component: () => import(/* webpackChunkName: "DocApprovalHistory" */ '../views/report/components/DocDetail.vue'), } ] const authRouters = [ { key: 'CON_REPORT_DOC', path: '/ControlReportList', name: 'ControlReportList', component: () => import(/* webpackChunkName: "ControlReportList" */ '../views/controlReport/ControlReportList.vue') }, { key: 'CON_REPORT_DOC', path: '/docApproval', name: 'DocApproval', component: () => import(/* webpackChunkName: "DocApproval" */ '../views/controlReport/DocApproval.vue') }, { key: 'CON_REPORT_DOC', path: '/DocApprovalHistory', name: 'DocApprovalHistory', component: () => import(/* webpackChunkName: "DocApprovalHistory" */ '../views/controlReport/DocApprovalHistory.vue') }, { key: 'BIZ_REPORT_DOC', path: '/ReportList', name: 'ReportList', component: () => import(/* webpackChunkName: "ReportList" */ '../views/report/ReportList.vue') }, { key: 'BIZ_REPORT_DOC', path: '/reportDocApproval', name: 'reportDocApproval', component: () => import(/* webpackChunkName: "DocApproval" */ '../views/report/DocApproval.vue') }, { key: 'BIZ_REPORT_DOC', path: '/reportDocApprovalHistory', name: 'reportDocApprovalHistory', component: () => import(/* webpackChunkName: "DocApprovalHistory" */ '../views/report/DocApprovalHistory.vue') }, { key: 'REPORT_CENTRE_DICT', path: '/DictList', name: 'DictList', component: () => import(/* webpackChunkName: "DocApprovalHistory" */ '../views/dict/DictList.vue'), } ] const routes = [ { path: '/', // redirect: accessRouters.length && accessRouters[0].path ? accessRouters[0].path : '/noData', redirect: '/ControlReportList', component: Home, children: staticRouter }, { path: '/about', name: 'About', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () => import(/* webpackChunkName: "about" */ '../views/About.vue') }, { path: '/noData', name: 'noData', component: () => import(/* webpackChunkName: "about" */ '@/components/error/noData.vue') }, { path: '*', component: resolve => { require(['@/components/error/404.vue'], resolve) } } ] const router = new VueRouter({ routes, mode: 'history' }) getCurrentInfo().then(res => { if(res.statusCode == 200) { let auth = res.data.user.ocodeList; let accessRouters = authRouters.filter(item => { return auth.includes(item.key); }) || [] let routers = [ { path: '/', component: Home, children: accessRouters } ]; // 重点这段代码 router.addRoutes(routers); if (window.location.hash === '#/' || window.location.hash === '') { if (accessRouters[0] && accessRouters.length) { router.push(accessRouters[0]) } else { router.push('/noData') } } } }) export default router