123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /**
- * 补全URL地址
- * @param link
- * @param prefix
- * @returns {string|*}
- */
- const URL_ORIGIN = process.env.VUE_APP_BASE_SITE || location.origin
- export function completeURLPrefix (link, prefix = URL_ORIGIN) {
- const hasPrefix = /^http(s)?:\/\//.test(link)
- const hasSign = /^\//.test(link)
- if (!hasPrefix) {
- return prefix + (hasSign ? '' : '/') + link
- } else {
- return link
- }
- }
- class RouterUtils {
- /**
- * 初始化工具
- * @param router - 基座 router
- * @param subApps - 子应用基础配置数据
- */
- constructor ({ router, subApps }) {
- this._router = router
- this._subApps = subApps
- }
- /**
- * 同 currentRoute
- * @returns {any}
- */
- currentRoute () {
- return this._router.currentRoute
- }
- /**
- * 打开应用、页面
- * @param route
- * @param {string} route.link - 地址
- * @param {string} [route.appType=outer] - 类型 outer href打开外部链接, iframe 打开内部iframe, qiankun 内部qiankun加载
- * @param {string} [route.openType] - 优先使用,可覆盖 options 参数, _blank => open, replace => replace
- * @param {string} [route.appName] - *注意:提供该参数时会从配置的 subApps中,根据 appName 读取 routeBase 拼接
- * @param options
- * @param {boolean} [options.open=false] - 是否使用新窗口打开 window.open
- * @param {boolean} [options.replace=false] - 是否使用 replace
- */
- open ({ route = {}, options = {} }) {
- const { appType = 'outer', openType = '' } = route
- const { open = false, replace = false } = options
- const toRoute = this.getRoute(route)
- const routePath = this._getLink(toRoute, false)
- const fullLink = completeURLPrefix(routePath)
- const canOpen = openType === '_blank' || open
- const canReplace = openType === 'replace' || replace
- if (canOpen) {
- return window.open(fullLink)
- }
- if (appType === 'outer') {
- if (canReplace) {
- location.replace(fullLink)
- } else {
- location.href = fullLink
- }
- } else {
- if (replace) {
- this._router.replace({ path: routePath })
- } else {
- this._router.push({ path: routePath })
- }
- }
- }
- /**
- * 刷新当前应用、页面 (待优化)
- */
- reload () {
- location.reload()
- }
- _getLink (route, needBase = false) {
- return decodeURIComponent(needBase ? route.href : route.route.fullPath)
- }
- getRoute (route = {}) {
- const { link = '', appType = '', appName = '' } = route
- let toRoute = {}
- switch (appType) {
- case 'qiankun': {
- toRoute = this.getAppRoute({ link, appName })
- break
- }
- case 'iframe': {
- toRoute = this.getPageRoute({ link, appName })
- break
- }
- default: {
- toRoute = {
- href: completeURLPrefix(link),
- route: {
- fullPath: link
- }
- }
- break
- }
- }
- return toRoute
- }
- getAppRoute ({ appName = '', link = '' }) {
- const appInfo = this._getAppInfo(appName)
- const appBaseRoute = appInfo?.qiankun?.rule || ''
- return this._router.resolve({
- name: 'app',
- hash: appBaseRoute + link
- })
- }
- getPageRoute ({ appName = '', link = '' }) {
- const appInfo = this._getAppInfo(appName)
- const appBaseRoute = appInfo?.iframe?.rule || ''
- return this._router.resolve({
- name: 'page',
- query: {
- link: completeURLPrefix(appBaseRoute + link)
- }
- })
- }
- _getAppInfo (appName = '') {
- return this._subApps[appName] || {}
- }
- }
- export default RouterUtils
|