router.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { createRouter, createWebHistory } from 'vue-router'
  2. import { state } from './store'
  3. // @ts-ignore
  4. import NProgress from 'nprogress'
  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. },
  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. props: true,
  74. meta: { auth: true },
  75. },
  76. {
  77. path: '/repaire/appointment',
  78. component: () => import('./pages/repaire/appointment.vue'),
  79. props: true,
  80. meta: { auth: true },
  81. },
  82. {
  83. path: '/repaire/history',
  84. component: () => import('./pages/repaire/history.vue'),
  85. meta: { auth: true },
  86. },
  87. {
  88. path: '/invite',
  89. component: () => import('./pages/invite/index.vue'),
  90. meta: { auth: true },
  91. },
  92. ],
  93. })
  94. router.beforeEach(to => {
  95. if (to.meta.auth && !state.userInfo) {
  96. return {
  97. path: '/login',
  98. query: {
  99. from: to.fullPath,
  100. },
  101. replace: true,
  102. }
  103. }
  104. NProgress.start()
  105. })
  106. router.afterEach(() => NProgress.done())
  107. router.onError(() => {
  108. NProgress.status = null
  109. NProgress.remove()
  110. })
  111. export default router