fix:修复bug

This commit is contained in:
2026-03-17 12:03:54 +08:00
parent 5d7b973ddd
commit 60df817de5
32 changed files with 654 additions and 528 deletions
+75 -71
View File
@@ -52,14 +52,22 @@
</template>
<script lang="ts" setup>
import Config from "@/config";
import {EventEnum} from "@/constant/enums";
import {useLogicStore} from "@/pages-user/store/logic";
import {useUserStore} from "@/store";
import { getCurrentInstance } from "vue";
const {t, locale} = useI18n();
const userStore = useUserStore()
const logicStore = useLogicStore()
let openerEventChannel: any = null
onLoad(() => {
// 统一通过 getOpenerEventChannel 获取导航传入的 eventChannel
const pages = getCurrentPages() as any[]
const page = pages[pages.length - 1]
openerEventChannel = page?.getOpenerEventChannel?.() || null
})
// 生命周期:清空地址列表
onMounted(() => {
logicStore.clearPlacesList()
@@ -99,7 +107,7 @@ const querySearch = computed(() => {
function handleClickLocation(item: any) {
console.log('item', item)
uni.$emit(EventEnum.CHOOSE_ADDRESS, item)
openerEventChannel?.emit?.('chooseAddress', item)
uni.navigateBack()
}
@@ -130,7 +138,7 @@ function handleUseLocation() {
},
})
} else {
uni.$emit(EventEnum.CHOOSE_ADDRESS, {
openerEventChannel?.emit?.('chooseAddress', {
displayName: userStore.location.location,
formattedAddress: userStore.location.formattedAddress || '',
location: {
@@ -192,8 +200,8 @@ function getCityName(latitude: number, longitude: number) {
latitude
};
// 发事件
uni.$emit(EventEnum.CHOOSE_ADDRESS, {
// 回传上一页(eventChannel
openerEventChannel?.emit?.('chooseAddress', {
displayName: cityName,
formattedAddress,
location: { lng: longitude, lat: latitude }
@@ -220,69 +228,65 @@ const mapDataComputed = computed(() => {
lat: userStore.location.latitude || -36.8485,
}
})
</script>
<script lang="ts">
import { defineComponent } from "vue";
import { useLogicStore } from "@/pages-user/store/logic";
// 监听来自 renderjs 的事件
onMounted(() => {
// 监听搜索结果
uni.$on('MAP_SEARCH_RESULT', (places: any) => {
console.log(places, '接收到的搜索结果')
if (places && places.length > 0) {
// 解析实际返回的数据结构
const parsedPlaces = places.map((placeArray: any) => {
const placeData = placeArray[0]?.[0];
if (!placeData) return null;
export default defineComponent({
methods: {
onMapSearchResult(places: any) {
const logicStore = useLogicStore()
console.log(places, '接收到的搜索结果')
const placeId = placeData[1];
const formattedAddress = placeData[8] || '';
const locationArray = placeData[11];
const displayNameArray = placeData[27];
if (places && places.length > 0) {
// 保持与原先一致的解析逻辑
const parsedPlaces = places
.map((placeArray: any) => {
const placeData = placeArray[0]?.[0]
if (!placeData) return null
return {
id: placeId,
displayName: displayNameArray?.[0] || formattedAddress,
formattedAddress: formattedAddress,
location: locationArray ? {
lat: locationArray[0],
lng: locationArray[1]
} : null
};
}).filter(Boolean);
const placeId = placeData[1]
const formattedAddress = placeData[8] || ''
const locationArray = placeData[11]
const displayNameArray = placeData[27]
console.log('解析后的地址列表', parsedPlaces);
logicStore.setPlacesList(parsedPlaces);
} else {
logicStore.setPlacesList([]);
}
})
return {
id: placeId,
displayName: displayNameArray?.[0] || formattedAddress,
formattedAddress: formattedAddress,
location: locationArray
? {
lat: locationArray[0],
lng: locationArray[1],
}
: null,
}
})
.filter(Boolean)
// 监听地图未加载提示
uni.$on('MAP_NOT_LOADED', () => {
uni.showToast({
title: 'Map is not loaded yet, please wait',
icon: 'none'
});
})
// 监听搜索超时提示
uni.$on('MAP_SEARCH_TIMEOUT', () => {
uni.showToast({
title: 'Search timeout, please try again',
icon: 'none'
});
})
// 监听搜索加载状态
uni.$on('MAP_SEARCH_LOADING', (isLoading: boolean) => {
// logicStore.searchLoading = isLoading;
})
})
onUnmounted(() => {
// 清理事件监听
uni.$off('MAP_SEARCH_RESULT')
uni.$off('MAP_NOT_LOADED')
uni.$off('MAP_SEARCH_TIMEOUT')
uni.$off('MAP_SEARCH_LOADING')
console.log('解析后的地址列表', parsedPlaces)
logicStore.setPlacesList(parsedPlaces)
} else {
logicStore.setPlacesList([])
}
},
onMapNotLoaded() {
uni.showToast({
title: 'Map is not loaded yet, please wait',
icon: 'none',
})
},
onMapSearchTimeout() {
uni.showToast({
title: 'Search timeout, please try again',
icon: 'none',
})
},
onMapSearchLoading(_isLoading: boolean) {
// 如需联动 loading,可在此处驱动 store
},
},
})
</script>
<script lang="renderjs" module="mapRenderjs">
@@ -377,24 +381,24 @@ export default {
async searchPlace({keyword}) {
console.log('搜索关键词', keyword);
if (!keyword) {
uni.$emit('MAP_SEARCH_RESULT', [])
this.$ownerInstance.callMethod('onMapSearchResult', [])
return;
}
if (!mapLoaded) {
console.log('地图未加载完成,无法搜索');
uni.$emit('MAP_NOT_LOADED');
this.$ownerInstance.callMethod('onMapNotLoaded');
return;
}
if (!map || !this.google) {
return
}
uni.$emit('MAP_SEARCH_LOADING', true);
this.$ownerInstance.callMethod('onMapSearchLoading', true);
let timeoutId;
try {
// 设置搜索超时
const timeoutPromise = new Promise((_, reject) => {
timeoutId = setTimeout(() => {
uni.$emit('MAP_SEARCH_LOADING', false);
this.$ownerInstance.callMethod('onMapSearchLoading', false);
reject(new Error('Search timeout'));
}, 10000); // 10 秒超时
});
@@ -430,16 +434,16 @@ export default {
});
console.log('序列化后的搜索结果', serializedPlaces);
uni.$emit('MAP_SEARCH_RESULT', serializedPlaces);
this.$ownerInstance.callMethod('onMapSearchResult', serializedPlaces);
} catch (e) {
console.log('搜索错误原因', e);
if (e.message === 'Search timeout') {
uni.$emit('MAP_SEARCH_TIMEOUT');
this.$ownerInstance.callMethod('onMapSearchTimeout');
}
uni.$emit('MAP_SEARCH_RESULT', []);
this.$ownerInstance.callMethod('onMapSearchResult', []);
} finally {
clearTimeout(timeoutId);
uni.$emit('MAP_SEARCH_LOADING', false);
this.$ownerInstance.callMethod('onMapSearchLoading', false);
}
}
},