router.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { createRouter, createWebHistory } from 'vue-router'
  2. import { state } from './store'
  3. import NProgress from 'nprogress'
  4. import Dashboard from './pages/dashboard/index.vue'
  5. import NotFound from './pages/404'
  6. NProgress.configure({ showSpinner: false })
  7. declare module 'vue-router' {
  8. interface RouteMeta {
  9. auth?: boolean
  10. }
  11. }
  12. const router = createRouter({
  13. history: createWebHistory(import.meta.env.BASE_URL),
  14. scrollBehavior() {
  15. return { top: 0, left: 0 }
  16. },
  17. routes: [
  18. {
  19. path: '/login',
  20. component: () => import('./pages/login/index.vue'),
  21. },
  22. {
  23. path: '/register',
  24. component: () => import('./pages/register/index.vue'),
  25. },
  26. {
  27. path: '/password/:action',
  28. component: () => import('./pages/password/index.vue'),
  29. props: true,
  30. },
  31. {
  32. path: '/imei/:action',
  33. component: () => import('./pages/imei/index.vue'),
  34. props: true,
  35. meta: { auth: true },
  36. },
  37. {
  38. path: '/fill-order',
  39. component: () => import('./pages/fill-order/index.vue'),
  40. },
  41. {
  42. path: '/pay-result/:status',
  43. component: () => import('./pages/pay-result/index.vue'),
  44. },
  45. {
  46. path: '/account',
  47. component: () => import('./pages/account/index.vue'),
  48. meta: { auth: true },
  49. },
  50. {
  51. path: '/order',
  52. component: () => import('./pages/my-order/index.vue'),
  53. meta: { auth: true },
  54. },
  55. {
  56. path: '/order/:id',
  57. component: () => import('./pages/benefits/index.vue'),
  58. meta: { auth: true },
  59. },
  60. {
  61. path: '/gift-card',
  62. component: () => import('./pages/gift-card/index.vue'),
  63. meta: { auth: true },
  64. },
  65. {
  66. path: '/mailing',
  67. component: () => import('./pages/mailing/index.vue'),
  68. meta: { auth: true },
  69. },
  70. {
  71. path: '/renewal',
  72. component: () => import('./pages/renewal/index.vue'),
  73. meta: { auth: true },
  74. },
  75. {
  76. path: '/repair/appointment',
  77. component: () => import('./pages/repair/appointment.vue'),
  78. meta: { auth: true },
  79. },
  80. {
  81. path: '/repair/history',
  82. component: () => import('./pages/repair/history.vue'),
  83. meta: { auth: true },
  84. },
  85. {
  86. path: '/invite',
  87. component: () => import('./pages/invite/index.vue'),
  88. meta: { auth: true },
  89. },
  90. {
  91. path: '/dashboard',
  92. component: Dashboard,
  93. meta: { auth: true },
  94. },
  95. {
  96. path: '/:pathMatch(.*)*',
  97. name: 'NotFound',
  98. component: NotFound,
  99. },
  100. ],
  101. })
  102. router.beforeEach(to => {
  103. if (to.meta.auth && !state.userInfo) {
  104. return {
  105. path: '/login',
  106. query: {
  107. from: to.fullPath,
  108. },
  109. replace: true,
  110. }
  111. }
  112. NProgress.start()
  113. })
  114. router.afterEach(() => {
  115. state.ready = true
  116. NProgress.done()
  117. })
  118. router.onError(() => {
  119. NProgress.status = null
  120. NProgress.remove()
  121. })
  122. export default router