12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import { browser } from '@/helpers/utils'
- import { showDialog } from 'vant'
- import { createRouter, createWebHashHistory, createWebHistory, Router } from 'vue-router'
- import { postMessage } from '@/helpers/native-message'
- import routesTeacher from './routes-teacher'
- import routesStudent from './routes-student'
- import routesSchool from './routes-school'
- import baseEvent from '@/base-event'
- const paymentType = (window as any).paymentType
- let routes: any = []
- let baseUrl = null as any
- if (paymentType === 'STUDENT') {
- routes = routesStudent
- } else if (paymentType === 'TEACHER') {
- routes = routesTeacher
- baseUrl = '/teacher.html'
- } else if (paymentType === 'SCHOOL') {
- routes = routesSchool
- baseUrl = '/school.html'
- }
- // if (location.origin.indexOf('location') <= -1 || location.origin.indexOf('192.168.3') <= -1) {
- // baseUrl = ''
- // }
- console.log(routes, baseUrl, paymentType)
- const router: Router = createRouter({
- // history: createWebHistory('/school.html'),
- history: createWebHashHistory(),
- routes,
- scrollBehavior(to, from, savedPosition) {
- if (to.hash) {
- return {
- el: to.hash,
- behavior: 'smooth'
- }
- }
- }
- })
- const whitePath = ['/coursewarePlay', '/lessonCourseware', '/courseList', '/companion-teacher-register', '/manage-teacher-register']
- router.beforeEach((to, from, next) => {
- if (!whitePath.includes(to.path)) {
- baseEvent.emit('toastShow')
- }
- // console.log(to, from)
- document.title = (to.meta.title || '管乐团') as any
- next()
- })
- router.afterEach((to, from) => {
- // 为了处理全屏弹窗loading没有关闭
- if (to.path === from.path) {
- baseEvent.emit('toastClose')
- } else {
- setTimeout(() => {
- // const toastStatus = baseEvent.emit('toastStatus')
- baseEvent.emit('toastClose')
- }, 300);
- }
- })
- let isOpen = false
- router.onError((error) => {
- if (error instanceof Error) {
- const isChunkLoadFailed = error.name.indexOf('chunk')
- const targetPath = router.currentRoute.value.fullPath
- console.log(error)
- if (isChunkLoadFailed && !isOpen) {
- baseEvent.emit('toastClose')
- isOpen = true
- showDialog({
- title: '更新提示',
- message: 'APP有更新请点击确定刷新页面?',
- confirmButtonColor: 'var(--van-primary)'
- }).then(() => {
- // on close
- if (browser().isApp) {
- postMessage({ api: 'back' })
- } else {
- location.hash = targetPath
- window.location.reload()
- }
- })
- }
- }
- })
- export default router
|