utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * 补全URL地址
  3. * @param link
  4. * @param prefix
  5. * @returns {string|*}
  6. */
  7. const URL_ORIGIN = process.env.VUE_APP_BASE_SITE || location.origin
  8. export function completeURLPrefix (link, prefix = URL_ORIGIN) {
  9. const hasPrefix = /^http(s)?:\/\//.test(link)
  10. const hasSign = /^\//.test(link)
  11. if (!hasPrefix) {
  12. return prefix + (hasSign ? '' : '/') + link
  13. } else {
  14. return link
  15. }
  16. }
  17. class RouterUtils {
  18. /**
  19. * 初始化工具
  20. * @param router - 基座 router
  21. * @param subApps - 子应用基础配置数据
  22. */
  23. constructor ({ router, subApps }) {
  24. this._router = router
  25. this._subApps = subApps
  26. }
  27. /**
  28. * 同 currentRoute
  29. * @returns {any}
  30. */
  31. currentRoute () {
  32. return this._router.currentRoute
  33. }
  34. /**
  35. * 打开应用、页面
  36. * @param route
  37. * @param {string} route.link - 地址
  38. * @param {string} [route.appType=outer] - 类型 outer href打开外部链接, iframe 打开内部iframe, qiankun 内部qiankun加载
  39. * @param {string} [route.openType] - 优先使用,可覆盖 options 参数, _blank => open, replace => replace
  40. * @param {string} [route.appName] - *注意:提供该参数时会从配置的 subApps中,根据 appName 读取 routeBase 拼接
  41. * @param options
  42. * @param {boolean} [options.open=false] - 是否使用新窗口打开 window.open
  43. * @param {boolean} [options.replace=false] - 是否使用 replace
  44. */
  45. open ({ route = {}, options = {} }) {
  46. const { appType = 'outer', openType = '' } = route
  47. const { open = false, replace = false } = options
  48. const toRoute = this.getRoute(route)
  49. const routePath = this._getLink(toRoute, false)
  50. const fullLink = completeURLPrefix(routePath)
  51. const canOpen = openType === '_blank' || open
  52. const canReplace = openType === 'replace' || replace
  53. if (canOpen) {
  54. return window.open(fullLink)
  55. }
  56. if (appType === 'outer') {
  57. if (canReplace) {
  58. location.replace(fullLink)
  59. } else {
  60. location.href = fullLink
  61. }
  62. } else {
  63. if (replace) {
  64. this._router.replace({ path: routePath })
  65. } else {
  66. this._router.push({ path: routePath })
  67. }
  68. }
  69. }
  70. /**
  71. * 刷新当前应用、页面 (待优化)
  72. */
  73. reload () {
  74. location.reload()
  75. }
  76. _getLink (route, needBase = false) {
  77. return decodeURIComponent(needBase ? route.href : route.route.fullPath)
  78. }
  79. getRoute (route = {}) {
  80. const { link = '', appType = '', appName = '' } = route
  81. let toRoute = {}
  82. switch (appType) {
  83. case 'qiankun': {
  84. toRoute = this.getAppRoute({ link, appName })
  85. break
  86. }
  87. case 'iframe': {
  88. toRoute = this.getPageRoute({ link, appName })
  89. break
  90. }
  91. default: {
  92. toRoute = {
  93. href: completeURLPrefix(link),
  94. route: {
  95. fullPath: link
  96. }
  97. }
  98. break
  99. }
  100. }
  101. return toRoute
  102. }
  103. getAppRoute ({ appName = '', link = '' }) {
  104. const appInfo = this._getAppInfo(appName)
  105. const appBaseRoute = appInfo?.qiankun?.rule || ''
  106. return this._router.resolve({
  107. name: 'app',
  108. hash: appBaseRoute + link
  109. })
  110. }
  111. getPageRoute ({ appName = '', link = '' }) {
  112. const appInfo = this._getAppInfo(appName)
  113. const appBaseRoute = appInfo?.iframe?.rule || ''
  114. return this._router.resolve({
  115. name: 'page',
  116. query: {
  117. link: completeURLPrefix(appBaseRoute + link)
  118. }
  119. })
  120. }
  121. _getAppInfo (appName = '') {
  122. return this._subApps[appName] || {}
  123. }
  124. }
  125. export default RouterUtils