index.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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', '/lessonCourseware', '/courseList', '/companion-teacher-register', '/manage-teacher-register']
  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. // const toastStatus = baseEvent.emit('toastStatus')
  54. baseEvent.emit('toastClose')
  55. }, 300);
  56. }
  57. })
  58. let isOpen = false
  59. router.onError((error) => {
  60. if (error instanceof Error) {
  61. const isChunkLoadFailed = error.name.indexOf('chunk')
  62. const targetPath = router.currentRoute.value.fullPath
  63. console.log(error)
  64. if (isChunkLoadFailed && !isOpen) {
  65. baseEvent.emit('toastClose')
  66. isOpen = true
  67. showDialog({
  68. title: '更新提示',
  69. message: 'APP有更新请点击确定刷新页面?',
  70. confirmButtonColor: 'var(--van-primary)'
  71. }).then(() => {
  72. // on close
  73. if (browser().isApp) {
  74. postMessage({ api: 'back' })
  75. } else {
  76. location.hash = targetPath
  77. window.location.reload()
  78. }
  79. })
  80. }
  81. }
  82. })
  83. export default router