fix_map_search_2
This commit is contained in:
parent
7c39f20521
commit
af926257b9
@ -337,89 +337,35 @@ export default {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 搜索地址 - 调用后端API
|
// 搜索地址 - 调用后端API
|
||||||
const searchAddress = () => {
|
const searchAddress = async () => {
|
||||||
if (!searchKeyword.value.trim()) {
|
if (!searchKeyword.value.trim()) {
|
||||||
searchResults.value = [];
|
searchResults.value = [];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = localStorage.getItem('token');
|
try {
|
||||||
if (!token) {
|
const result = await addressAPI.searchPOI(searchKeyword.value, '全国');
|
||||||
ElMessage.error('请先登录');
|
console.log('🔍 搜索结果:', result);
|
||||||
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);
|
|
||||||
|
|
||||||
// 检查响应状态
|
if (result && result.length > 0) {
|
||||||
if (response.status === 401) {
|
searchResults.value = result.map(poi => ({
|
||||||
ElMessage.error('登录已过期,请重新登录');
|
id: poi.id,
|
||||||
localStorage.removeItem('token');
|
name: poi.name,
|
||||||
window.location.href = '/login';
|
address: poi.address,
|
||||||
throw new Error('Unauthorized');
|
district: poi.district,
|
||||||
}
|
location: { lng: poi.lng, lat: poi.lat },
|
||||||
|
distance: 0
|
||||||
// 检查响应是否成功
|
}));
|
||||||
if (!response.ok) {
|
console.log('🎯 处理后的搜索结果:', searchResults.value);
|
||||||
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('未找到相关地址');
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
searchResults.value = [];
|
searchResults.value = [];
|
||||||
const errorMsg = result?.message || '搜索失败';
|
ElMessage.info('未找到相关地址');
|
||||||
console.error('❌ 搜索失败:', errorMsg);
|
|
||||||
ElMessage.error(errorMsg);
|
|
||||||
}
|
}
|
||||||
})
|
} catch (error) {
|
||||||
.catch(error => {
|
|
||||||
console.error('❌ 搜索错误:', error);
|
console.error('❌ 搜索错误:', error);
|
||||||
searchResults.value = [];
|
searchResults.value = [];
|
||||||
|
ElMessage.error('搜索失败,请稍后重试');
|
||||||
if (error.message !== 'Unauthorized') {
|
}
|
||||||
ElMessage.error('搜索失败,请稍后重试');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 选择搜索结果
|
// 选择搜索结果
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user