index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { browser } from '@/helpers/utils';
  2. import { showDialog } from 'vant';
  3. import { createRouter, createWebHashHistory, Router } from 'vue-router';
  4. import { postMessage } from '@/helpers/native-message';
  5. import routes from './routes-common';
  6. const router: Router = createRouter({
  7. history: createWebHashHistory(),
  8. routes,
  9. scrollBehavior(to) {
  10. if (to.hash) {
  11. return {
  12. el: to.hash,
  13. behavior: 'smooth'
  14. };
  15. }
  16. }
  17. });
  18. router.beforeEach((to, from, next) => {
  19. document.title = (to.meta.title || '学生端') as any;
  20. // 团购报名页面区分两种类型的名称
  21. if (to.path === '/student-register-apply') {
  22. document.title = to.query.rt === 'MUST_BUY_GOODS' ? '音乐(器乐)数字AI团购通道' : '报名通道';
  23. }
  24. next();
  25. });
  26. let isOpen = false;
  27. router.onError(error => {
  28. if (error instanceof Error) {
  29. const isChunkLoadFailed = error.name.indexOf('chunk');
  30. const targetPath = router.currentRoute.value.fullPath;
  31. console.log(error);
  32. if (isChunkLoadFailed && !isOpen) {
  33. isOpen = true;
  34. showDialog({
  35. title: '更新提示',
  36. message: 'APP有更新请点击确定刷新页面?',
  37. confirmButtonColor: 'var(--van-primary)'
  38. }).then(() => {
  39. // on close
  40. if (browser().isApp) {
  41. postMessage({ api: 'back' });
  42. } else {
  43. location.hash = targetPath;
  44. window.location.reload();
  45. }
  46. });
  47. }
  48. }
  49. });
  50. export default router;