plm前端
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.

164 lines
6.1 KiB

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