index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * 全站路由配置
  3. *
  4. * 建议:
  5. * 1. 代码中路由统一使用name属性跳转(不使用path属性)
  6. */
  7. import Vue from 'vue'
  8. import Router from 'vue-router'
  9. import http from '@/utils/httpRequest'
  10. import { isURL } from '@/utils/validate'
  11. import { clearLoginInfo } from '@/utils'
  12. Vue.use(Router)
  13. // 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
  14. const _import = require('./import-' + process.env.NODE_ENV)
  15. // 全局路由(无需嵌套上左右整体布局)
  16. const globalRoutes = [
  17. { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  18. { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } }
  19. ]
  20. // 主入口路由(需嵌套上左右整体布局)
  21. const mainRoutes = {
  22. path: '/',
  23. component: _import('main'),
  24. name: 'main',
  25. redirect: { name: 'home' },
  26. meta: { title: '主入口整体布局' },
  27. children: [
  28. // 通过meta对象设置路由展示方式
  29. // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
  30. // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
  31. // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
  32. { path: '/home', component: _import('common/dashboard'), name: 'home', meta: { title: '首页', isHome: true } },
  33. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
  34. { path: '/msg-center', component: _import('modules/msg-center/notice'), name: 'msgCenter', meta: { title: '我的消息中心' } },
  35. { path: '/msg-announcement', component: _import('modules/msg-center/announcement'), name: 'msgAnnouncement', meta: { title: '公告消息' } },
  36. { path: '/msg-approve', component: _import('modules/msg-center/approve'), name: 'msgApprove', meta: { title: '审批消息' } },
  37. { path: '/msg-work', component: _import('modules/works/work'), name: 'msgWork', meta: { title: '我的工作台' } }
  38. // { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } },
  39. // { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }
  40. ],
  41. beforeEnter (to, from, next) {
  42. let token = Vue.cookie.get('token')
  43. if (!token || !/\S/.test(token)) {
  44. clearLoginInfo()
  45. next({ name: 'login' })
  46. }
  47. next()
  48. }
  49. }
  50. const router = new Router({
  51. mode: 'history', // 'hash',
  52. scrollBehavior: () => ({ y: 0 }),
  53. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  54. routes: globalRoutes.concat(mainRoutes)
  55. })
  56. router.beforeEach((to, from, next) => {
  57. // 添加动态(菜单)路由
  58. // 1. 已经添加 or 全局路由, 直接访问
  59. // 2. 获取菜单列表, 添加并保存本地存储
  60. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  61. next()
  62. } else {
  63. http({
  64. url: http.adornUrl('/user-service/menu/nav'), // user-service
  65. method: 'get',
  66. params: http.adornParams()
  67. }).then(({data}) => {
  68. if (data && data.code === '200') {
  69. fnAddDynamicMenuRoutes(data.data.menuList)
  70. router.options.isAddDynamicMenuRoutes = true
  71. sessionStorage.setItem('menuList', JSON.stringify(data.data.menuList || '[]'))
  72. sessionStorage.setItem('permissions', JSON.stringify(data.data.permissions || '[]'))
  73. next({ ...to, replace: true })
  74. } else {
  75. sessionStorage.setItem('menuList', '[]')
  76. sessionStorage.setItem('permissions', '[]')
  77. next()
  78. }
  79. }).catch((e) => {
  80. console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  81. router.push({ name: 'login' })
  82. })
  83. }
  84. })
  85. /**
  86. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  87. * @param {*} route 当前路由
  88. */
  89. function fnCurrentRouteType (route, globalRoutes = []) {
  90. var temp = []
  91. for (var i = 0; i < globalRoutes.length; i++) {
  92. if (route.path === globalRoutes[i].path) {
  93. return 'global'
  94. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  95. temp = temp.concat(globalRoutes[i].children)
  96. }
  97. }
  98. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  99. }
  100. /**
  101. * 添加动态(菜单)路由
  102. * @param {*} menuList 菜单列表
  103. * @param {*} routes 递归创建的动态(菜单)路由
  104. */
  105. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  106. var temp = []
  107. for (var i = 0; i < menuList.length; i++) {
  108. if (menuList[i].list && menuList[i].list.length >= 1) {
  109. temp = temp.concat(menuList[i].list)
  110. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  111. menuList[i].url = menuList[i].url.replace(/^\//, '')
  112. var route = {
  113. path: menuList[i].url.replace('/', '-'),
  114. component: null,
  115. name: menuList[i].url.replace('/', '-'),
  116. meta: {
  117. menuId: menuList[i].menuId,
  118. title: menuList[i].name,
  119. isDynamic: true,
  120. isTab: true,
  121. iframeUrl: ''
  122. }
  123. }
  124. // url以http[s]://开头, 通过iframe展示
  125. if (isURL(menuList[i].url)) {
  126. route['path'] = `i-${menuList[i].menuId}`
  127. route['name'] = `i-${menuList[i].menuId}`
  128. route['meta']['iframeUrl'] = menuList[i].url
  129. } else {
  130. try {
  131. route['component'] = _import(`modules/${menuList[i].url}`) || null
  132. } catch (e) {}
  133. }
  134. routes.push(route)
  135. }
  136. }
  137. if (temp.length >= 1) {
  138. fnAddDynamicMenuRoutes(temp, routes)
  139. } else {
  140. mainRoutes.name = 'main-dynamic'
  141. mainRoutes.children = routes
  142. router.addRoutes([
  143. mainRoutes,
  144. { path: '*', redirect: { name: '404' } }
  145. ])
  146. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  147. // console.log('\n')
  148. // console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue')
  149. // console.log(mainRoutes.children)
  150. // console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue')
  151. }
  152. }
  153. export default router