乐天mes前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.6 KiB

4 years ago
4 years ago
4 years ago
  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. import i18n from '@/i18n/i18n'
  13. Vue.use(Router)
  14. // 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
  15. const _import = require('./import-' + process.env.NODE_ENV)
  16. // 全局路由(无需嵌套上左右整体布局)
  17. const globalRoutes = [
  18. { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  19. { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } }
  20. ]
  21. // 主入口路由(需嵌套上左右整体布局)
  22. const mainRoutes = {
  23. path: '/',
  24. component: _import('main'),
  25. name: 'main',
  26. redirect: { name: 'home' },
  27. meta: { title: '主入口整体布局' },
  28. children: [
  29. // 通过meta对象设置路由展示方式
  30. // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
  31. // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
  32. // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
  33. { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
  34. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
  35. { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } },
  36. // { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }
  37. ],
  38. beforeEnter (to, from, next) {
  39. let token = Vue.cookie.get('token')
  40. if (!token || !/\S/.test(token)) {
  41. clearLoginInfo()
  42. next({ name: 'login' })
  43. }
  44. next()
  45. }
  46. }
  47. const router = new Router({
  48. mode: 'hash',
  49. scrollBehavior: () => ({ y: 0 }),
  50. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  51. routes: globalRoutes.concat(mainRoutes)
  52. })
  53. router.beforeEach((to, from, next) => {
  54. // 添加动态(菜单)路由
  55. // 1. 已经添加 or 全局路由, 直接访问
  56. // 2. 获取菜单列表, 添加并保存本地存储
  57. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  58. next()
  59. } else {
  60. http({
  61. url: http.adornUrl('/sys/menu/nav'),
  62. method: 'get',
  63. params: {
  64. 'l': i18n.locale, //i18n.locale
  65. menuType: "pc"
  66. }
  67. }).then(({data}) => {
  68. if (data && data.code === 0) {
  69. fnAddDynamicMenuRoutes(data.menuList)
  70. router.options.isAddDynamicMenuRoutes = true
  71. sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
  72. sessionStorage.setItem('permissions', JSON.stringify(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