router.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. },
  44. {
  45. path: '/account',
  46. component: () => import('./pages/account/index.vue'),
  47. meta: { auth: true },
  48. },
  49. {
  50. path: '/order',
  51. component: () => import('./pages/my-order/index.vue'),
  52. meta: { auth: true },
  53. },
  54. {
  55. path: '/order/:id',
  56. component: () => import('./pages/benefits/index.vue'),
  57. meta: { auth: true },
  58. },
  59. {
  60. path: '/gift-card',
  61. component: () => import('./pages/gift-card/index.vue'),
  62. meta: { auth: true },
  63. },
  64. {
  65. path: '/mailing',
  66. component: () => import('./pages/mailing/index.vue'),
  67. meta: { auth: true },
  68. },
  69. {
  70. path: '/renewal',
  71. component: () => import('./pages/renewal/index.vue'),
  72. meta: { auth: true },
  73. },
  74. {
  75. path: '/repair/appointment',
  76. component: () => import('./pages/repair/appointment.vue'),
  77. meta: { auth: true },
  78. },
  79. {
  80. path: '/repair/history',
  81. component: () => import('./pages/repair/history.vue'),
  82. meta: { auth: true },
  83. },
  84. {
  85. path: '/invite',
  86. component: () => import('./pages/invite/index.vue'),
  87. meta: { auth: true },
  88. },
  89. {
  90. path: '/dashboard',
  91. component: Dashboard,
  92. meta: { auth: true },
  93. },
  94. ],
  95. })
  96. router.beforeEach(to => {
  97. if (to.meta.auth && !state.userInfo) {
  98. return {
  99. path: '/login',
  100. query: {
  101. from: to.fullPath,
  102. },
  103. replace: true,
  104. }
  105. }
  106. NProgress.start()
  107. })
  108. router.afterEach(() => NProgress.done())
  109. router.onError(() => {
  110. NProgress.status = null
  111. NProgress.remove()
  112. })
  113. export default router