router.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. meta: { auth: true },
  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. })
  97. router.beforeEach(to => {
  98. if (to.meta.auth && !state.userInfo) {
  99. return {
  100. path: '/login',
  101. query: {
  102. from: to.fullPath,
  103. },
  104. replace: true,
  105. }
  106. }
  107. NProgress.start()
  108. })
  109. router.afterEach(() => NProgress.done())
  110. router.onError(() => {
  111. NProgress.status = null
  112. NProgress.remove()
  113. })
  114. export default router