HomeLease/components/map-location/map-location.vue

186 lines
4.1 KiB
Vue
Raw Normal View History

2025-08-19 09:51:32 +08:00
<template>
<view class="map-location-component">
<!-- 地址显示框 -->
2025-08-19 09:51:32 +08:00
<view class="form-item address-form-item">
<text class="field-label">{{ label || '地址' }}</text>
<view class="address-display-wrapper">
<view class="address-display" @click="authVerification">
<text :class="{ 'has-value': addressValue }" 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,
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-08-19 09:51:32 +08:00
data() {
return {
2025-08-21 09:46:31 +08:00
addressValue: this.value,
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: {
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
},
})
},
handerChooseLocation(latitude, longitude) {
uni.chooseLocation({
latitude: latitude || '',
longitude: longitude || '',
success: res => {
this.addressValue = res.address || res.name
this.locationData = res // 保存完整的定位信息
// 触发事件传递给父组件
this.$emit('location-selected', res)
this.$emit('location-success', res)
this.$emit('update:location', res) // 新增:传递完整定位数据
uni.setStorageSync('currentLocation', res)
2025-08-19 09:51:32 +08:00
},
fail: err => {
console.log('取消选择位置', err)
this.$emit('location-cancel', err)
this.$emit('location-error', err)
2025-08-19 09:51:32 +08:00
},
})
},
handleOpenSetting() {
wx.openSetting({
success: res => {
if (res.authSetting['scope.userLocation']) {
this.handerChooseLocation()
}
},
2025-08-19 09:51:32 +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 {
border-bottom: 1rpx solid #d8d8d8;
2025-08-19 09:51:32 +08:00
width: 100%;
}
.address-form-item {
display: flex;
align-items: center;
padding: 20rpx 0;
2025-08-19 09:51:32 +08:00
.field-label {
font-size: 28rpx;
color: #333;
font-weight: 400;
flex: 1;
line-height: 1.5;
2025-08-19 09:51:32 +08:00
}
}
.address-display-wrapper {
2025-08-19 09:51:32 +08:00
flex: 3;
display: flex;
align-items: center;
2025-08-19 09:51:32 +08:00
padding: 0 20rpx;
border-radius: 12rpx;
background: #fff;
.address-display {
2025-08-19 09:51:32 +08:00
flex: 1;
display: flex;
align-items: center;
}
2025-08-19 09:51:32 +08:00
.address-text {
font-size: 28rpx;
color: #999;
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
}
.address-text.has-value {
color: #000000; /* 有值时黑色 */
}
2025-08-19 09:51:32 +08:00
.map-icon-wrapper {
display: flex;
align-items: center;
justify-content: center;
width: 42rpx;
height: 42rpx;
background: #f15a04;
2025-08-19 09:51:32 +08:00
border-radius: 50%;
margin-left: 16rpx;
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>