2025-08-19 09:51:32 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<view class="map-location-component">
|
2025-09-03 17:57:17 +08:00
|
|
|
|
<!-- 地址显示框 -->
|
2025-08-19 09:51:32 +08:00
|
|
|
|
<view class="form-item address-form-item">
|
|
|
|
|
|
<text class="field-label">{{ label || '地址' }}</text>
|
2025-09-03 17:57:17 +08:00
|
|
|
|
<view class="address-display-wrapper">
|
|
|
|
|
|
<view class="address-display" @click="authVerification">
|
|
|
|
|
|
<text class="address-text">{{ addressValue || placeholder || '请选择收货地址' }}</text>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
<view class="map-icon-wrapper" title="点击获取定位并打开地图" @click="authVerification">
|
2025-08-19 09:51:32 +08:00
|
|
|
|
<text class="map-icon">🗺️</text>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'MapLocation',
|
|
|
|
|
|
props: {
|
|
|
|
|
|
label: {
|
|
|
|
|
|
type: String,
|
2025-08-21 09:46:31 +08:00
|
|
|
|
default: '地址',
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
placeholder: {
|
|
|
|
|
|
type: String,
|
2025-09-03 17:57:17 +08:00
|
|
|
|
default: '请选择收货地址',
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
value: {
|
|
|
|
|
|
type: String,
|
2025-08-21 09:46:31 +08:00
|
|
|
|
default: '',
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-09-03 17:57:17 +08:00
|
|
|
|
|
2025-08-19 09:51:32 +08:00
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
2025-08-21 09:46:31 +08:00
|
|
|
|
addressValue: this.value,
|
2025-09-03 18:05:12 +08:00
|
|
|
|
locationData: null, // 存储完整的定位信息
|
2025-08-19 09:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
watch: {
|
|
|
|
|
|
value(newVal) {
|
|
|
|
|
|
this.addressValue = newVal
|
|
|
|
|
|
},
|
|
|
|
|
|
addressValue(newVal) {
|
|
|
|
|
|
this.$emit('input', newVal)
|
2025-08-21 09:46:31 +08:00
|
|
|
|
},
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
2025-09-03 17:57:17 +08:00
|
|
|
|
authVerification() {
|
|
|
|
|
|
uni.getSetting({
|
|
|
|
|
|
success: res => {
|
|
|
|
|
|
if (res.authSetting['scope.userLocation']) {
|
|
|
|
|
|
this.handerChooseLocation()
|
|
|
|
|
|
} else if (res.authSetting['scope.userLocation'] === undefined) {
|
|
|
|
|
|
this.handleOpenSetting()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.handleOpenSetting()
|
|
|
|
|
|
}
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
2025-09-03 17:57:17 +08:00
|
|
|
|
},
|
|
|
|
|
|
handerChooseLocation(latitude, longitude) {
|
|
|
|
|
|
uni.chooseLocation({
|
|
|
|
|
|
latitude: latitude || '',
|
|
|
|
|
|
longitude: longitude || '',
|
|
|
|
|
|
success: res => {
|
|
|
|
|
|
this.addressValue = res.address || res.name
|
2025-09-03 18:05:12 +08:00
|
|
|
|
this.locationData = res // 保存完整的定位信息
|
|
|
|
|
|
|
|
|
|
|
|
// 触发事件传递给父组件
|
2025-09-03 17:57:17 +08:00
|
|
|
|
this.$emit('location-selected', res)
|
|
|
|
|
|
this.$emit('location-success', res)
|
2025-09-03 18:05:12 +08:00
|
|
|
|
this.$emit('update:location', res) // 新增:传递完整定位数据
|
|
|
|
|
|
|
2025-09-03 17:57:17 +08:00
|
|
|
|
uni.setStorageSync('currentLocation', res)
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
fail: err => {
|
2025-09-03 17:57:17 +08:00
|
|
|
|
console.log('取消选择位置', err)
|
|
|
|
|
|
this.$emit('location-cancel', err)
|
|
|
|
|
|
this.$emit('location-error', err)
|
2025-08-19 09:51:32 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
2025-09-03 17:57:17 +08:00
|
|
|
|
handleOpenSetting() {
|
|
|
|
|
|
wx.openSetting({
|
|
|
|
|
|
success: res => {
|
|
|
|
|
|
if (res.authSetting['scope.userLocation']) {
|
|
|
|
|
|
this.handerChooseLocation()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-08-19 09:51:32 +08:00
|
|
|
|
})
|
|
|
|
|
|
},
|
2025-09-03 18:05:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 新增方法:获取经纬度数据供父组件调用
|
|
|
|
|
|
getLocationData() {
|
|
|
|
|
|
return this.locationData
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 新增方法:清空定位数据
|
|
|
|
|
|
clearLocation() {
|
|
|
|
|
|
this.locationData = null
|
|
|
|
|
|
this.addressValue = ''
|
|
|
|
|
|
},
|
2025-08-21 09:46:31 +08:00
|
|
|
|
},
|
2025-08-19 09:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
|
.map-location-component {
|
2025-08-25 17:08:30 +08:00
|
|
|
|
border-bottom: 1rpx solid #d8d8d8;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
width: 100%;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.address-form-item {
|
|
|
|
|
|
display: flex;
|
2025-09-03 18:05:12 +08:00
|
|
|
|
align-items: center;
|
2025-09-03 17:57:17 +08:00
|
|
|
|
padding: 20rpx 0;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
.field-label {
|
|
|
|
|
|
font-size: 28rpx;
|
|
|
|
|
|
color: #333;
|
|
|
|
|
|
font-weight: 400;
|
|
|
|
|
|
flex: 1;
|
2025-09-03 18:05:12 +08:00
|
|
|
|
line-height: 1.5;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 17:57:17 +08:00
|
|
|
|
.address-display-wrapper {
|
2025-08-19 09:51:32 +08:00
|
|
|
|
flex: 3;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2025-09-03 17:57:17 +08:00
|
|
|
|
min-height: 80rpx;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
padding: 0 20rpx;
|
|
|
|
|
|
border-radius: 12rpx;
|
|
|
|
|
|
background: #fff;
|
|
|
|
|
|
|
2025-09-03 17:57:17 +08:00
|
|
|
|
.address-display {
|
2025-08-19 09:51:32 +08:00
|
|
|
|
flex: 1;
|
2025-09-03 17:57:17 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
min-height: 56rpx;
|
|
|
|
|
|
}
|
2025-08-19 09:51:32 +08:00
|
|
|
|
|
2025-09-03 17:57:17 +08:00
|
|
|
|
.address-text {
|
|
|
|
|
|
font-size: 28rpx;
|
2025-09-03 18:05:12 +08:00
|
|
|
|
color: #999;
|
2025-09-03 17:57:17 +08:00
|
|
|
|
line-height: 1.5;
|
|
|
|
|
|
word-break: break-all;
|
|
|
|
|
|
white-space: pre-wrap;
|
|
|
|
|
|
display: -webkit-box;
|
|
|
|
|
|
-webkit-box-orient: vertical;
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
text-overflow: ellipsis;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.map-icon-wrapper {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
width: 48rpx;
|
|
|
|
|
|
height: 48rpx;
|
2025-09-03 18:05:12 +08:00
|
|
|
|
background: #f15a04;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
border-radius: 50%;
|
|
|
|
|
|
margin-left: 16rpx;
|
2025-09-03 17:57:17 +08:00
|
|
|
|
flex-shrink: 0;
|
2025-08-19 09:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
|
|
transform: scale(0.9);
|
|
|
|
|
|
box-shadow: 0 2rpx 8rpx rgba(241, 90, 4, 0.4);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.map-icon {
|
|
|
|
|
|
font-size: 28rpx;
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-21 09:46:31 +08:00
|
|
|
|
</style>
|