@@ -3,6 +3,11 @@ import {
|
||||
isEmbeddedInWechatMiniProgram,
|
||||
isNativeWechatMiniProgram
|
||||
} from '@/utils/hostEnvironment'
|
||||
import {
|
||||
buildWechatOpenLocationPageUrl,
|
||||
validateWechatOpenLocationTarget,
|
||||
type WechatOpenLocationTarget
|
||||
} from '@/utils/wechatOpenLocationProtocol'
|
||||
|
||||
export type ThirdPartyMapProvider = 'amap' | 'baidu' | 'tencent'
|
||||
|
||||
@@ -11,12 +16,7 @@ export interface ThirdPartyMapSearchTarget {
|
||||
region: string
|
||||
}
|
||||
|
||||
export interface WechatMiniProgramLocationTarget {
|
||||
latitude: number
|
||||
longitude: number
|
||||
name: string
|
||||
address?: string
|
||||
}
|
||||
export type WechatMiniProgramLocationTarget = WechatOpenLocationTarget
|
||||
|
||||
interface WechatMiniProgramBridge {
|
||||
getEnv?: (callback: (result: { miniprogram?: boolean }) => void) => void
|
||||
@@ -27,7 +27,17 @@ interface WechatMiniProgramBridge {
|
||||
}) => void
|
||||
}
|
||||
|
||||
let wechatJsSdkLoadPromise: Promise<boolean> | null = null
|
||||
export type WechatMiniProgramLocationOpenResult =
|
||||
| { status: 'opened' }
|
||||
| { status: 'not-embedded' }
|
||||
| { status: 'failed'; reason: 'invalid-target' | 'bridge-unavailable' | 'not-in-mini-program' | 'navigation-failed' }
|
||||
|
||||
const BRIDGE_WAIT_TIMEOUT = 2500
|
||||
const BRIDGE_POLL_INTERVAL = 50
|
||||
const SDK_SCRIPT_LOAD_TIMEOUT = 10000
|
||||
const GET_ENV_TIMEOUT = 1500
|
||||
const NAVIGATION_TIMEOUT = 3000
|
||||
let miniProgramNavigationPromise: Promise<WechatMiniProgramLocationOpenResult> | null = null
|
||||
|
||||
export interface ThirdPartyMapProviderOption {
|
||||
provider: ThirdPartyMapProvider
|
||||
@@ -82,18 +92,7 @@ export const openThirdPartyMapSearch = (
|
||||
|
||||
export const buildWechatMiniProgramOpenLocationUrl = (
|
||||
target: WechatMiniProgramLocationTarget
|
||||
) => {
|
||||
const params = [
|
||||
['latitude', String(target.latitude)],
|
||||
['longitude', String(target.longitude)],
|
||||
['name', target.name],
|
||||
['address', target.address || '']
|
||||
]
|
||||
.map(([key, value]) => `${key}=${encode(value)}`)
|
||||
.join('&')
|
||||
|
||||
return `/pages/open-location/index?${params}`
|
||||
}
|
||||
) => buildWechatOpenLocationPageUrl(target)
|
||||
|
||||
const getWechatMiniProgramBridge = (): WechatMiniProgramBridge | null => {
|
||||
if (typeof window === 'undefined') return null
|
||||
@@ -105,29 +104,35 @@ const getWechatMiniProgramBridge = (): WechatMiniProgramBridge | null => {
|
||||
}).wx?.miniProgram || null
|
||||
}
|
||||
|
||||
const loadWechatJsSdk = () => {
|
||||
if (typeof document === 'undefined') return Promise.resolve(false)
|
||||
if (getWechatMiniProgramBridge()) return Promise.resolve(true)
|
||||
if (wechatJsSdkLoadPromise) return wechatJsSdkLoadPromise
|
||||
const ensureWechatJsSdkScript = () => {
|
||||
if (typeof document === 'undefined' || getWechatMiniProgramBridge()) return
|
||||
const existingScript = document.querySelector<HTMLScriptElement>('script[data-wechat-js-sdk="true"]')
|
||||
if (existingScript) {
|
||||
const startedAt = Number(existingScript.dataset.wechatJsSdkStartedAt || 0)
|
||||
const isExpiredLoading = existingScript.dataset.wechatJsSdkState === 'loading'
|
||||
&& Date.now() - startedAt > SDK_SCRIPT_LOAD_TIMEOUT
|
||||
if (existingScript.dataset.wechatJsSdkState !== 'error' && !isExpiredLoading) return
|
||||
existingScript.remove()
|
||||
}
|
||||
|
||||
wechatJsSdkLoadPromise = new Promise<boolean>((resolve) => {
|
||||
const existingScript = document.querySelector<HTMLScriptElement>('script[data-wechat-js-sdk="true"]')
|
||||
if (existingScript) {
|
||||
existingScript.addEventListener('load', () => resolve(Boolean(getWechatMiniProgramBridge())), { once: true })
|
||||
existingScript.addEventListener('error', () => resolve(false), { once: true })
|
||||
return
|
||||
}
|
||||
|
||||
const script = document.createElement('script')
|
||||
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
|
||||
script.async = true
|
||||
script.dataset.wechatJsSdk = 'true'
|
||||
script.onload = () => resolve(Boolean(getWechatMiniProgramBridge()))
|
||||
script.onerror = () => resolve(false)
|
||||
const script = document.createElement('script')
|
||||
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
|
||||
script.async = true
|
||||
script.dataset.wechatJsSdk = 'true'
|
||||
script.dataset.wechatJsSdkState = 'loading'
|
||||
script.dataset.wechatJsSdkStartedAt = String(Date.now())
|
||||
script.onload = () => {
|
||||
script.dataset.wechatJsSdkState = 'loaded'
|
||||
}
|
||||
script.onerror = () => {
|
||||
script.dataset.wechatJsSdkState = 'error'
|
||||
script.remove()
|
||||
}
|
||||
try {
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
|
||||
return wechatJsSdkLoadPromise
|
||||
} catch (error) {
|
||||
console.error('加载微信 JS SDK 失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const isWechatBrowser = () => (
|
||||
@@ -135,6 +140,25 @@ const isWechatBrowser = () => (
|
||||
&& /MicroMessenger/i.test(window.navigator?.userAgent || '')
|
||||
)
|
||||
|
||||
const wait = (duration: number) => new Promise<void>((resolve) => {
|
||||
setTimeout(resolve, duration)
|
||||
})
|
||||
|
||||
const waitForWechatMiniProgramBridge = async () => {
|
||||
const immediateBridge = getWechatMiniProgramBridge()
|
||||
if (immediateBridge) return immediateBridge
|
||||
|
||||
ensureWechatJsSdkScript()
|
||||
const startedAt = Date.now()
|
||||
while (Date.now() - startedAt < BRIDGE_WAIT_TIMEOUT) {
|
||||
await wait(BRIDGE_POLL_INTERVAL)
|
||||
const bridge = getWechatMiniProgramBridge()
|
||||
if (bridge) return bridge
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const resolveMiniProgramWebViewEnvironment = (
|
||||
miniProgram: WechatMiniProgramBridge
|
||||
) => {
|
||||
@@ -150,13 +174,13 @@ const resolveMiniProgramWebViewEnvironment = (
|
||||
clearTimeout(timeout)
|
||||
resolve(isMiniProgram)
|
||||
}
|
||||
const timeout = setTimeout(() => finish(false), 1500)
|
||||
const timeout = setTimeout(() => finish(isEmbeddedInWechatMiniProgram()), GET_ENV_TIMEOUT)
|
||||
|
||||
try {
|
||||
getEnv((result) => finish(Boolean(result.miniprogram)))
|
||||
getEnv((result) => finish(Boolean(result?.miniprogram) || isEmbeddedInWechatMiniProgram()))
|
||||
} catch (error) {
|
||||
console.error('识别小程序 web-view 环境失败:', error)
|
||||
finish(false)
|
||||
// 保留超时重检,让延迟注入的宿主环境标识仍有机会生效。
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -164,50 +188,127 @@ const resolveMiniProgramWebViewEnvironment = (
|
||||
const navigateToMiniProgramLocationPage = (
|
||||
miniProgram: WechatMiniProgramBridge,
|
||||
target: WechatMiniProgramLocationTarget
|
||||
) => new Promise<boolean>((resolve) => {
|
||||
) => new Promise<WechatMiniProgramLocationOpenResult>((resolve) => {
|
||||
let settled = false
|
||||
const finish = (result: WechatMiniProgramLocationOpenResult) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
clearTimeout(timeout)
|
||||
resolve(result)
|
||||
}
|
||||
const timeout = setTimeout(() => {
|
||||
uni.showToast({
|
||||
title: '宿主地图页响应超时',
|
||||
icon: 'none'
|
||||
})
|
||||
finish({ status: 'failed', reason: 'navigation-failed' })
|
||||
}, NAVIGATION_TIMEOUT)
|
||||
|
||||
if (!miniProgram.navigateTo) {
|
||||
resolve(false)
|
||||
uni.showToast({
|
||||
title: '宿主地图桥接不可用',
|
||||
icon: 'none'
|
||||
})
|
||||
finish({ status: 'failed', reason: 'bridge-unavailable' })
|
||||
return
|
||||
}
|
||||
|
||||
miniProgram.navigateTo({
|
||||
url: buildWechatMiniProgramOpenLocationUrl(target),
|
||||
success: () => resolve(true),
|
||||
fail: (error) => {
|
||||
console.error('跳转小程序地图页失败:', error)
|
||||
uni.showToast({
|
||||
title: '无法打开地图导航',
|
||||
icon: 'none'
|
||||
})
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
try {
|
||||
miniProgram.navigateTo({
|
||||
url: buildWechatMiniProgramOpenLocationUrl(target),
|
||||
success: () => finish({ status: 'opened' }),
|
||||
fail: (error) => {
|
||||
if (settled) return
|
||||
console.error('跳转小程序地图页失败:', error)
|
||||
const errorMessage = typeof error === 'object' && error && 'errMsg' in error
|
||||
? String(error.errMsg)
|
||||
: ''
|
||||
uni.showToast({
|
||||
title: /page.*not found|not found.*page/i.test(errorMessage)
|
||||
? '宿主小程序未配置地图页'
|
||||
: '宿主地图页跳转失败',
|
||||
icon: 'none'
|
||||
})
|
||||
finish({ status: 'failed', reason: 'navigation-failed' })
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('调用小程序地图桥接失败:', error)
|
||||
uni.showToast({
|
||||
title: '宿主地图桥接调用失败',
|
||||
icon: 'none'
|
||||
})
|
||||
finish({ status: 'failed', reason: 'navigation-failed' })
|
||||
}
|
||||
})
|
||||
|
||||
export const openWechatMiniProgramLocation = async (
|
||||
const openWechatMiniProgramLocationOnce = async (
|
||||
target: WechatMiniProgramLocationTarget
|
||||
) => {
|
||||
if (isNativeWechatMiniProgram()) {
|
||||
return openExternalNavigation(target)
|
||||
): Promise<WechatMiniProgramLocationOpenResult> => {
|
||||
const validation = validateWechatOpenLocationTarget(target)
|
||||
if (!validation.ok) {
|
||||
uni.showToast({
|
||||
title: validation.message,
|
||||
icon: 'none'
|
||||
})
|
||||
return { status: 'failed', reason: 'invalid-target' }
|
||||
}
|
||||
|
||||
const synchronouslyEmbedded = isEmbeddedInWechatMiniProgram()
|
||||
if (!synchronouslyEmbedded && !isWechatBrowser()) return false
|
||||
|
||||
await loadWechatJsSdk()
|
||||
const miniProgram = getWechatMiniProgramBridge()
|
||||
if (!miniProgram?.navigateTo) {
|
||||
if (synchronouslyEmbedded) {
|
||||
if (isNativeWechatMiniProgram()) {
|
||||
try {
|
||||
const opened = await openExternalNavigation(validation.value)
|
||||
return opened
|
||||
? { status: 'opened' }
|
||||
: { status: 'failed', reason: 'navigation-failed' }
|
||||
} catch (error) {
|
||||
console.error('原生小程序地图能力调用失败:', error)
|
||||
uni.showToast({
|
||||
title: '请在小程序内打开地图',
|
||||
title: '无法打开微信地图',
|
||||
icon: 'none'
|
||||
})
|
||||
return { status: 'failed', reason: 'navigation-failed' }
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if (!isEmbeddedInWechatMiniProgram() && !isWechatBrowser()) {
|
||||
return { status: 'not-embedded' }
|
||||
}
|
||||
|
||||
const miniProgram = await waitForWechatMiniProgramBridge()
|
||||
if (!miniProgram?.navigateTo) {
|
||||
if (isEmbeddedInWechatMiniProgram()) {
|
||||
uni.showToast({
|
||||
title: '小程序地图桥接加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
return { status: 'failed', reason: 'bridge-unavailable' }
|
||||
}
|
||||
return { status: 'not-embedded' }
|
||||
}
|
||||
|
||||
const isMiniProgramWebView = await resolveMiniProgramWebViewEnvironment(miniProgram)
|
||||
if (!isMiniProgramWebView) return false
|
||||
if (!isMiniProgramWebView) {
|
||||
if (isEmbeddedInWechatMiniProgram()) {
|
||||
uni.showToast({
|
||||
title: '当前页面未连接宿主小程序',
|
||||
icon: 'none'
|
||||
})
|
||||
return { status: 'failed', reason: 'not-in-mini-program' }
|
||||
}
|
||||
return { status: 'not-embedded' }
|
||||
}
|
||||
|
||||
return navigateToMiniProgramLocationPage(miniProgram, target)
|
||||
return navigateToMiniProgramLocationPage(miniProgram, validation.value)
|
||||
}
|
||||
|
||||
export const openWechatMiniProgramLocation = (
|
||||
target: WechatMiniProgramLocationTarget
|
||||
) => {
|
||||
if (miniProgramNavigationPromise) return miniProgramNavigationPromise
|
||||
|
||||
miniProgramNavigationPromise = openWechatMiniProgramLocationOnce(target)
|
||||
.finally(() => {
|
||||
miniProgramNavigationPromise = null
|
||||
})
|
||||
return miniProgramNavigationPromise
|
||||
}
|
||||
|
||||
40
src/services/WechatOpenLocationService.ts
Normal file
40
src/services/WechatOpenLocationService.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { WechatOpenLocationTarget } from '@/utils/wechatOpenLocationProtocol'
|
||||
|
||||
export type WechatOpenLocationResult =
|
||||
| { status: 'opened' }
|
||||
| { status: 'failed'; reason: 'api-failed' | 'timeout' }
|
||||
|
||||
const OPEN_LOCATION_TIMEOUT = 5000
|
||||
|
||||
export const openWechatHostLocation = (
|
||||
target: WechatOpenLocationTarget
|
||||
) => new Promise<WechatOpenLocationResult>((resolve) => {
|
||||
let settled = false
|
||||
const finish = (result: WechatOpenLocationResult) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
clearTimeout(timeout)
|
||||
resolve(result)
|
||||
}
|
||||
const timeout = setTimeout(() => {
|
||||
finish({ status: 'failed', reason: 'timeout' })
|
||||
}, OPEN_LOCATION_TIMEOUT)
|
||||
|
||||
try {
|
||||
uni.openLocation({
|
||||
latitude: target.latitude,
|
||||
longitude: target.longitude,
|
||||
name: target.name,
|
||||
address: target.address || '',
|
||||
scale: 18,
|
||||
success: () => finish({ status: 'opened' }),
|
||||
fail: (error) => {
|
||||
console.error('打开地图位置失败:', error)
|
||||
finish({ status: 'failed', reason: 'api-failed' })
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('调用微信地图能力失败:', error)
|
||||
finish({ status: 'failed', reason: 'api-failed' })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user