Fix:5066 【商城小程序】体验券订单未显示当前位置与该体验店的距离

This commit is contained in:
linxi
2026-04-02 17:56:22 +08:00
parent 68a215e491
commit 7993549bfd
+88 -1
View File
@@ -592,7 +592,9 @@ export default {
keyword: '',
_orderId: '',
showDatePicker: false,
dateRange: []
dateRange: [],
userLatitude: null,
userLongitude: null
}
},
components: {
@@ -645,6 +647,7 @@ export default {
onLoad() {
this.type = this.type || parseInt(this.$yroute.query.type) || 0
this.orderTypeTabIndex = parseInt(this.$yroute.query.tabIndex) || 0
this.initUserLocation()
this.orderTypeTabChange({
...this.orderTypeTabList[this.orderTypeTabIndex],
index: this.orderTypeTabIndex
@@ -662,6 +665,89 @@ export default {
}
},
methods: {
initUserLocation() {
uni.getLocation({
type: 'gcj02',
success: res => {
this.userLatitude = Number(res.latitude)
this.userLongitude = Number(res.longitude)
this.updateOrderDistances()
}
})
},
getOrderLocation(order) {
const info = order
const latitude = Number(info.merLatitude || 0)
const longitude = Number( info.merLongitude || 0)
if (!Number.isFinite(latitude) || !Number.isFinite(longitude) || latitude === 0 || longitude === 0) {
return null
}
return { latitude, longitude }
},
hasValidDistance(distance) {
if (distance == null) {
return false
}
const text = String(distance).trim().toLowerCase()
if (!text || text === '0' || text === '0km' || text === '0.0km') {
return false
}
return true
},
calculateDistance(lat1, lon1, lat2, lon2) {
if (!lat1 || !lon1 || !lat2 || !lon2) {
return ''
}
const R = 6371
const dLat = (lat2 - lat1) * Math.PI / 180
const dLon = (lon2 - lon1) * Math.PI / 180
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
const d = R * c
return d.toFixed(1) + 'km'
},
updateOrderDistances() {
if (!this.orderList.length || this.userLatitude == null || this.userLongitude == null) {
return
}
this.orderList = this.orderList.map(order => {
if (!order.experienceOrderInfo) {
return order
}
const oldDistance = order.distance || (order.experienceOrderInfo && order.experienceOrderInfo.distance)
if (this.hasValidDistance(oldDistance)) {
return order
}
const location = this.getOrderLocation(order)
console.log(location);
if (!location) {
return order
}
const distance = this.calculateDistance(
this.userLatitude,
this.userLongitude,
location.latitude,
location.longitude
)
if (!distance) {
return order
}
const experienceOrderInfo = order.experienceOrderInfo
? {
...order.experienceOrderInfo,
distance
}
: order.experienceOrderInfo
return {
...order,
distance,
experienceOrderInfo
}
})
},
handleDateSelected(dateRange) {
const params = {
@@ -1027,6 +1113,7 @@ export default {
item.showMore = false;
})
}
this.updateOrderDistances()
this.page++;
this.loaded = res.data.length < this.limit
this.loading = false