健腾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.

165 lines
6.4 KiB

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