qiankun-common-functions.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * 判断当前应用路由是否匹配到
  3. * @param router
  4. * @returns {boolean}
  5. */
  6. function getHasRouteMatched (router) {
  7. var pathName = location.pathname
  8. var baseName = router.options.base
  9. var pathIndex = pathName.indexOf(baseName)
  10. // 存在别名情况,需要额外匹配
  11. var nowPath = pathName
  12. if (pathIndex > -1) {
  13. nowPath = pathName.slice(pathIndex + baseName.length)
  14. }
  15. var { route } = router.resolve({
  16. path: nowPath
  17. })
  18. var hasRouteMatched = route.matched.length > 0
  19. return hasRouteMatched
  20. }
  21. function sendNotFindPageError (content, options) {
  22. if (window.Sentry && typeof window.Sentry.captureMessage === 'function') {
  23. Sentry.captureMessage(content, options)
  24. }
  25. }
  26. /**
  27. * 子应用 404 页面拦截
  28. * @param router
  29. */
  30. function addNotFindPage(router) {
  31. var notFindPage = "/page_error/404.html";
  32. router.beforeEach((to, from, next) => {
  33. // vue-router相关路由,history.state中必有key
  34. var vueRouterHistory = history.state && history.state.key
  35. if (to.matched.length === 0 && vueRouterHistory) {
  36. sendNotFindPageError(new Error('Error getHasRouteMatched beforeEach'), {
  37. level: 'error',
  38. tags: {
  39. href: location.href
  40. },
  41. extra: {
  42. router: router,
  43. to: to,
  44. from: from
  45. }
  46. })
  47. if (window.jQuery) {
  48. $.ajax({
  49. url: '/data-export/help-report',
  50. method: 'post',
  51. data: {
  52. type: 'Error getHasRouteMatched beforeEach',
  53. extra: JSON.stringify({
  54. router: router,
  55. to: to,
  56. from: from
  57. })
  58. }
  59. })
  60. }
  61. window.top.location.replace(notFindPage);
  62. } else {
  63. next();
  64. }
  65. });
  66. if (!getHasRouteMatched(router)) {
  67. sendNotFindPageError(new Error('Error getHasRouteMatched'), {
  68. level: 'error',
  69. tags: {
  70. href: location.href
  71. },
  72. extra: {
  73. router: router
  74. }
  75. })
  76. if (window.jQuery) {
  77. $.ajax({
  78. url: '/data-export/help-report',
  79. method: 'post',
  80. data: {
  81. type: 'Error getHasRouteMatched',
  82. extra: JSON.stringify({
  83. router: router,
  84. })
  85. }
  86. })
  87. }
  88. window.top.location.replace(notFindPage);
  89. }
  90. }