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.

161 lines
5.7 KiB

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