UNPKG

@ecip/ecip-web

Version:

A magical vue admin. An out-of-box UI solution for enterprise applications. Newest development stack of vue. Lots of awesome features

86 lines (80 loc) 3.03 kB
export function urlSearchReWrite(urlSearch) { const appId = window._CONFIG['centerAuthAppId'] || window._CONFIG['appId'] // 如果 urlSearch 中没有 appId,添加 if (!urlSearch) { return appId ? '?appId=' + appId : '' } if (!urlSearch.includes('appId=') && appId) { urlSearch += '&appId=' + appId } // 如果 urlSearch 中有 token,去掉 if (urlSearch.includes('token')) { // 使用正则表达式来去掉 token 参数 urlSearch = urlSearch.replace(/[\?&]token=[^&]+/, '') // 如果去掉 token 参数后,urlSearch 开头仍然是 '&' 或者 '?',则去掉多余的符号 if (urlSearch.startsWith('&')) { urlSearch = urlSearch.substring(1) } } if (urlSearch.startsWith('&')) { urlSearch = urlSearch.substring(1) + '?' } if (!urlSearch.startsWith('?')) { urlSearch = '?' + urlSearch } return urlSearch } export function getParamFromUrl(paraName, url) { const urlObj = url.split('#/') const arrObj = urlObj[0].split('?') if (arrObj.length > 1) { const arrPara = arrObj[arrObj.length - 1].split('&') let arr for (let i = 0; i < arrPara.length; i++) { arr = arrPara[i].split('=') if (arr != null && arr[0] === paraName) { return arr[1] } } return '' } else { return '' } } export function redirectToAuthCenter() { let baseUrl = process.env.NODE_ENV === 'development' ? process.env.BASE_URL : process.env.VUE_APP_BASE_API // 去掉baseurl后面的斜杠 if (baseUrl.endsWith('/')) { baseUrl = baseUrl.substring(0, baseUrl.length - 1) } const serviceUrl = window.location.protocol + '//' + window.location.host + baseUrl + urlSearchReWrite(window.location.search) + location.hash const service = encodeURIComponent(serviceUrl) // console.debug('user.js', `${window._CONFIG['centerAuthUrl']}` + '?service=' + service) window.location.href = `${window._CONFIG['centerAuthUrl']}` + '?service=' + service } // 重写浏览器导航url地址,去掉baseurl后面的斜杠和token参数 export function urlFormat() { let baseUrl = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'staging' ? '' : process.env.VUE_APP_BASE_API let search = window.location.search // 去掉baseurl后面的斜杠 if (baseUrl.endsWith('/')) { baseUrl = baseUrl.substring(0, baseUrl.length - 1) } if (search.includes('token')) { search = urlSearchReWrite(search) } // console.log(location.hash) // if (location.hash.includes('?token=')) { // const parts = location.hash.split('?') // const queryParams = parts[1].split('&').filter(param => !param.startsWith('token=')).join('&') // location.hash = parts[0] + (queryParams ? '?' + queryParams : '') // } // console.log(location.hash) const serviceUrl = window.location.protocol + '//' + window.location.host + baseUrl + search + location.hash history.replaceState(null, null, serviceUrl) } export default { redirectToAuthCenter, urlSearchReWrite, getParamFromUrl }