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.

159 lines
5.5 KiB

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