index-student.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { browser } from '@/helpers/utils'
  2. import { state } from '@/state'
  3. import { Dialog } from 'vant'
  4. import { createRouter, createWebHashHistory, Router } from 'vue-router'
  5. import routes from './routes-student'
  6. const router: Router = createRouter({
  7. history: createWebHashHistory(),
  8. routes
  9. })
  10. router.beforeEach((to, from, next) => {
  11. const title = to.meta.title
  12. document.title = (title || '管乐迷') as any
  13. next()
  14. // if (browser().iPhone && !state.version) {
  15. // try {
  16. // postMessage(
  17. // {
  18. // api: 'getVersion'
  19. // },
  20. // (res: any) => {
  21. // state.version = res.version
  22. // console.log(res, 'version')
  23. // setTimeout(() => {
  24. // next()
  25. // }, 50)
  26. // }
  27. // )
  28. // } catch {}
  29. // // 为了处理上面方法的没有返回
  30. // setTimeout(() => {
  31. // if (!state.version) {
  32. // next()
  33. // // }
  34. // // }, 5000)
  35. // } else {
  36. // console.log(222)
  37. // next()
  38. // }
  39. })
  40. let isOpen = false
  41. router.onError(error => {
  42. if (error instanceof Error) {
  43. const isChunkLoadFailed = error.name.indexOf('chunk')
  44. const targetPath = router.currentRoute.value.fullPath
  45. if (isChunkLoadFailed && !isOpen) {
  46. isOpen = true
  47. Dialog.alert({
  48. title: '更新提示',
  49. message: 'APP有更新请点击确定刷新页面?',
  50. confirmButtonColor: 'var(--van-primary)'
  51. }).then(() => {
  52. // on close
  53. location.hash = targetPath
  54. window.location.reload()
  55. })
  56. }
  57. }
  58. })
  59. export default router