function normalizeUrl(url) {
|
return url ? String(url) : ''
|
}
|
|
function isSinglePageH5Target(url) {
|
// #ifdef H5
|
const routes = typeof window !== 'undefined' ? window.__uniRoutes : null
|
const target = normalizeUrl(url).replace(/^\//, '')
|
if (Array.isArray(routes) && routes.length <= 1) {
|
const current = (routes[0] && routes[0].path) || ''
|
return target && target !== current
|
}
|
// #endif
|
return false
|
}
|
|
export function routeTo(url, type = 'navigateTo') {
|
const targetUrl = normalizeUrl(url)
|
if (!targetUrl) {
|
return
|
}
|
|
if (isSinglePageH5Target(targetUrl)) {
|
uni.showToast({
|
title: '请停止后重新运行项目',
|
icon: 'none'
|
})
|
return
|
}
|
|
const navigate = uni[type] || uni.navigateTo
|
try {
|
navigate({
|
url: targetUrl,
|
fail() {
|
uni.showToast({
|
title: '页面未注册,请重新运行项目',
|
icon: 'none'
|
})
|
}
|
})
|
} catch (error) {
|
uni.showToast({
|
title: '页面未注册,请重新运行项目',
|
icon: 'none'
|
})
|
}
|
}
|
|
export function backOrHome(homeUrl = '/pages/workbench/index') {
|
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
|
if (pages.length > 1) {
|
uni.navigateBack()
|
return
|
}
|
routeTo(homeUrl, 'reLaunch')
|
}
|