fix(admin,partner): replace Tencent iframe picker with server LBS search
CI / verify (pull_request) Has been cancelled
CI / verify (pull_request) Has been cancelled
Avoid locpicker formatted_addresses crash; proxy suggest/nearby/reverse via /common/lbs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -16,10 +16,38 @@ export type GeocodeAddressResult = {
|
||||
logId: bigint;
|
||||
};
|
||||
|
||||
export type PlaceSuggestItem = {
|
||||
id: string;
|
||||
title: string;
|
||||
address: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
city?: string;
|
||||
};
|
||||
|
||||
export type ReverseGeocodeDetailResult = {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
address: string;
|
||||
name?: string;
|
||||
province: string;
|
||||
city: string;
|
||||
district: string;
|
||||
logId: bigint;
|
||||
};
|
||||
|
||||
function normalizeCityName(name: string) {
|
||||
return name.replace(/市$/, '').trim();
|
||||
}
|
||||
|
||||
type TencentPlaceRow = {
|
||||
id?: string;
|
||||
title?: string;
|
||||
address?: string;
|
||||
city?: string;
|
||||
location?: { lat?: number; lng?: number };
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class TencentLbsProvider {
|
||||
private readonly logger = new Logger(TencentLbsProvider.name);
|
||||
@@ -203,4 +231,202 @@ export class TencentLbsProvider {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private mapPlaceRows(rows: TencentPlaceRow[] | undefined): PlaceSuggestItem[] {
|
||||
if (!rows?.length) return [];
|
||||
const out: PlaceSuggestItem[] = [];
|
||||
for (const row of rows) {
|
||||
const lat = Number(row.location?.lat);
|
||||
const lng = Number(row.location?.lng);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lng)) continue;
|
||||
const title = (row.title || '').trim();
|
||||
const address = (row.address || '').trim();
|
||||
if (!title && !address) continue;
|
||||
out.push({
|
||||
id: String(row.id || `${lat},${lng}`),
|
||||
title: title || address,
|
||||
address: address || title,
|
||||
latitude: lat,
|
||||
longitude: lng,
|
||||
city: row.city?.trim() || undefined,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 关键词输入提示(地点搜索) */
|
||||
async suggestPlaces(
|
||||
keyword: string,
|
||||
options?: { region?: string; latitude?: number; longitude?: number },
|
||||
): Promise<{ items: PlaceSuggestItem[]; error?: string }> {
|
||||
const trimmed = keyword.trim();
|
||||
if (!trimmed) return { items: [] };
|
||||
if (!this.isEnabled()) {
|
||||
return { items: [], error: 'TENCENT_LBS_KEY 未配置' };
|
||||
}
|
||||
|
||||
const url = new URL('https://apis.map.qq.com/ws/place/v1/suggestion');
|
||||
url.searchParams.set('keyword', trimmed.slice(0, 64));
|
||||
url.searchParams.set('key', this.getLbsKey());
|
||||
url.searchParams.set('policy', '1');
|
||||
url.searchParams.set('page_index', '1');
|
||||
url.searchParams.set('page_size', '20');
|
||||
const region = options?.region?.trim();
|
||||
if (region) url.searchParams.set('region', region);
|
||||
if (
|
||||
options?.latitude != null &&
|
||||
options?.longitude != null &&
|
||||
Number.isFinite(options.latitude) &&
|
||||
Number.isFinite(options.longitude)
|
||||
) {
|
||||
url.searchParams.set('location', `${options.latitude},${options.longitude}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(url.toString());
|
||||
const data = (await res.json()) as {
|
||||
status?: number;
|
||||
message?: string;
|
||||
data?: TencentPlaceRow[];
|
||||
};
|
||||
if (data.status !== 0) {
|
||||
this.logger.warn(`Tencent LBS suggest failed: ${data.message ?? data.status}`);
|
||||
return { items: [], error: data.message || '地点搜索失败' };
|
||||
}
|
||||
return { items: this.mapPlaceRows(data.data) };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
this.logger.error(`Tencent LBS suggest failed: ${message}`);
|
||||
return { items: [], error: message };
|
||||
}
|
||||
}
|
||||
|
||||
/** 周边地点(打开选点时预填附近列表) */
|
||||
async exploreNearby(
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
radiusMeters = 1000,
|
||||
): Promise<{ items: PlaceSuggestItem[]; error?: string }> {
|
||||
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) {
|
||||
return { items: [], error: '经纬度无效' };
|
||||
}
|
||||
if (!this.isEnabled()) {
|
||||
return { items: [], error: 'TENCENT_LBS_KEY 未配置' };
|
||||
}
|
||||
|
||||
const radius = Math.min(5000, Math.max(200, Math.round(radiusMeters)));
|
||||
const url = new URL('https://apis.map.qq.com/ws/place/v1/explore');
|
||||
url.searchParams.set('boundary', `nearby(${latitude},${longitude},${radius})`);
|
||||
url.searchParams.set('policy', '1');
|
||||
url.searchParams.set('page_size', '20');
|
||||
url.searchParams.set('key', this.getLbsKey());
|
||||
|
||||
try {
|
||||
const res = await fetch(url.toString());
|
||||
const data = (await res.json()) as {
|
||||
status?: number;
|
||||
message?: string;
|
||||
data?: TencentPlaceRow[];
|
||||
};
|
||||
if (data.status !== 0) {
|
||||
this.logger.warn(`Tencent LBS explore failed: ${data.message ?? data.status}`);
|
||||
return { items: [], error: data.message || '周边检索失败' };
|
||||
}
|
||||
return { items: this.mapPlaceRows(data.data) };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
this.logger.error(`Tencent LBS explore failed: ${message}`);
|
||||
return { items: [], error: message };
|
||||
}
|
||||
}
|
||||
|
||||
/** 逆地理(含地址文案,供选点回填) */
|
||||
async reverseGeocodeDetail(
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
): Promise<{ item: ReverseGeocodeDetailResult | null; error?: string }> {
|
||||
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) {
|
||||
return { item: null, error: '经纬度无效' };
|
||||
}
|
||||
if (!this.isEnabled()) {
|
||||
return { item: null, error: 'TENCENT_LBS_KEY 未配置' };
|
||||
}
|
||||
|
||||
const url = new URL('https://apis.map.qq.com/ws/geocoder/v1/');
|
||||
url.searchParams.set('location', `${latitude},${longitude}`);
|
||||
url.searchParams.set('key', this.getLbsKey());
|
||||
url.searchParams.set('get_poi', '1');
|
||||
|
||||
try {
|
||||
const res = await fetch(url.toString());
|
||||
const data = (await res.json()) as {
|
||||
status?: number;
|
||||
message?: string;
|
||||
result?: {
|
||||
address?: string;
|
||||
formatted_addresses?: { recommend?: string; rough?: string };
|
||||
address_component?: {
|
||||
province?: string;
|
||||
city?: string;
|
||||
district?: string;
|
||||
street?: string;
|
||||
street_number?: string;
|
||||
};
|
||||
ad_info?: {
|
||||
province?: string;
|
||||
city?: string;
|
||||
district?: string;
|
||||
};
|
||||
pois?: Array<{ title?: string; address?: string }>;
|
||||
};
|
||||
};
|
||||
if (data.status !== 0 || !data.result) {
|
||||
return { item: null, error: data.message || '逆地理编码失败' };
|
||||
}
|
||||
const result = data.result;
|
||||
const ad = result.ad_info ?? result.address_component;
|
||||
const province = ad?.province ?? '';
|
||||
const city = normalizeCityName(ad?.city ?? '');
|
||||
const district = ad?.district ?? '';
|
||||
const recommend =
|
||||
result.formatted_addresses?.recommend?.trim() ||
|
||||
result.formatted_addresses?.rough?.trim() ||
|
||||
result.address?.trim() ||
|
||||
'';
|
||||
const poiTitle = result.pois?.[0]?.title?.trim();
|
||||
if (!recommend && !poiTitle) {
|
||||
return { item: null, error: '未解析到地址' };
|
||||
}
|
||||
const log = await this.prisma.logThirdParty.create({
|
||||
data: {
|
||||
provider: 'WECHAT_MAP',
|
||||
scene: 'REVERSE_GEOCODE',
|
||||
requestUrl: 'https://apis.map.qq.com/ws/geocoder/v1/',
|
||||
requestBody: { latitude, longitude, detail: true },
|
||||
responseBody: {
|
||||
status: data.status,
|
||||
address: recommend,
|
||||
city,
|
||||
},
|
||||
status: 'SUCCESS',
|
||||
},
|
||||
});
|
||||
return {
|
||||
item: {
|
||||
latitude,
|
||||
longitude,
|
||||
address: recommend || poiTitle || '',
|
||||
name: poiTitle || recommend || undefined,
|
||||
province,
|
||||
city,
|
||||
district,
|
||||
logId: log.id,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
this.logger.error(`Tencent LBS reverse detail failed: ${message}`);
|
||||
return { item: null, error: message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user