index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import {
  2. createRouter,
  3. createWebHistory,
  4. createWebHashHistory,
  5. } from 'vue-router';
  6. import {
  7. ElMessage
  8. } from 'element-plus';
  9. import NProgress from 'nprogress'; //页面顶部加载进度条
  10. import 'nprogress/nprogress.css';
  11. //此处注释store
  12. import {
  13. Store
  14. } from '@/store/index';
  15. NProgress.configure({
  16. showSpinner: false
  17. });
  18. var store = new Store()
  19. // 公共路由
  20. export const constantRoutes = [
  21. /*
  22. *** 添加router路由
  23. *** 11.23日修改
  24. */
  25. {
  26. path: '/home',
  27. component: () => import('/src/views/Main/MainView.vue'),
  28. children: [{
  29. path: 'Map3d',
  30. name: 'Map3d',
  31. component: () => import('../views/Map3d/Map3DMain.vue'),
  32. meta: {
  33. keepAlive: true // 需要缓存
  34. }
  35. }, {
  36. path: 'table',
  37. name: 'table',
  38. component: () => import('../views/Table/Table.vue'),
  39. meta: {
  40. keepAlive: false // 不需要缓存
  41. }
  42. }, {
  43. path: 'echarts',
  44. name: 'echarts',
  45. component: () => import('../views/Charts/Charts.vue'),
  46. meta: {
  47. keepAlive: false // 不需要缓存
  48. }
  49. }, {
  50. path: 'SysAdmin',
  51. name: 'SysAdmin',
  52. component: () => import('../views/SysAdmin/SysAdmin.vue'),
  53. meta: {
  54. keepAlive: false // 不需要缓存
  55. }
  56. }, {
  57. path: '',
  58. name: 'map',
  59. component: () => import('../views/Map3d/Map3DMain.vue'),
  60. meta: {
  61. keepAlive: true // 需要缓存
  62. }
  63. }
  64. ]
  65. }, {
  66. path: '/login',
  67. component: () => import('/src/views/Login/Login.vue')
  68. }, {
  69. path: '/',
  70. component: () => import('/src/views/Login/Login.vue')
  71. }
  72. ];
  73. // 错误路由
  74. const errorRoute = {
  75. path: '/:pathMatch(.*)',
  76. redirect: '/404'
  77. }
  78. const router = createRouter({
  79. history: createWebHashHistory(),
  80. routes: constantRoutes
  81. })
  82. // 白名单列表
  83. const whiteList = ['/login']
  84. // 路由加载前(全局路由前置守卫)
  85. router.beforeEach((to, from, next) => {
  86. console.log('store', store.token)
  87. NProgress.start();
  88. // token存在的情况
  89. if (store.token || to.path=="/login") {
  90. next();
  91. NProgress.done();
  92. } else {
  93. // 没有token
  94. next('/login') // 否则全部重定向到登录页
  95. NProgress.done();
  96. }
  97. });
  98. // 路由加载后(全局路由后置守卫)
  99. router.afterEach(() => {
  100. NProgress.done();
  101. });
  102. // 获取扁平化路由,将多级路由转换成一级路由
  103. // export const getKeepAliveRoutes = (rs, breadcrumb) => {
  104. // const routerList = []
  105. // rs.forEach((item) => {
  106. // if (item.meta.title) {
  107. // // breadcrumb.push(item.meta.title)
  108. // }
  109. // if (item.children && item.children.length > 0) {
  110. // routerList.push(...getKeepAliveRoutes(item.children, breadcrumb))
  111. // } else {
  112. // // item.meta.breadcrumb.push(...breadcrumb)
  113. // routerList.push(item)
  114. // }
  115. // // breadcrumb.pop()
  116. // })
  117. // return routerList
  118. // }
  119. // 加载vue组件
  120. // const layoutModules =
  121. // import.meta.glob('/src/views/**/*.vue')
  122. // 根据路径,动态获取vue组件
  123. // const getDynamicComponent = (path) => {
  124. // const component = layoutModules[`/src/views/${path}.vue`]
  125. // if (!component) {
  126. // console.error('组件不存在,路径为:', path)
  127. // }
  128. // return component
  129. // }
  130. // 根据菜单列表,生成路由数据
  131. // export const generateRoutes = (menuList) => {
  132. // const routerList = []
  133. // menuList.forEach((menu) => {
  134. // let component;
  135. // let path;
  136. // if (menu.children && menu.children.length > 0) {
  137. // component = () => import('@/layouts/backend/index.vue')
  138. // path = '/p/' + menu.menuId
  139. // } else {
  140. // component = getDynamicComponent(menu.component)
  141. // path = menu.menuUrl
  142. // }
  143. // const route = {
  144. // path: path,
  145. // name: menu.menuName,
  146. // component: component,
  147. // children: [],
  148. // meta: {
  149. // title: menu.menuName,
  150. // icon: menu.menuIcon,
  151. // id: '' + menu.menuId,
  152. // cache: true,
  153. // _blank: menu.openStyle === 1,
  154. // breadcrumb: []
  155. // }
  156. // }
  157. // // 有子菜单的情况
  158. // if (menu.children && menu.children.length > 0) {
  159. // route.children?.push(...generateRoutes(menu.children))
  160. // }
  161. // routerList.push(route)
  162. // })
  163. // return routerList
  164. // }
  165. export default router;