fix:bug解决

This commit is contained in:
李伊帆
2026-10-28 10:36:39 +08:00
parent c369127166
commit 91bca314ba
57 changed files with 2839 additions and 384 deletions

View File

@@ -66,9 +66,18 @@ function isAmapOk(status?: string | number): boolean {
return String(status) === '1'
}
function parseAmapLocation(location?: string): { longitude: number; latitude: number } | null {
if (!location || location === '[]') return null
const [lngText, latText] = location.split(',')
/** 高德部分字段在 JSON 里会是 [] 而非字符串,需统一转成文本再解析 */
function amapFieldText(value: unknown): string {
if (value == null) return ''
if (Array.isArray(value)) return ''
const text = String(value).trim()
return text === '[]' ? '' : text
}
function parseAmapLocation(location?: unknown): { longitude: number; latitude: number } | null {
const text = amapFieldText(location)
if (!text) return null
const [lngText, latText] = text.split(',')
const longitude = Number(lngText)
const latitude = Number(latText)
if (!Number.isFinite(longitude) || !Number.isFinite(latitude)) return null
@@ -116,12 +125,15 @@ function mapPois(pois: AmapPoi[] | undefined): MapLocationPoint[] {
if (!Array.isArray(pois)) return []
const results: MapLocationPoint[] = []
for (const poi of pois) {
const name = poi.name?.trim()
const name = amapFieldText(poi.name)
if (!name) continue
const coords = parseAmapLocation(poi.location)
if (!coords) continue
const region = [poi.pname, poi.cityname, poi.adname].filter(Boolean).join('')
const address = poi.address?.trim() || region || name
const region = [poi.pname, poi.cityname, poi.adname]
.map((part) => amapFieldText(part))
.filter(Boolean)
.join('')
const address = amapFieldText(poi.address) || region || name
results.push({
name,
address,
@@ -136,11 +148,11 @@ function mapInputTips(body: AmapInputTipsResponse | null): MapLocationPoint[] {
if (!body || !isAmapOk(body.status) || !Array.isArray(body.tips)) return []
const results: MapLocationPoint[] = []
for (const tip of body.tips) {
const name = tip.name?.trim()
const name = amapFieldText(tip.name)
if (!name) continue
const coords = parseAmapLocation(tip.location)
if (!coords) continue
const address = tip.address?.trim() || tip.district?.trim() || name
const address = amapFieldText(tip.address) || amapFieldText(tip.district) || name
results.push({
name,
address,
@@ -264,7 +276,8 @@ export async function searchAmapPlaces(
}
if (city) {
textData.city = city
textData.citylimit = 'true'
// 仅作范围偏好,避免严格 citylimit 导致模糊词搜不到
textData.citylimit = 'false'
}
const [aroundBody, tipsBody, textBody] = await Promise.all([
@@ -293,6 +306,29 @@ export async function searchAmapPlaces(
return { places }
}
if (city) {
const [fallbackTips, fallbackText] = await Promise.all([
requestAmap<AmapInputTipsResponse>('/v3/assistant/inputtips', {
keywords: trimmed,
datatype: 'all',
...(location ? { location } : {}),
}),
requestAmap<AmapPlaceSearchResponse>('/v3/place/text', {
keywords: trimmed,
offset: '20',
page: '1',
extensions: 'base',
}),
])
const fallbackPlaces = dedupePlaces([
...mapInputTips(fallbackTips),
...mapPois(fallbackText?.pois),
])
if (fallbackPlaces.length) {
return { places: fallbackPlaces }
}
}
const failed = [aroundBody, tipsBody, textBody].find((item) => item && !isAmapOk(item.status))
if (failed) {
return { places: [], error: mapAmapError(failed) }