公众号前端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

index.js 5.3 KiB

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