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.

173 lines
6.2 KiB

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