index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { isIPad, isMobile } from 'billd-utils';
  2. import { createRouter, createWebHistory } from 'vue-router';
  3. import MobileLayout from '@/layout/mobile/index.vue';
  4. import PcLayout from '@/layout/pc/index.vue';
  5. import type { RouteRecordRaw } from 'vue-router';
  6. export const mobileRouterName = {
  7. h5: 'h5',
  8. h5Room: 'h5Room',
  9. h5Area: 'h5Area',
  10. };
  11. export const routerName = {
  12. home: 'home',
  13. about: 'about',
  14. account: 'account',
  15. rank: 'rank',
  16. sponsors: 'sponsors',
  17. support: 'support',
  18. order: 'order',
  19. shop: 'shop',
  20. link: 'link',
  21. ad: 'ad',
  22. faq: 'faq',
  23. team: 'team',
  24. oauth: 'oauth',
  25. release: 'release',
  26. notFound: 'notFound',
  27. group: 'group',
  28. profile: 'profile',
  29. pull: 'pull',
  30. push: 'push',
  31. ...mobileRouterName,
  32. };
  33. // 默认路由
  34. export const defaultRoutes: RouteRecordRaw[] = [
  35. {
  36. name: routerName.oauth,
  37. path: '/oauth/:platform',
  38. component: () => import('@/views/oauth/index.vue'),
  39. },
  40. {
  41. path: '/',
  42. component: PcLayout,
  43. children: [
  44. {
  45. name: routerName.home,
  46. path: '/',
  47. component: () => import('@/views/home/index.vue'),
  48. },
  49. {
  50. name: routerName.about,
  51. path: '/about',
  52. children: [
  53. {
  54. name: routerName.group,
  55. path: 'group',
  56. component: () => import('@/views/group/index.vue'),
  57. },
  58. {
  59. name: routerName.faq,
  60. path: 'faq',
  61. component: () => import('@/views/faq/index.vue'),
  62. },
  63. {
  64. name: routerName.team,
  65. path: 'team',
  66. component: () => import('@/views/team/index.vue'),
  67. },
  68. {
  69. name: routerName.release,
  70. path: 'release',
  71. component: () => import('@/views/release/index.vue'),
  72. },
  73. ],
  74. },
  75. {
  76. name: routerName.rank,
  77. path: '/rank',
  78. component: () => import('@/views/rank/index.vue'),
  79. },
  80. {
  81. name: routerName.shop,
  82. path: '/shop',
  83. component: () => import('@/views/shop/index.vue'),
  84. },
  85. {
  86. name: routerName.profile,
  87. path: '/profile/:userId',
  88. component: () => import('@/views/profile/index.vue'),
  89. },
  90. {
  91. name: routerName.account,
  92. path: '/account',
  93. component: () => import('@/views/account/index.vue'),
  94. },
  95. {
  96. name: routerName.sponsors,
  97. path: '/sponsors',
  98. component: () => import('@/views/sponsors/index.vue'),
  99. },
  100. {
  101. name: routerName.support,
  102. path: '/support',
  103. component: () => import('@/views/support/index.vue'),
  104. },
  105. {
  106. name: routerName.order,
  107. path: '/order',
  108. component: () => import('@/views/order/index.vue'),
  109. },
  110. {
  111. name: routerName.ad,
  112. path: '/ad',
  113. component: () => import('@/views/ad/index.vue'),
  114. },
  115. {
  116. name: routerName.pull,
  117. path: '/pull/:roomId',
  118. component: () => import('@/views/pull/index.vue'),
  119. },
  120. {
  121. name: routerName.push,
  122. path: '/push',
  123. component: () => import('@/views/push/index.vue'),
  124. },
  125. ],
  126. },
  127. {
  128. path: '/h5',
  129. component: MobileLayout,
  130. children: [
  131. {
  132. name: mobileRouterName.h5,
  133. path: '',
  134. component: () => import('@/views/h5/index.vue'),
  135. },
  136. {
  137. name: mobileRouterName.h5Area,
  138. path: 'area/:areaId',
  139. component: () => import('@/views/h5/area/index.vue'),
  140. },
  141. ],
  142. },
  143. {
  144. name: mobileRouterName.h5Room,
  145. path: '/h5/:roomId',
  146. component: () => import('@/views/h5/room/index.vue'),
  147. },
  148. ];
  149. const router = createRouter({
  150. routes: [
  151. ...defaultRoutes,
  152. {
  153. path: '/:pathMatch(.*)*',
  154. name: routerName.notFound,
  155. component: () => import('@/views/notFound.vue'),
  156. },
  157. ],
  158. history: createWebHistory(),
  159. });
  160. router.beforeEach((to, from, next) => {
  161. if (to.name === routerName.oauth) {
  162. return next();
  163. }
  164. if (isMobile() && !isIPad()) {
  165. if (!Object.keys(mobileRouterName).includes(to.name as string)) {
  166. // 当前移动端,但是跳转了非移动端路由
  167. return next({ name: mobileRouterName.h5 });
  168. } else {
  169. return next();
  170. }
  171. } else {
  172. if (Object.keys(mobileRouterName).includes(to.name as string)) {
  173. // 当前非移动端,但是跳转了移动端路由
  174. return next({ name: routerName.home });
  175. }
  176. return next();
  177. }
  178. });
  179. export default router;