longz
2026-05-28 23fe797f9dded6d7ec9ecbcf760f764374dea8e0
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
export function toAbsoluteH5Url(path: string) {
  // #ifdef H5
  const base = new URL(window.location.href)
  base.searchParams.delete('code')
  base.searchParams.delete('state')
  base.hash = path.startsWith('#') ? path : `#${path.startsWith('/') ? path : `/${path}`}`
  return base.toString()
  // #endif
  // #ifndef H5
  return path
  // #endif
}
 
export async function shareH5Link(path: string, title = '访客码分享') {
  const url = toAbsoluteH5Url(path)
  // #ifdef H5
  const navigatorWithShare = navigator as Navigator & {
    share?: (data: { title?: string, url?: string }) => Promise<void>
  }
  if (typeof navigatorWithShare.share === 'function') {
    await navigatorWithShare.share({ title, url })
    return
  }
  // #endif
  await new Promise<void>((resolve, reject) => {
    uni.setClipboardData({
      data: url,
      success: () => resolve(),
      fail: () => reject(new Error('复制分享链接失败')),
    })
  })
  uni.showToast({ title: '分享链接已复制', icon: 'none' })
}