| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <template>
- <view class="address-picker">
- <!-- 显示当前选中的地址 -->
- <view class="current-address" @click="openPicker">
- <text class="address-text">{{ currentAddress || '请选择地址' }}</text>
- <text class="address-arrow">›</text>
- </view>
- <!-- 地址选择面板 -->
- <view class="picker-mask" v-if="showPicker" @click="closePicker">
- <view class="picker-panel" @click.stop>
- <!-- 面板头部 -->
- <view class="picker-header">
- <text class="picker-btn" @click="closePicker">取消</text>
- <text class="picker-title">选择地址</text>
- <text class="picker-btn picker-confirm" @click="confirmAddress">确定</text>
- </view>
- <!-- 面包屑导航 -->
- <view class="picker-breadcrumb">
- <text
- :class="['crumb', currentLevel === 0 ? 'crumb-active' : '']"
- @click="jumpToLevel(0)"
- >{{ tempProvince || '省份' }}</text>
- <text v-if="tempProvince" class="crumb-arrow">›</text>
- <text
- :class="['crumb', currentLevel === 1 ? 'crumb-active' : '']"
- @click="jumpToLevel(1)"
- >{{ tempCity || '城市' }}</text>
- <text v-if="tempCity" class="crumb-arrow">›</text>
- <text
- :class="['crumb', currentLevel === 2 ? 'crumb-active' : '']"
- @click="jumpToLevel(2)"
- >{{ tempDistrict || '区县' }}</text>
- <text v-if="tempDistrict" class="crumb-arrow">›</text>
- <text
- :class="['crumb', currentLevel === 3 ? 'crumb-active' : '']"
- @click="jumpToLevel(3)"
- >{{ tempStreet || '街道' }}</text>
- </view>
- <!-- 选择列表 -->
- <scroll-view class="picker-list" scroll-y>
- <view
- v-for="(item, idx) in currentItems"
- :key="idx"
- :class="['picker-item', isSelected(item) ? 'picker-item-selected' : '']"
- @click="selectItem(item)"
- >
- <text class="item-text">{{ getItemName(item) }}</text>
- <text v-if="isSelected(item)" class="item-check">✓</text>
- </view>
- <view v-if="loading" class="picker-loading">
- <text>加载中...</text>
- </view>
- </scroll-view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import { getProvinces, getStreetChildren } from '@/utils/api.js'
- export default {
- name: 'AddressPicker',
- props: {
- value: {
- type: Number,
- default: null
- }
- },
- data() {
- return {
- showPicker: false,
- currentLevel: 0, // 0=省份, 1=城市, 2=区县, 3=街道
- loading: false,
- // 数据列表
- provinces: [],
- cities: [],
- districts: [],
- streets: [],
- // 临时选中项
- tempProvince: null,
- tempCity: null,
- tempDistrict: null,
- tempStreet: null,
- // 完整地址文本
- currentAddress: ''
- }
- },
- computed: {
- currentItems() {
- if (this.currentLevel === 0) return this.provinces
- if (this.currentLevel === 1) return this.cities
- if (this.currentLevel === 2) return this.districts
- if (this.currentLevel === 3) return this.streets
- return []
- }
- },
- watch: {
- value: {
- immediate: true,
- handler(newVal) {
- if (newVal) {
- this.loadAddressById(newVal)
- }
- }
- }
- },
- mounted() {
- this.loadProvinces()
- },
- methods: {
- async loadProvinces() {
- try {
- const res = await getProvinces()
- this.provinces = res.data || []
- } catch (e) {
- console.error('加载省份失败', e)
- }
- },
- async loadCities(provinceId) {
- try {
- const res = await getStreetChildren(provinceId)
- this.cities = res.data || []
- } catch (e) {
- console.error('加载城市失败', e)
- }
- },
- async loadDistricts(cityId) {
- try {
- const res = await getStreetChildren(cityId)
- this.districts = res.data || []
- } catch (e) {
- console.error('加载区县失败', e)
- }
- },
- async loadStreets(districtId) {
- try {
- const res = await getStreetChildren(districtId)
- this.streets = res.data || []
- } catch (e) {
- console.error('加载街道失败', e)
- }
- },
- async loadAddressById(streetId) {
- try {
- const res = await getStreetChildren(streetId)
- const path = res.data || []
- if (path.length >= 4) {
- this.tempProvince = path[0]
- this.tempCity = path[1]
- this.tempDistrict = path[2]
- this.tempStreet = path[3]
- this.currentAddress = path[3].fullName || this.buildAddress(path)
- }
- // 加载下级选项
- if (this.tempProvince) await this.loadCities(this.tempProvince.id)
- if (this.tempCity) await this.loadDistricts(this.tempCity.id)
- if (this.tempDistrict) await this.loadStreets(this.tempDistrict.id)
- } catch (e) {
- console.error('加载地址路径失败', e)
- }
- },
- buildAddress(path) {
- if (!path || path.length === 0) return ''
- var parts = []
- if (path[0]) parts.push(path[0].province || path[0].name)
- if (path[1]) parts.push(path[1].city || path[1].name)
- if (path[2]) parts.push(path[2].district || path[2].name)
- if (path[3]) parts.push(path[3].street || path[3].name)
- return parts.join('')
- },
- openPicker() {
- this.showPicker = true
- if (!this.tempProvince && this.provinces.length === 0) {
- this.loadProvinces()
- }
- },
- closePicker() {
- this.showPicker = false
- },
- jumpToLevel(level) {
- if (level < this.currentLevel) {
- this.currentLevel = level
- }
- },
- selectItem(item) {
- if (this.currentLevel === 0) {
- this.tempProvince = item
- this.tempCity = null
- this.tempDistrict = null
- this.tempStreet = null
- this.cities = []
- this.districts = []
- this.streets = []
- this.currentLevel = 1
- this.loadCities(item.id)
- } else if (this.currentLevel === 1) {
- this.tempCity = item
- this.tempDistrict = null
- this.tempStreet = null
- this.districts = []
- this.streets = []
- this.currentLevel = 2
- this.loadDistricts(item.id)
- } else if (this.currentLevel === 2) {
- this.tempDistrict = item
- this.tempStreet = null
- this.streets = []
- this.currentLevel = 3
- this.loadStreets(item.id)
- } else if (this.currentLevel === 3) {
- this.tempStreet = item
- this.confirmAddress()
- }
- },
- confirmAddress() {
- this.updateAddressText()
- this.emitChange()
- this.closePicker()
- },
- updateAddressText() {
- var parts = []
- if (this.tempProvince) parts.push(this.tempProvince.province || this.tempProvince.name)
- if (this.tempCity) parts.push(this.tempCity.city || this.tempCity.name)
- if (this.tempDistrict) parts.push(this.tempDistrict.district || this.tempDistrict.name)
- if (this.tempStreet) parts.push(this.tempStreet.street || this.tempStreet.name)
- this.currentAddress = parts.join('')
- },
- emitChange() {
- const streetId = this.tempStreet ? this.tempStreet.id : null
- const addressData = {
- streetId: streetId,
- province: this.tempProvince ? this.tempProvince.province : null,
- city: this.tempCity ? this.tempCity.city : null,
- district: this.tempDistrict ? this.tempDistrict.district : null,
- street: this.tempStreet ? this.tempStreet.street : null,
- fullName: this.currentAddress
- }
- this.$emit('input', streetId)
- this.$emit('change', addressData)
- },
- getItemName(item) {
- if (!item) return ''
- return item.province || item.city || item.district || item.street || item.name || ''
- },
- isSelected(item) {
- var name = this.getItemName(item)
- if (this.currentLevel === 0) return this.tempProvince && this.getItemName(this.tempProvince) === name
- if (this.currentLevel === 1) return this.tempCity && this.getItemName(this.tempCity) === name
- if (this.currentLevel === 2) return this.tempDistrict && this.getItemName(this.tempDistrict) === name
- if (this.currentLevel === 3) return this.tempStreet && this.getItemName(this.tempStreet) === name
- return false
- },
- reset() {
- this.tempProvince = null
- this.tempCity = null
- this.tempDistrict = null
- this.tempStreet = null
- this.currentAddress = ''
- this.provinces = []
- this.cities = []
- this.districts = []
- this.streets = []
- this.currentLevel = 0
- this.emitChange()
- }
- }
- }
- </script>
- <style scoped>
- .address-picker {
- width: 100%;
- }
- .current-address {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 24rpx 20rpx;
- background: #f8f8f8;
- border-radius: 12rpx;
- border: 2rpx solid #eee;
- }
- .address-text {
- font-size: 28rpx;
- color: #333;
- flex: 1;
- }
- .address-arrow {
- font-size: 32rpx;
- color: #999;
- margin-left: 10rpx;
- }
- /* 面板遮罩 */
- .picker-mask {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: rgba(0, 0, 0, 0.5);
- z-index: 999;
- display: flex;
- align-items: flex-end;
- }
- .picker-panel {
- width: 100%;
- background: #fff;
- border-radius: 24rpx 24rpx 0 0;
- max-height: 70vh;
- display: flex;
- flex-direction: column;
- }
- /* 面板头部 */
- .picker-header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 30rpx 30rpx 20rpx;
- border-bottom: 1px solid #eee;
- }
- .picker-btn {
- font-size: 28rpx;
- color: #666;
- padding: 10rpx 20rpx;
- }
- .picker-confirm {
- color: #f97316;
- font-weight: bold;
- }
- .picker-title {
- font-size: 32rpx;
- font-weight: bold;
- color: #333;
- }
- /* 面包屑导航 */
- .picker-breadcrumb {
- display: flex;
- align-items: center;
- padding: 20rpx 30rpx;
- background: #fafafa;
- border-bottom: 1px solid #eee;
- flex-wrap: wrap;
- }
- .crumb {
- font-size: 26rpx;
- color: #666;
- padding: 8rpx 16rpx;
- background: #eee;
- border-radius: 20rpx;
- }
- .crumb-active {
- background: #f97316;
- color: #fff;
- }
- .crumb-arrow {
- font-size: 28rpx;
- color: #999;
- margin: 0 8rpx;
- }
- /* 选择列表 */
- .picker-list {
- flex: 1;
- min-height: 300rpx;
- max-height: 500rpx;
- padding: 20rpx 0;
- }
- .picker-item {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 28rpx 40rpx;
- border-bottom: 1px solid #f5f5f5;
- }
- .item-text {
- font-size: 30rpx;
- color: #333;
- }
- .item-check {
- font-size: 32rpx;
- color: #f97316;
- font-weight: bold;
- }
- .picker-item-selected .item-text {
- color: #f97316;
- font-weight: bold;
- }
- .picker-loading {
- padding: 40rpx;
- text-align: center;
- color: #999;
- font-size: 28rpx;
- }
- </style>
|