From af926257b9e19ce765c7f9779d8bf54472685b8a Mon Sep 17 00:00:00 2001 From: superlishunqin <852326703@qq.com> Date: Tue, 30 Sep 2025 06:24:30 +0800 Subject: [PATCH] fix_map_search_2 --- .../src/components/MapAddressSelector.vue | 90 ++++--------------- 1 file changed, 18 insertions(+), 72 deletions(-) diff --git a/frontend/src/components/MapAddressSelector.vue b/frontend/src/components/MapAddressSelector.vue index 7298307..6341bda 100644 --- a/frontend/src/components/MapAddressSelector.vue +++ b/frontend/src/components/MapAddressSelector.vue @@ -337,89 +337,35 @@ export default { }; // 搜索地址 - 调用后端API - const searchAddress = () => { + const searchAddress = async () => { if (!searchKeyword.value.trim()) { searchResults.value = []; return; } - const token = localStorage.getItem('token'); - if (!token) { - ElMessage.error('请先登录'); - return; - } - - // 调用后端API进行搜索 - fetch('/api/address/search-poi', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': 'Bearer ' + token - }, - body: JSON.stringify({ - keyword: searchKeyword.value, - city: '全国' - }) - }) - .then(response => { - console.log('📡 响应状态:', response.status, response.statusText); + try { + const result = await addressAPI.searchPOI(searchKeyword.value, '全国'); + console.log('🔍 搜索结果:', result); - // 检查响应状态 - if (response.status === 401) { - ElMessage.error('登录已过期,请重新登录'); - localStorage.removeItem('token'); - window.location.href = '/login'; - throw new Error('Unauthorized'); - } - - // 检查响应是否成功 - if (!response.ok) { - throw new Error(`HTTP ${response.status}: ${response.statusText}`); - } - - // 检查 Content-Type - const contentType = response.headers.get('content-type'); - if (!contentType || !contentType.includes('application/json')) { - console.error('❌ 响应不是 JSON 格式:', contentType); - throw new Error('响应格式错误'); - } - - return response.json(); - }) - .then(result => { - console.log('🔍 前端收到后端响应:', result); - - if (result && result.code === 200) { - if (result.data && result.data.length > 0) { - console.log('✅ 响应数据正常:', result.data); - searchResults.value = result.data.map(poi => ({ - id: poi.id, - name: poi.name, - address: poi.address, - district: poi.district, - location: { lng: poi.lng, lat: poi.lat }, - distance: 0 - })); - console.log("🎯 处理后的搜索结果:", searchResults.value); - } else { - searchResults.value = []; - ElMessage.info('未找到相关地址'); - } + if (result && result.length > 0) { + searchResults.value = result.map(poi => ({ + id: poi.id, + name: poi.name, + address: poi.address, + district: poi.district, + location: { lng: poi.lng, lat: poi.lat }, + distance: 0 + })); + console.log('🎯 处理后的搜索结果:', searchResults.value); } else { searchResults.value = []; - const errorMsg = result?.message || '搜索失败'; - console.error('❌ 搜索失败:', errorMsg); - ElMessage.error(errorMsg); + ElMessage.info('未找到相关地址'); } - }) - .catch(error => { + } catch (error) { console.error('❌ 搜索错误:', error); searchResults.value = []; - - if (error.message !== 'Unauthorized') { - ElMessage.error('搜索失败,请稍后重试'); - } - }); + ElMessage.error('搜索失败,请稍后重试'); + } }; // 选择搜索结果