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:
@@ -59,10 +59,9 @@ WX_PAY_NOTIFY_URL=https://api.dukanghaoke.com/api/v1/callbacks/wechat/pay
|
||||
# 企业微信智能机器人总开关(Bot 实例在 HQ「企微机器人」模块创建)
|
||||
WECOM_AIBOT_ENABLED=false
|
||||
|
||||
# 腾讯位置服务(地理编码 / 逆地理 / 地图选点组件)
|
||||
# 控制台须:1) 开启 WebServiceAPI(否则选点搜不到附近列表)
|
||||
# 2) WebService 域名白名单加入 apis.map.qq.com
|
||||
# 3) 浏览器 Key 按管理端 / 合伙人 H5 域名限制(可选)
|
||||
# 腾讯位置服务(地理编码 / 逆地理 / 地点搜索选点,服务端调用)
|
||||
# 控制台须开启 WebServiceAPI;服务端调用建议 Key 不设域名白名单,或改用 IP 白名单
|
||||
# (浏览器内嵌官方选点组件已弃用,避免 mapapi.qq.com / formatted_addresses 崩溃)
|
||||
TENCENT_LBS_KEY=
|
||||
|
||||
# 阿里云 OSS(ali-oss@6.x;凭证齐全时直传,缺失则服务端报错)
|
||||
|
||||
@@ -107,7 +107,7 @@ export const SYSTEM_CONFIG_FIELDS: SystemConfigFieldMeta[] = [
|
||||
{ key: 'OSS_MAX_UPLOAD_BYTES', label: '单文件上限(字节)', group: G.oss, type: 'number', requiresRestart: false },
|
||||
|
||||
{ key: 'USER_H5_URL', label: 'C 端 H5 落地页', group: G.app, type: 'string', requiresRestart: false },
|
||||
{ key: 'TENCENT_LBS_KEY', label: '腾讯位置服务 Key(须开 WebServiceAPI;白名单含 apis.map.qq.com)', group: G.app, type: 'password', secret: true, requiresRestart: false },
|
||||
{ key: 'TENCENT_LBS_KEY', label: '腾讯位置服务 Key(须开 WebServiceAPI;服务端地点搜索/地理编码)', group: G.app, type: 'password', secret: true, requiresRestart: false },
|
||||
|
||||
{ key: 'DEPLOY_WEBHOOK_URL', label: '发布 Webhook URL', group: G.deploy, type: 'string', requiresRestart: false },
|
||||
{ key: 'DEPLOY_WEBHOOK_SECRET', label: '发布 Webhook Secret', group: G.deploy, type: 'password', secret: true, requiresRestart: false },
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export class ClientConfigController {
|
||||
mockSms: cfg.mockSms,
|
||||
mockWechat: cfg.mockWechat,
|
||||
wxAuthorize: cfg.wxAuthorize,
|
||||
/** 浏览器地图选点用;建议在腾讯控制台按域名限制 Key */
|
||||
/** 可选暴露;选点已改为服务端 /common/lbs,前端可不依赖此字段 */
|
||||
tencentLbsKey: cfg.tencentLbsKey || undefined,
|
||||
miniHome: {
|
||||
banners: parseMiniHomeBanners(env.MINI_HOME_BANNERS),
|
||||
|
||||
@@ -14,6 +14,7 @@ import { TicketController } from './ticket.controller';
|
||||
import { ThirdPartyLogController } from './third-party-log.controller';
|
||||
import { WechatController } from './wechat.controller';
|
||||
import { ClientConfigController } from './client-config.controller';
|
||||
import { LbsController } from './lbs.controller';
|
||||
import { WechatLocationService } from './wechat-location.service';
|
||||
|
||||
@Module({
|
||||
@@ -25,6 +26,7 @@ import { WechatLocationService } from './wechat-location.service';
|
||||
ThirdPartyLogController,
|
||||
WechatController,
|
||||
ClientConfigController,
|
||||
LbsController,
|
||||
],
|
||||
providers: [
|
||||
ResourceService,
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Controller,
|
||||
Get,
|
||||
Query,
|
||||
ServiceUnavailableException,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { TencentLbsProvider } from '../../integrations/map/tencent-lbs.provider';
|
||||
|
||||
function parseCoord(raw: string | undefined, label: string): number | undefined {
|
||||
if (raw == null || raw === '') return undefined;
|
||||
const n = Number(raw);
|
||||
if (!Number.isFinite(n)) throw new BadRequestException(`${label}无效`);
|
||||
return n;
|
||||
}
|
||||
|
||||
@Controller('common/lbs')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class LbsController {
|
||||
constructor(private readonly tencentLbs: TencentLbsProvider) {}
|
||||
|
||||
@Get('suggest')
|
||||
async suggest(
|
||||
@Query('keyword') keyword?: string,
|
||||
@Query('region') region?: string,
|
||||
@Query('lat') lat?: string,
|
||||
@Query('lng') lng?: string,
|
||||
) {
|
||||
if (!this.tencentLbs.isEnabled()) {
|
||||
throw new ServiceUnavailableException('未配置腾讯位置服务 Key(TENCENT_LBS_KEY)');
|
||||
}
|
||||
const q = (keyword ?? '').trim();
|
||||
if (!q) return { items: [] };
|
||||
const latitude = parseCoord(lat, '纬度');
|
||||
const longitude = parseCoord(lng, '经度');
|
||||
const result = await this.tencentLbs.suggestPlaces(q, {
|
||||
region: region?.trim() || undefined,
|
||||
latitude,
|
||||
longitude,
|
||||
});
|
||||
if (result.error && !result.items.length) {
|
||||
throw new BadRequestException(result.error);
|
||||
}
|
||||
return { items: result.items };
|
||||
}
|
||||
|
||||
@Get('nearby')
|
||||
async nearby(@Query('lat') lat?: string, @Query('lng') lng?: string, @Query('radius') radius?: string) {
|
||||
if (!this.tencentLbs.isEnabled()) {
|
||||
throw new ServiceUnavailableException('未配置腾讯位置服务 Key(TENCENT_LBS_KEY)');
|
||||
}
|
||||
const latitude = parseCoord(lat, '纬度');
|
||||
const longitude = parseCoord(lng, '经度');
|
||||
if (latitude == null || longitude == null) {
|
||||
throw new BadRequestException('请提供 lat、lng');
|
||||
}
|
||||
const r = radius != null && radius !== '' ? Number(radius) : 1000;
|
||||
const result = await this.tencentLbs.exploreNearby(latitude, longitude, Number.isFinite(r) ? r : 1000);
|
||||
if (result.error && !result.items.length) {
|
||||
throw new BadRequestException(result.error);
|
||||
}
|
||||
return { items: result.items };
|
||||
}
|
||||
|
||||
@Get('reverse')
|
||||
async reverse(@Query('lat') lat?: string, @Query('lng') lng?: string) {
|
||||
if (!this.tencentLbs.isEnabled()) {
|
||||
throw new ServiceUnavailableException('未配置腾讯位置服务 Key(TENCENT_LBS_KEY)');
|
||||
}
|
||||
const latitude = parseCoord(lat, '纬度');
|
||||
const longitude = parseCoord(lng, '经度');
|
||||
if (latitude == null || longitude == null) {
|
||||
throw new BadRequestException('请提供 lat、lng');
|
||||
}
|
||||
const result = await this.tencentLbs.reverseGeocodeDetail(latitude, longitude);
|
||||
if (!result.item) {
|
||||
throw new BadRequestException(result.error || '逆地理编码失败');
|
||||
}
|
||||
return {
|
||||
latitude: result.item.latitude,
|
||||
longitude: result.item.longitude,
|
||||
address: result.item.address,
|
||||
name: result.item.name,
|
||||
province: result.item.province,
|
||||
city: result.item.city,
|
||||
district: result.item.district,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user