index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { browser } from '@/helpers/utils'
  2. import { Dialog } from 'vant'
  3. import { createRouter, createWebHistory, Router } from 'vue-router'
  4. import { postMessage } from '@/helpers/native-message'
  5. import routesTeacher from './routes-teacher'
  6. import routesStudent from './routes-student'
  7. import routesManage from './routes-manage'
  8. const paymentType = (window as any).paymentType
  9. let routes: any = []
  10. if (paymentType === 'STUDENT') {
  11. routes = routesStudent
  12. } else if (paymentType === 'TEACHER') {
  13. routes = routesTeacher
  14. } else if (paymentType === 'SCHOOL') {
  15. routes = routesManage
  16. }
  17. const router: Router = createRouter({
  18. history: createWebHistory(),
  19. routes,
  20. scrollBehavior(to, from, savedPosition) {
  21. if (to.hash) {
  22. return {
  23. el: to.hash,
  24. behavior: 'smooth'
  25. }
  26. }
  27. }
  28. })
  29. router.beforeEach((to, from, next) => {
  30. document.title = (to.meta.title || '管乐团') as any
  31. next()
  32. })
  33. let isOpen = false
  34. router.onError(error => {
  35. if (error instanceof Error) {
  36. const isChunkLoadFailed = error.name.indexOf('chunk')
  37. const targetPath = router.currentRoute.value.fullPath
  38. if (isChunkLoadFailed && !isOpen) {
  39. isOpen = true
  40. Dialog.alert({
  41. title: '更新提示',
  42. message: 'APP有更新请点击确定刷新页面?',
  43. confirmButtonColor: 'var(--van-primary)'
  44. }).then(() => {
  45. // on close
  46. if (browser().isApp) {
  47. postMessage({ api: 'back' })
  48. } else {
  49. location.hash = targetPath
  50. window.location.reload()
  51. }
  52. })
  53. }
  54. }
  55. })
  56. export default router