Fix:5066 【商城小程序】体验券订单未显示当前位置与该体验店的距离
This commit is contained in:
@@ -592,7 +592,9 @@ export default {
|
|||||||
keyword: '',
|
keyword: '',
|
||||||
_orderId: '',
|
_orderId: '',
|
||||||
showDatePicker: false,
|
showDatePicker: false,
|
||||||
dateRange: []
|
dateRange: [],
|
||||||
|
userLatitude: null,
|
||||||
|
userLongitude: null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
@@ -645,6 +647,7 @@ export default {
|
|||||||
onLoad() {
|
onLoad() {
|
||||||
this.type = this.type || parseInt(this.$yroute.query.type) || 0
|
this.type = this.type || parseInt(this.$yroute.query.type) || 0
|
||||||
this.orderTypeTabIndex = parseInt(this.$yroute.query.tabIndex) || 0
|
this.orderTypeTabIndex = parseInt(this.$yroute.query.tabIndex) || 0
|
||||||
|
this.initUserLocation()
|
||||||
this.orderTypeTabChange({
|
this.orderTypeTabChange({
|
||||||
...this.orderTypeTabList[this.orderTypeTabIndex],
|
...this.orderTypeTabList[this.orderTypeTabIndex],
|
||||||
index: this.orderTypeTabIndex
|
index: this.orderTypeTabIndex
|
||||||
@@ -662,6 +665,89 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
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) {
|
handleDateSelected(dateRange) {
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
@@ -1027,6 +1113,7 @@ export default {
|
|||||||
item.showMore = false;
|
item.showMore = false;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
this.updateOrderDistances()
|
||||||
this.page++;
|
this.page++;
|
||||||
this.loaded = res.data.length < this.limit
|
this.loaded = res.data.length < this.limit
|
||||||
this.loading = false
|
this.loading = false
|
||||||
|
|||||||
Reference in New Issue
Block a user