Files
frontend-miniapp/src/services/WechatOpenLocationService.ts
lyf f9fefa11ef
Some checks failed
CI / verify (push) Has been cancelled
修复小程序内来馆地图导航
2026-07-11 17:24:45 +08:00

41 lines
1.2 KiB
TypeScript

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' })
}
})