/** * 全站路由配置 * * 建议: * 1. 代码中路由统一使用name属性跳转(不使用path属性) */ import Vue from 'vue' import Router from 'vue-router' import http from '@/utils/httpRequest' import { isURL } from '@/utils/validate' import { clearLoginInfo } from '@/utils' Vue.use(Router) // 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载 const _import = require('./import-' + process.env.NODE_ENV) // 全局路由(无需嵌套上左右整体布局) const globalRoutes = [ { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } }, { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } } ] // 主入口路由(需嵌套上左右整体布局) const mainRoutes = { path: '/', component: _import('main'), name: 'main', redirect: { name: 'home' }, meta: { title: '主入口整体布局' }, children: [ // 通过meta对象设置路由展示方式 // 1. isTab: 是否通过tab展示内容, true: 是, false: 否 // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否 // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理! { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } }, { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } }, { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } }, { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }, { path: '/book-book', component: _import('modules/book/book'), name: 'book-book', meta: { title: '图书列表', isTab: true } }, { path: '/order-buyorderdetail', component: _import('modules/order/buyorderdetail'), name: 'order-buyorderdetail', meta: { title: '订单详情', isTab: true } }, { path: '/book-bookchapter', component: _import('modules/book/bookchapter'), name: 'book-chapter', meta: { title: '图书章节', isTab: true } }, { path: '/book-bookchaptercontent', component: _import('modules/book/bookchaptercontent'), name: 'book-content', meta: { title: '章节内容', isTab: true } }, { path: '/seckill-seckillprodrelation', component: _import('modules/seckill/seckillprodrelation'), name: 'seckill-seckillprodrelation', meta: { title: '秒杀商品', isTab: true } }, { path: '/buyorder-mergeorder', component: _import('modules/order/buyorder-mergeorder'), name: 'buyorder-mergeorder', meta: { title: '合并发货', isTab: true } }, { path: '/buyorder-print', component: _import('modules/order/buyorder-print'), name: 'buyorder-print', meta: { title: '打印面单', isTab: true } }, { path: '/activity-list', component: _import('modules/activity/activityList'), name: 'activity-list', meta: { title: '活动列表', isTab: true } }, { path: '/user-point-memery', component: _import('modules/user/user-point-memery'), name: 'user-point-memery', meta: { title: '天医币充/扣记录', isTab: true } }, { path: '/user-point-jf', component: _import('modules/user/user-point-jf'), name: 'user-point-jf', meta: { title: '积分充/扣记录', isTab: true } }, { path: '/user-add-course', component: _import('modules/user/user-add-course'), name: 'user-add-course', meta: { title: '开通课程', isTab: true } }, { path: '/order-epay', component: _import('modules/order/order-errorpay'), name: 'order-epay', meta: { title: '充值异常列表', isTab: true } }, { path: '/xieyi_list', component: _import('modules/xieyi/xieyi'), name: 'xieyi_list', meta: { title: '协议管理', isTab: true } }, { path: '/book-comments', component: _import('modules/book/bookComments'), name: 'book-comments', meta: { title: '书评管理', isTab: true } }, { path: '/clock', component: _import('modules/book/clock'), name: 'clock', meta: { title: '打卡管理', isTab: true } }, { path: '/talkBook', component: _import('modules/book/talkBook'), name: 'talkBook', meta: { title: '讲书管理', isTab: true } }, { path: '/yian', component: _import('modules/book/yian'), name: 'yian', meta: { title: '医案管理', isTab: true } }, { path: '/acupointManagement', component: _import('modules/acupoint/acupointManagement'), name: 'acupointManagement', meta: { title: '经穴分类管理', isTab: true } }, { path: '/acupointList', component: _import('modules/acupoint/acupointList'), name: 'acupointList', meta: { title: '经穴文章列表', isTab: true } }, { path: '/acupointDetail', component: _import('modules/acupoint/acupointDetail'), name: 'acupointDetail', meta: { title: '穴位详细', isTab: true } }, { path: '/medicineList', component: _import('modules/medicine/medicineList'), name: 'medicineList', meta: { title: '方药分类管理', isTab: true } }, { path: '/medicineArt', component: _import('modules/medicine/medicineArt'), name: 'medicineArt', meta: { title: '方药文章管理', isTab: true } }, { path: '/countyManagement', component: _import('modules/county/countyManagement'), name: 'countyManagement', meta: { title: '国家区域管理', isTab: true } }, { path: '/commentsList', component: _import('modules/book/commentsList'), name: 'commentsList', meta: { title: '评论管理', isTab: true } }, { path: '/course-courseList', component: _import('modules/course/courseList'), name: 'course-courseList', meta: { title: '课程列表', isTab: true } }, { path: '/course-courseCatalogue', component: _import('modules/course/courseCatalogue'), name: 'course-courseCatalogue', meta: { title: '课程目录列表', isTab: true } }, { path: '/course-courseChapter', component: _import('modules/course/courseChapter'), name: 'course-courseChapter', meta: { title: '课程目录章节列表', isTab: true } }, { path: '/course-courseVideo', component: _import('modules/course/courseVideo'), name: 'course-courseVideo', meta: { title: '课程目录章节视频列表', isTab: true } }, { path: '/course-sociologyList', component: _import('modules/course/sociologyList'), name: 'course-sociologyList', meta: { title: '国学标签列表', isTab: true } }, { path: '/userCourse', component: _import('modules/user/userCourse'), name: 'userCourse', meta: { title: '用户课程列表', isTab: true } }, { path: '/userCertificate', component: _import('modules/user/userCertificate'), name: 'userCertificate', meta: { title: '用户证书列表', isTab: true } }, { path: '/userPoints', component: _import('modules/user/userPoints'), name: 'userPoints', meta: { title: '用户湖分列表', isTab: true } }, { path: '/workOrder', component: _import('modules/workOrderLIst'), name: 'workOrder', meta: { title: '工单列表', isTab: true } }, // { path: '/miniClassList', component: _import('modules/miniClass/miniClassList'), name: 'miniClass', meta: { title: '班级列表', isTab: true } }, // { path: '/miniClassList8', component: _import('modules/miniClass/miniClassList8'), name: 'miniClassList8', meta: { title: 'miniClassList8', isTab: true } }, { path: '/ClassType', component: _import('modules/miniClass/miniClassType'), name: 'ClassType', meta: { title: '班级类型管理', isTab: true } }, { path: '/reLearn', component: _import('modules/order/reLearn'), name: 'reLearn', meta: { title: '复读订单', isTab: true } }, { path: '/training-course-list', component: _import('modules/trainingCourse/training-course-list'), name: 'training-course-list', meta: { title: '培训班管理', isTab: true } }, { path: '/training-course-user', component: _import('modules/trainingCourse/training-course-user'), name: 'training-course-user', meta: { title: '用户列表', isTab: true } }, { path: '/talents-list', component: _import('modules/talents/talents-list'), name: 'talents-list', meta: { title: '太湖英才列表', isTab: true } }, { path: '/course-taihumedList', component: _import('modules/course/taihumedList'), name: 'course-taihumedList', meta: { title: '太湖标签', isTab: true } }, { path: '/courses-list', component: _import('modules/talents/courses-list'), name: 'courses-list', meta: { title: '查看课程', isTab: true } }, { path: '/mergeList', component: _import('modules/certificate/mergeList'), name: 'mergeList', meta: { title: '小班、自考证书', isTab: true } }, { path: '/medicalrecords-medicalList', component: _import('modules/medicalrecords/medicalList'), name: 'medicalrecords-medicalList', meta: { title: '医案列表', isTab: true } }, ], beforeEnter (to, from, next) { let token = Vue.cookie.get('token') if (!token || !/\S/.test(token)) { clearLoginInfo() next({ name: 'login' }) } next() } } const router = new Router({ mode: 'hash', scrollBehavior: () => ({ y: 0 }), isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由 routes: globalRoutes.concat(mainRoutes) }) router.beforeEach((to, from, next) => { // 添加动态(菜单)路由 // 1. 已经添加 or 全局路由, 直接访问 // 2. 获取菜单列表, 添加并保存本地存储 if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') { next() } else { http({ url: http.adornUrl('/sys/menu/nav'), method: 'get', params: http.adornParams() }).then(({data}) => { if (data && data.code === 0) { fnAddDynamicMenuRoutes(data.menuList) router.options.isAddDynamicMenuRoutes = true sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]')) sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]')) next({ ...to, replace: true }) } else { sessionStorage.setItem('menuList', '[]') sessionStorage.setItem('permissions', '[]') next() } }).catch((e) => { console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue') router.push({ name: 'login' }) }) } }) /** * 判断当前路由类型, global: 全局路由, main: 主入口路由 * @param {*} route 当前路由 */ function fnCurrentRouteType (route, globalRoutes = []) { var temp = [] for (var i = 0; i < globalRoutes.length; i++) { if (route.path === globalRoutes[i].path) { return 'global' } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) { temp = temp.concat(globalRoutes[i].children) } } return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main' } /** * 添加动态(菜单)路由 * @param {*} menuList 菜单列表 * @param {*} routes 递归创建的动态(菜单)路由 */ function fnAddDynamicMenuRoutes (menuList = [], routes = []) { var temp = [] for (var i = 0; i < menuList.length; i++) { if (menuList[i].list && menuList[i].list.length >= 1) { temp = temp.concat(menuList[i].list) } else if (menuList[i].url && /\S/.test(menuList[i].url)) { menuList[i].url = menuList[i].url.replace(/^\//, '') var route = { path: menuList[i].url.replace('/', '-'), component: null, name: menuList[i].url.replace('/', '-'), meta: { menuId: menuList[i].menuId, title: menuList[i].name, isDynamic: true, isTab: true, iframeUrl: '' } } // url以http[s]://开头, 通过iframe展示 if (isURL(menuList[i].url)) { route['path'] = `i-${menuList[i].menuId}` route['name'] = `i-${menuList[i].menuId}` route['meta']['iframeUrl'] = menuList[i].url } else { try { route['component'] = _import(`modules/${menuList[i].url}`) || null } catch (e) {} } routes.push(route) } } if (temp.length >= 1) { fnAddDynamicMenuRoutes(temp, routes) } else { mainRoutes.name = 'main-dynamic' mainRoutes.children = routes router.addRoutes([ mainRoutes, { path: '*', redirect: { name: '404' } } ]) sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]')) console.log('\n') console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue') console.log(mainRoutes.children) console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue') } } export default router