diff --git a/pages/order/MyOrder/index.vue b/pages/order/MyOrder/index.vue index a1b6548..794632d 100644 --- a/pages/order/MyOrder/index.vue +++ b/pages/order/MyOrder/index.vue @@ -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