fix_map_search_2
This commit is contained in:
parent
7c39f20521
commit
af926257b9
@ -337,62 +337,18 @@ 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;
|
||||
}
|
||||
try {
|
||||
const result = await addressAPI.searchPOI(searchKeyword.value, '全国');
|
||||
console.log('🔍 搜索结果:', result);
|
||||
|
||||
// 调用后端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 (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 => ({
|
||||
if (result && result.length > 0) {
|
||||
searchResults.value = result.map(poi => ({
|
||||
id: poi.id,
|
||||
name: poi.name,
|
||||
address: poi.address,
|
||||
@ -400,26 +356,16 @@ export default {
|
||||
location: { lng: poi.lng, lat: poi.lat },
|
||||
distance: 0
|
||||
}));
|
||||
console.log("🎯 处理后的搜索结果:", searchResults.value);
|
||||
console.log('🎯 处理后的搜索结果:', searchResults.value);
|
||||
} else {
|
||||
searchResults.value = [];
|
||||
ElMessage.info('未找到相关地址');
|
||||
}
|
||||
} else {
|
||||
searchResults.value = [];
|
||||
const errorMsg = result?.message || '搜索失败';
|
||||
console.error('❌ 搜索失败:', errorMsg);
|
||||
ElMessage.error(errorMsg);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
} catch (error) {
|
||||
console.error('❌ 搜索错误:', error);
|
||||
searchResults.value = [];
|
||||
|
||||
if (error.message !== 'Unauthorized') {
|
||||
ElMessage.error('搜索失败,请稍后重试');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 选择搜索结果
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user