斯瑞奇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.

160 lines
5.7 KiB

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: '/scheduleBoard', component: _import('modules/board/scheduleBoard'), name: 'scheduleBoard', 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. http({
  63. url: http.adornUrl('/sys/menu/nav'),
  64. method: 'get',
  65. params: {
  66. 'l': i18n.locale, //i18n.locale
  67. menuType: "pc"
  68. }
  69. }).then(({data}) => {
  70. if (data && data.code === 0) {
  71. fnAddDynamicMenuRoutes(data.menuList)
  72. router.options.isAddDynamicMenuRoutes = true
  73. sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
  74. sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]'))
  75. next({ ...to, replace: true })
  76. } else {
  77. sessionStorage.setItem('menuList', '[]')
  78. sessionStorage.setItem('permissions', '[]')
  79. next()
  80. }
  81. }).catch((e) => {
  82. console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  83. router.push({ name: 'login' })
  84. })
  85. }
  86. })
  87. /**
  88. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  89. * @param {*} route 当前路由
  90. */
  91. function fnCurrentRouteType (route, globalRoutes = []) {
  92. var temp = []
  93. for (var i = 0; i < globalRoutes.length; i++) {
  94. if (route.path === globalRoutes[i].path) {
  95. return 'global'
  96. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  97. temp = temp.concat(globalRoutes[i].children)
  98. }
  99. }
  100. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  101. }
  102. /**
  103. * 添加动态(菜单)路由
  104. * @param {*} menuList 菜单列表
  105. * @param {*} routes 递归创建的动态(菜单)路由
  106. */
  107. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  108. var temp = []
  109. for (var i = 0; i < menuList.length; i++) {
  110. if (menuList[i].list && menuList[i].list.length >= 1) {
  111. temp = temp.concat(menuList[i].list)
  112. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  113. menuList[i].url = menuList[i].url.replace(/^\//, '')
  114. var route = {
  115. path: menuList[i].url.replace('/', '-'),
  116. component: null,
  117. name: menuList[i].url.replace('/', '-'),
  118. meta: {
  119. menuId: menuList[i].menuId,
  120. title: menuList[i].name,
  121. isDynamic: true,
  122. isTab: true,
  123. iframeUrl: ''
  124. }
  125. }
  126. // url以http[s]://开头, 通过iframe展示
  127. if (isURL(menuList[i].url)) {
  128. route['path'] = `i-${menuList[i].menuId}`
  129. route['name'] = `i-${menuList[i].menuId}`
  130. route['meta']['iframeUrl'] = menuList[i].url
  131. } else {
  132. try {
  133. route['component'] = _import(`modules/${menuList[i].url}`) || null
  134. } catch (e) {}
  135. }
  136. routes.push(route)
  137. }
  138. }
  139. if (temp.length >= 1) {
  140. fnAddDynamicMenuRoutes(temp, routes)
  141. } else {
  142. mainRoutes.name = 'main-dynamic'
  143. mainRoutes.children = routes
  144. router.addRoutes([
  145. mainRoutes,
  146. { path: '*', redirect: { name: '404' } }
  147. ])
  148. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  149. }
  150. }
  151. export default router