index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/home'), name: 'home', meta: { title: '首页' } },
  33. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } }
  34. // { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } },
  35. // { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }
  36. ],
  37. beforeEnter (to, from, next) {
  38. let token = Vue.cookie.get('token')
  39. if (!token || !/\S/.test(token)) {
  40. clearLoginInfo()
  41. next({ name: 'login' })
  42. }
  43. next()
  44. }
  45. }
  46. const router = new Router({
  47. mode: 'history', // 'hash',
  48. scrollBehavior: () => ({ y: 0 }),
  49. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  50. routes: globalRoutes.concat(mainRoutes)
  51. })
  52. router.beforeEach((to, from, next) => {
  53. // 添加动态(菜单)路由
  54. // 1. 已经添加 or 全局路由, 直接访问
  55. // 2. 获取菜单列表, 添加并保存本地存储
  56. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  57. next()
  58. } else {
  59. http({
  60. url: http.adornUrl('/user-service/menu/nav'), // user-service
  61. method: 'get',
  62. params: http.adornParams()
  63. }).then(({data}) => {
  64. if (data && data.code === '200') {
  65. fnAddDynamicMenuRoutes(data.data.menuList)
  66. router.options.isAddDynamicMenuRoutes = true
  67. sessionStorage.setItem('menuList', JSON.stringify(data.data.menuList || '[]'))
  68. sessionStorage.setItem('permissions', JSON.stringify(data.data.permissions || '[]'))
  69. next({ ...to, replace: true })
  70. } else {
  71. sessionStorage.setItem('menuList', '[]')
  72. sessionStorage.setItem('permissions', '[]')
  73. next()
  74. }
  75. }).catch((e) => {
  76. console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  77. router.push({ name: 'login' })
  78. })
  79. }
  80. })
  81. /**
  82. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  83. * @param {*} route 当前路由
  84. */
  85. function fnCurrentRouteType (route, globalRoutes = []) {
  86. var temp = []
  87. for (var i = 0; i < globalRoutes.length; i++) {
  88. if (route.path === globalRoutes[i].path) {
  89. return 'global'
  90. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  91. temp = temp.concat(globalRoutes[i].children)
  92. }
  93. }
  94. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  95. }
  96. /**
  97. * 添加动态(菜单)路由
  98. * @param {*} menuList 菜单列表
  99. * @param {*} routes 递归创建的动态(菜单)路由
  100. */
  101. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  102. var temp = []
  103. for (var i = 0; i < menuList.length; i++) {
  104. if (menuList[i].list && menuList[i].list.length >= 1) {
  105. temp = temp.concat(menuList[i].list)
  106. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  107. menuList[i].url = menuList[i].url.replace(/^\//, '')
  108. var route = {
  109. path: menuList[i].url.replace('/', '-'),
  110. component: null,
  111. name: menuList[i].url.replace('/', '-'),
  112. meta: {
  113. menuId: menuList[i].menuId,
  114. title: menuList[i].name,
  115. isDynamic: true,
  116. isTab: true,
  117. iframeUrl: ''
  118. }
  119. }
  120. // url以http[s]://开头, 通过iframe展示
  121. if (isURL(menuList[i].url)) {
  122. route['path'] = `i-${menuList[i].menuId}`
  123. route['name'] = `i-${menuList[i].menuId}`
  124. route['meta']['iframeUrl'] = menuList[i].url
  125. } else {
  126. try {
  127. route['component'] = _import(`modules/${menuList[i].url}`) || null
  128. } catch (e) {}
  129. }
  130. routes.push(route)
  131. }
  132. }
  133. if (temp.length >= 1) {
  134. fnAddDynamicMenuRoutes(temp, routes)
  135. } else {
  136. mainRoutes.name = 'main-dynamic'
  137. mainRoutes.children = routes
  138. router.addRoutes([
  139. mainRoutes,
  140. { path: '*', redirect: { name: '404' } }
  141. ])
  142. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  143. console.log('\n')
  144. console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue')
  145. console.log(mainRoutes.children)
  146. console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue')
  147. }
  148. }
  149. export default router