fix: 配送地图地理编码改为后端接口
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 文件职责:封装配送页地址地理编码能力。
|
||||
* 1. 通过 TenantApi 后端代理调用腾讯地图 WebService,避免前端暴露 SK。
|
||||
* 2. 内置结果缓存,减少重复请求与抖动。
|
||||
*/
|
||||
import type { LngLatTuple } from './delivery-page/geojson';
|
||||
|
||||
import { geocodeStoreDeliveryAddressApi } from '#/api/store-delivery';
|
||||
|
||||
const geocodeCache = new Map<string, LngLatTuple>();
|
||||
const geocodeFailedAddressSet = new Set<string>();
|
||||
|
||||
function toTuple(
|
||||
latitude: null | number,
|
||||
longitude: null | number,
|
||||
): LngLatTuple | null {
|
||||
const lng = Number(longitude);
|
||||
const lat = Number(latitude);
|
||||
if (!Number.isFinite(lng) || !Number.isFinite(lat)) return null;
|
||||
if (lng < -180 || lng > 180 || lat < -90 || lat > 90) return null;
|
||||
if (lng === 0 && lat === 0) return null;
|
||||
return [lng, lat];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将地址解析为经纬度中心点。
|
||||
*/
|
||||
export async function geocodeAddressToLngLat(rawAddress: string) {
|
||||
const address = rawAddress?.trim() ?? '';
|
||||
if (!address) return null;
|
||||
|
||||
if (geocodeCache.has(address)) {
|
||||
const cached = geocodeCache.get(address);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
if (geocodeFailedAddressSet.has(address)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await geocodeStoreDeliveryAddressApi(address);
|
||||
const parsed = toTuple(
|
||||
response?.latitude ?? null,
|
||||
response?.longitude ?? null,
|
||||
);
|
||||
if (!parsed) {
|
||||
geocodeFailedAddressSet.add(address);
|
||||
return null;
|
||||
}
|
||||
|
||||
geocodeCache.set(address, parsed);
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
geocodeFailedAddressSet.add(address);
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn('地址地理编码失败', error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user