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.

168 lines
7.1 KiB

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