liujie
2026-09-17 819124be8f16794d409a67f58d5257aef37d9b64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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')
}