fix_map_search_2

This commit is contained in:
superlishunqin 2025-09-30 06:24:30 +08:00
parent 7c39f20521
commit af926257b9

View File

@ -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('搜索失败,请稍后重试');
}
};
//