index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: 'test',//四情
  51. name: 'test',
  52. component: () => import('../views/test/test.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.roleId)
  87. NProgress.start();
  88. // token存在的情况
  89. if (store.roleId || to.path=="/login") {
  90. next();
  91. NProgress.done();
  92. } else {
  93. // 没有token
  94. // next('/login') // 否则全部重定向到登录页
  95. next()
  96. NProgress.done();
  97. }
  98. });
  99. // 路由加载后(全局路由后置守卫)
  100. router.afterEach(() => {
  101. NProgress.done();
  102. });
  103. // 获取扁平化路由,将多级路由转换成一级路由
  104. // export const getKeepAliveRoutes = (rs, breadcrumb) => {
  105. // const routerList = []
  106. // rs.forEach((item) => {
  107. // if (item.meta.title) {
  108. // // breadcrumb.push(item.meta.title)
  109. // }
  110. // if (item.children && item.children.length > 0) {
  111. // routerList.push(...getKeepAliveRoutes(item.children, breadcrumb))
  112. // } else {
  113. // // item.meta.breadcrumb.push(...breadcrumb)
  114. // routerList.push(item)
  115. // }
  116. // // breadcrumb.pop()
  117. // })
  118. // return routerList
  119. // }
  120. // 加载vue组件
  121. // const layoutModules =
  122. // import.meta.glob('/src/views/**/*.vue')
  123. // 根据路径,动态获取vue组件
  124. // const getDynamicComponent = (path) => {
  125. // const component = layoutModules[`/src/views/${path}.vue`]
  126. // if (!component) {
  127. // console.error('组件不存在,路径为:', path)
  128. // }
  129. // return component
  130. // }
  131. // 根据菜单列表,生成路由数据
  132. // export const generateRoutes = (menuList) => {
  133. // const routerList = []
  134. // menuList.forEach((menu) => {
  135. // let component;
  136. // let path;
  137. // if (menu.children && menu.children.length > 0) {
  138. // component = () => import('@/layouts/backend/index.vue')
  139. // path = '/p/' + menu.menuId
  140. // } else {
  141. // component = getDynamicComponent(menu.component)
  142. // path = menu.menuUrl
  143. // }
  144. // const route = {
  145. // path: path,
  146. // name: menu.menuName,
  147. // component: component,
  148. // children: [],
  149. // meta: {
  150. // title: menu.menuName,
  151. // icon: menu.menuIcon,
  152. // id: '' + menu.menuId,
  153. // cache: true,
  154. // _blank: menu.openStyle === 1,
  155. // breadcrumb: []
  156. // }
  157. // }
  158. // // 有子菜单的情况
  159. // if (menu.children && menu.children.length > 0) {
  160. // route.children?.push(...generateRoutes(menu.children))
  161. // }
  162. // routerList.push(route)
  163. // })
  164. // return routerList
  165. // }
  166. export default router;