修复首页来馆动效与腾讯地图跨域处理
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
lyf
2026-07-06 23:49:33 +08:00
parent 50902eca82
commit aa432bc4db
3 changed files with 171 additions and 8 deletions

View File

@@ -6,6 +6,7 @@
const TENCENT_MAP_KEY = import.meta.env.VITE_TENCENT_MAP_KEY?.trim() || ''
const TENCENT_MAP_BASE_URL = 'https://apis.map.qq.com/ws'
const TENCENT_DIRECTION_BASE_URL = `${TENCENT_MAP_BASE_URL}/direction/v1`
const isH5BrowserRuntime = typeof window !== 'undefined'
export interface TencentPoiResult {
id: string
@@ -42,6 +43,18 @@ const FALLBACK_MUSEUM_ENTRANCE: TencentPoiResult = {
distance: 0
}
const createUnavailableSearchResult = (message = 'H5 端腾讯地图 WebService 搜索需服务端代理'): TencentSearchResult => ({
status: -1,
message,
count: 0,
data: []
})
const createUnavailableRouteResult = (message = 'H5 端腾讯地图路线规划需服务端代理'): RouteResult => ({
status: -1,
message
})
/**
* 路线规划起点/终点坐标
*/
@@ -158,6 +171,11 @@ export async function planWalkingRoute(
from: RoutePoint,
to: RoutePoint
): Promise<RouteResult> {
if (isH5BrowserRuntime) {
console.info('H5 端跳过腾讯地图步行路线 WebService 请求,避免浏览器 CORS 拦截')
return createUnavailableRouteResult()
}
return new Promise((resolve) => {
uni.request({
url: `${TENCENT_DIRECTION_BASE_URL}/walking`,
@@ -231,6 +249,11 @@ export async function planDrivingRoute(
from: RoutePoint,
to: RoutePoint
): Promise<RouteResult> {
if (isH5BrowserRuntime) {
console.info('H5 端跳过腾讯地图驾车路线 WebService 请求,避免浏览器 CORS 拦截')
return createUnavailableRouteResult()
}
return new Promise((resolve) => {
uni.request({
url: `${TENCENT_DIRECTION_BASE_URL}/driving`,
@@ -304,6 +327,11 @@ export async function planTransitRoute(
from: RoutePoint,
to: RoutePoint
): Promise<RouteResult> {
if (isH5BrowserRuntime) {
console.info('H5 端跳过腾讯地图公交路线 WebService 请求,避免浏览器 CORS 拦截')
return createUnavailableRouteResult()
}
return new Promise((resolve) => {
uni.request({
url: `${TENCENT_DIRECTION_BASE_URL}/transit`,
@@ -388,6 +416,16 @@ export function searchPoi(
pageIndex: number = 1,
pageSize: number = 10
): Promise<TencentSearchResult> {
if (isH5BrowserRuntime) {
console.info('H5 端跳过腾讯地图 POI WebService 搜索,避免浏览器 CORS 拦截:', {
keyword,
region,
pageIndex,
pageSize
})
return Promise.resolve(createUnavailableSearchResult())
}
const boundary = `region(${region},0)`
return new Promise((resolve) => {
@@ -432,6 +470,10 @@ export function searchPoi(
* @returns 入口点位信息
*/
export async function searchMuseumEntrance(): Promise<TencentPoiResult | null> {
if (isH5BrowserRuntime) {
return FALLBACK_MUSEUM_ENTRANCE
}
// 先搜索博物馆
const museumResult = await searchPoi('深圳自然博物馆', '深圳市', 1, 5)
@@ -475,6 +517,16 @@ export function searchNearbyPoi(
keyword: string,
radius: number = 1000
): Promise<TencentSearchResult> {
if (isH5BrowserRuntime) {
console.info('H5 端跳过腾讯地图周边 WebService 搜索,避免浏览器 CORS 拦截:', {
lat,
lng,
keyword,
radius
})
return Promise.resolve(createUnavailableSearchResult())
}
const boundary = `nearby(${lat},${lng},${radius})`
return new Promise((resolve) => {