1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /**
- * 判断当前应用路由是否匹配到
- * @param router
- * @returns {boolean}
- */
- function getHasRouteMatched (router) {
- var pathName = location.pathname
- var baseName = router.options.base
- var pathIndex = pathName.indexOf(baseName)
- // 存在别名情况,需要额外匹配
- var nowPath = pathName
- if (pathIndex > -1) {
- nowPath = pathName.slice(pathIndex + baseName.length)
- }
- var { route } = router.resolve({
- path: nowPath
- })
- var hasRouteMatched = route.matched.length > 0
- return hasRouteMatched
- }
- function sendNotFindPageError (content, options) {
- if (window.Sentry && typeof window.Sentry.captureMessage === 'function') {
- Sentry.captureMessage(content, options)
- }
- }
- /**
- * 子应用 404 页面拦截
- * @param router
- */
- function addNotFindPage(router) {
- var notFindPage = "/page_error/404.html";
- router.beforeEach((to, from, next) => {
- // vue-router相关路由,history.state中必有key
- var vueRouterHistory = history.state && history.state.key
- if (to.matched.length === 0 && vueRouterHistory) {
- sendNotFindPageError(new Error('Error getHasRouteMatched beforeEach'), {
- level: 'error',
- tags: {
- href: location.href
- },
- extra: {
- router: router,
- to: to,
- from: from
- }
- })
- if (window.jQuery) {
- $.ajax({
- url: '/data-export/help-report',
- method: 'post',
- data: {
- type: 'Error getHasRouteMatched beforeEach',
- extra: JSON.stringify({
- router: router,
- to: to,
- from: from
- })
- }
- })
- }
- window.top.location.replace(notFindPage);
- } else {
- next();
- }
- });
- if (!getHasRouteMatched(router)) {
- sendNotFindPageError(new Error('Error getHasRouteMatched'), {
- level: 'error',
- tags: {
- href: location.href
- },
- extra: {
- router: router
- }
- })
- if (window.jQuery) {
- $.ajax({
- url: '/data-export/help-report',
- method: 'post',
- data: {
- type: 'Error getHasRouteMatched',
- extra: JSON.stringify({
- router: router,
- })
- }
- })
- }
- window.top.location.replace(notFindPage);
- }
- }
|