import Vue from 'vue' import Router from 'vue-router' import page from '@/config/page' import store from '@/vendors/vuex' import { renewalHead } from '@/model/indexRx' Vue.use(Router) // 组件实验室 // const _Laboratory = () => import('@/components/base/_laboratory') /* Layout */ import Layout from '../layout/index' /** * constantRoutes * 没有权限要求的基本页 * 所有角色都可以访问 * */ export const constantRoutes = [ { path: '/about', component: () => import('@/views/info/about'), hidden: true }, { path: '/privacyAgreement', component: () => import('@/views/info/privacyAgreement'), hidden: true }, { path: '/userAgreement', component: () => import('@/views/info/userAgreement'), hidden: true }, { path: '/login', component: () => import(/* webpackChunkName: "login" */ '@/views/login/index'), hidden: true }, { path: '/ship_login', component: () => import(/* webpackChunkName: "logindship" */ '@/views/login/index_ship'), hidden: true }, { path: '/404', component: () => page('404'), hidden: true }, { path: '/401', component: () => import(/* webpackChunkName: "401" */ '@/views/errorPage/401'), hidden: true }, { path: '/help', component: () => import('@/views/helpCenter/index'), hidden: true }, { path: '/', component: Layout, redirect: { name: 'home' }, meta: { title: 'home', icon: '-index-copy' }, children: [ { path: 'home', component: () => page('home'), name: 'home', meta: { title: 'home', icon: '-index-copy', affix: true, auth: 'homePage' }, hidden: true } ] } ] // process.env.NODE_ENV === 'development' && // constantRoutes.push({ path: '/lab', name: '_Laboratory', component: _Laboratory }) let cofigRouter = [] const modulesFiles = require.context('../views', true, /.js$/) modulesFiles.keys().forEach((model_item, key) => { if (model_item === 'index.js' || modulesFiles(model_item).default === undefined || modulesFiles(model_item).default.path === undefined) return cofigRouter = cofigRouter.concat(modulesFiles(model_item).default) }) // 需要根据用户角色动态加载的路由 export const asyncRoutes = cofigRouter const createRouter = () => new Router({ // mode: 'history', scrollBehavior: () => ({ y: 0 }), linkActiveClass: 'active', // router-link .active样式 routes: constantRoutes }) const router = createRouter() export function resetRouter() { const newRouter = createRouter() router.matcher = newRouter.matcher // reset router } router.beforeEach(async (to, from, next) => { // const { roles, isTrainDialog, guideInfo } = store.getters const { roles } = store.getters if (roles && roles.length && to.path !== '/help') { // if (Object.keys(guideInfo).length === 0) { // store.dispatch('user/getTrainInfo') // } // if (isTrainDialog.length === 0) { // store.dispatch('user/getNewLogin') // } if ( to.matched[1] && to.matched[1].parent && to.matched[1].parent.meta && to.matched[1].parent.meta.title ) { sessionStorage.setItem( 'ws-pf_moduleName', to.matched[1].parent.meta.title ) } else { sessionStorage.setItem('ws-pf_moduleName', to.meta.title) } if (to.meta.module || to.meta.auth) { sessionStorage.setItem('ws-pf_authority', to.meta.auth || to.meta.module) } } sessionStorage.setItem('ws-pf_menuName', to.meta.title) const { baseInfo } = store.getters if (Object.keys(baseInfo).length === 0) { store.dispatch('user/getBaseInfo') } let data = { title: '', routingUri: '' } if (to.name && to.meta.shortcutEntrance) { if (to.meta.shortcutEntrance.indexOf(',') == -1) { if (to.name == 'knowledgeList' || to.name == 'dataManageMentList') { data.title = to.meta.shortcutEntrance } else { data.title = to.meta.shortcutEntrance + ',' + to.meta.title } if (to.path.split('/').length > 3 && to.path.split('/')[1] != 'crew') { data.routingUri = to.path .split('/') .slice(0, -1) .join('/') } else { data.routingUri = to.path } } else { data.title = to.meta.shortcutEntrance if ( to.name != 'certificateList' && to.name != 'impaMa' && to.name != 'applyMa' && to.name != 'shipMa' && to.name != 'theLibraryMa' && to.name != 'inventoryManagementMa' ) { data.routingUri = to.path.split('/').length > 2 ? to.path .split('/') .slice(0, -1) .join('/') : to.path } else { data.routingUri = to.path } } renewalHead(data).toPromise().then(() => { next() }) } else { next() } }) export default router