index.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { browser } from '@/helpers/utils'
  2. import { showDialog } from 'vant'
  3. import { createRouter, createWebHashHistory, 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 routesSchool from './routes-school'
  8. import baseEvent from '@/base-event'
  9. const paymentType = (window as any).paymentType
  10. let routes: any = []
  11. let baseUrl = null as any
  12. if (paymentType === 'STUDENT') {
  13. routes = routesStudent
  14. } else if (paymentType === 'TEACHER') {
  15. routes = routesTeacher
  16. baseUrl = '/teacher.html'
  17. } else if (paymentType === 'SCHOOL') {
  18. routes = routesSchool
  19. baseUrl = '/school.html'
  20. }
  21. // if (location.origin.indexOf('location') <= -1 || location.origin.indexOf('192.168.3') <= -1) {
  22. // baseUrl = ''
  23. // }
  24. console.log(routes, baseUrl, paymentType)
  25. const router: Router = createRouter({
  26. // history: createWebHistory('/school.html'),
  27. history: createWebHashHistory(),
  28. routes,
  29. scrollBehavior(to, from, savedPosition) {
  30. if (to.hash) {
  31. return {
  32. el: to.hash,
  33. behavior: 'smooth'
  34. }
  35. }
  36. }
  37. })
  38. const whitePath = ['/coursewarePlay']
  39. router.beforeEach((to, from, next) => {
  40. // if (!whitePath.includes(to.path)){
  41. // baseEvent.emit('toastShow')
  42. // }
  43. // console.log(to, from)
  44. document.title = (to.meta.title || '管乐团') as any
  45. next()
  46. })
  47. router.afterEach((to, from) => {
  48. // 为了处理全屏弹窗loading没有关闭
  49. // if (to.path === from.path) {
  50. // baseEvent.emit('toastClose')
  51. // } else {
  52. // setTimeout(() => {
  53. // baseEvent.emit('toastClose')
  54. // }, 300);
  55. // }
  56. })
  57. let isOpen = false
  58. router.onError((error) => {
  59. if (error instanceof Error) {
  60. const isChunkLoadFailed = error.name.indexOf('chunk')
  61. const targetPath = router.currentRoute.value.fullPath
  62. console.log(error)
  63. if (isChunkLoadFailed && !isOpen) {
  64. baseEvent.emit('toastClose')
  65. isOpen = true
  66. showDialog({
  67. title: '更新提示',
  68. message: 'APP有更新请点击确定刷新页面?',
  69. confirmButtonColor: 'var(--van-primary)'
  70. }).then(() => {
  71. // on close
  72. if (browser().isApp) {
  73. postMessage({ api: 'back' })
  74. } else {
  75. location.hash = targetPath
  76. window.location.reload()
  77. }
  78. })
  79. }
  80. }
  81. })
  82. export default router