index.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const paymentType = (window as any).paymentType
  9. let routes: any = []
  10. let baseUrl = null as any
  11. if (paymentType === 'STUDENT') {
  12. routes = routesStudent
  13. } else if (paymentType === 'TEACHER') {
  14. routes = routesTeacher
  15. baseUrl = '/teacher.html'
  16. } else if (paymentType === 'SCHOOL') {
  17. routes = routesSchool
  18. baseUrl = '/school.html'
  19. }
  20. // if (location.origin.indexOf('location') <= -1 || location.origin.indexOf('192.168.3') <= -1) {
  21. // baseUrl = ''
  22. // }
  23. console.log(routes, baseUrl, paymentType)
  24. const router: Router = createRouter({
  25. // history: createWebHistory('/school.html'),
  26. history: createWebHashHistory(),
  27. routes,
  28. scrollBehavior(to, from, savedPosition) {
  29. if (to.hash) {
  30. return {
  31. el: to.hash,
  32. behavior: 'smooth'
  33. }
  34. }
  35. }
  36. })
  37. router.beforeEach((to, from, next) => {
  38. document.title = (to.meta.title || '管乐团') as any
  39. next()
  40. })
  41. let isOpen = false
  42. router.onError((error) => {
  43. if (error instanceof Error) {
  44. const isChunkLoadFailed = error.name.indexOf('chunk')
  45. const targetPath = router.currentRoute.value.fullPath
  46. console.log(error)
  47. if (isChunkLoadFailed && !isOpen) {
  48. isOpen = true
  49. showDialog({
  50. title: '更新提示',
  51. message: 'APP有更新请点击确定刷新页面?',
  52. confirmButtonColor: 'var(--van-primary)'
  53. }).then(() => {
  54. // on close
  55. if (browser().isApp) {
  56. postMessage({ api: 'back' })
  57. } else {
  58. location.hash = targetPath
  59. window.location.reload()
  60. }
  61. })
  62. }
  63. }
  64. })
  65. export default router