router.ts 2.8 KB

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