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.

178 lines
7.2 KiB

2 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: '/pre', component: _import('common/previewFile'), name: 'pre', meta: { title: '文件预览' } },
  21. { path: '/login-token', component: _import('common/login-token'), name: 'login', meta: { title: '登录' } },
  22. { path: '/screen-whole-lift-progress', component: _import('modules/longchuang/screen-whole-lift-progress'), name: 'screen-whole-lift-progress', meta: { title: '整梯生产进度看板' } },
  23. { path: '/screen-cable-cop-progress', component: _import('modules/longchuang/screen-cable-cop-progress'), name: 'screen-cable-cop-progress', meta: { title: '线缆/COP生产进度看板' } },
  24. { path: '/screen-renovation-progress', component: _import('modules/longchuang/screen-renovation-progress'), name: 'screen-renovation-progress', meta: { title: '改造项目生产进度看板' } },
  25. { path: '/screen-factory-overview', component: _import('modules/longchuang/screen-factory-overview'), name: 'screen-factory-overview', meta: { title: '工厂综合运营看板' } },
  26. { path: '/screen-rack-plating-progress', component: _import('modules/rack/screen-rack-plating-progress'), name: 'screen-rack-plating-progress', meta: { title: '挂具过站采集大屏' } },
  27. ]
  28. // 主入口路由(需嵌套上左右整体布局)
  29. const mainRoutes = {
  30. path: '/',
  31. component: _import('main'),
  32. name: 'main',
  33. redirect: { name: 'home' },
  34. meta: { title: '主入口整体布局' },
  35. children: [
  36. // 通过meta对象设置路由展示方式
  37. // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
  38. // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
  39. // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
  40. { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
  41. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
  42. { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } },
  43. // { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }
  44. { path: '/customer_report_show', component: _import('modules/report/customer_report_show'), name: 'report', meta: { title: '自定义报表展示' } },//2022-04-20 自定义报表路径
  45. ],
  46. beforeEnter (to, from, next) {
  47. let token =Vue.cookie.get('token')
  48. if (!token || !/\S/.test(token)) {
  49. clearLoginInfo()
  50. next({ name: 'login' })
  51. }
  52. next()
  53. }
  54. }
  55. const router = new Router({
  56. mode: 'hash',
  57. scrollBehavior: () => ({ y: 0 }),
  58. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  59. routes: globalRoutes.concat(mainRoutes)
  60. })
  61. router.beforeEach((to, from, next) => {console.log(Vue.prototype.$store)
  62. // 添加动态(菜单)路由
  63. // 1. 已经添加 or 全局路由, 直接访问
  64. // 2. 获取菜单列表, 添加并保存本地存储
  65. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  66. next()
  67. } else {
  68. let menuType = localStorage.getItem('menuType')
  69. http({
  70. url: http.adornUrl('/sys/menu/nav'),
  71. method: 'get',
  72. params: {
  73. 'l': i18n.locale, //i18n.locale
  74. menuType: 'pc'
  75. }
  76. }).then(({data}) => {
  77. if (data && data.code === 0) {
  78. fnAddDynamicMenuRoutes(data.menuList)
  79. router.options.isAddDynamicMenuRoutes = true
  80. sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
  81. sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]'))
  82. sessionStorage.setItem('userConfig', JSON.stringify(data.userConfig || '[]'))
  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. const token = localStorage.getItem('token');
  95. const isLogin = to.name === 'login'; // 判断是否是登录页
  96. // 保存原始路径(仅在未登录状态下保存)
  97. if (!token && !isLogin) {
  98. console.log('保存重定向路径:', to.fullPath);
  99. localStorage.setItem('redirectPath', to.fullPath);
  100. }
  101. })
  102. /**
  103. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  104. * @param {*} route 当前路由
  105. */
  106. function fnCurrentRouteType (route, globalRoutes = []) {
  107. var temp = []
  108. for (var i = 0; i < globalRoutes.length; i++) {
  109. if (route.path === globalRoutes[i].path) {
  110. return 'global'
  111. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  112. temp = temp.concat(globalRoutes[i].children)
  113. }
  114. }
  115. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  116. }
  117. /**
  118. * 添加动态(菜单)路由
  119. * @param {*} menuList 菜单列表
  120. * @param {*} routes 递归创建的动态(菜单)路由
  121. */
  122. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  123. var temp = []
  124. for (var i = 0; i < menuList.length; i++) {
  125. if (menuList[i].list && menuList[i].list.length >= 1) {
  126. temp = temp.concat(menuList[i].list)
  127. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  128. menuList[i].url = menuList[i].url.replace(/^\//, '')
  129. var route = {
  130. path: menuList[i].url.replace('/', '-'),
  131. component: null,
  132. name: menuList[i].url.replace('/', '-'),
  133. meta: {
  134. menuId: menuList[i].menuId,
  135. title: menuList[i].name,
  136. isDynamic: true,
  137. isTab: true,
  138. iframeUrl: ''
  139. }
  140. }
  141. // url以http[s]://开头, 通过iframe展示
  142. if (isURL(menuList[i].url)) {
  143. route['path'] = `i-${menuList[i].menuId}`
  144. route['name'] = `i-${menuList[i].menuId}`
  145. route['meta']['iframeUrl'] = menuList[i].url
  146. } else {
  147. try {
  148. route['component'] = _import(`modules/${menuList[i].url}`) || null
  149. } catch (e) {}
  150. }
  151. routes.push(route)
  152. }
  153. }
  154. if (temp.length >= 1) {
  155. fnAddDynamicMenuRoutes(temp, routes)
  156. } else {
  157. mainRoutes.name = 'main-dynamic'
  158. mainRoutes.children = routes
  159. router.addRoutes([
  160. mainRoutes,
  161. { path: '*', redirect: { name: '404' } }
  162. ])
  163. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  164. }
  165. }
  166. export default router