| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <template>
- <view class="address-picker">
- <view class="picker-row" @click="showProvincePicker">
- <text class="picker-label">省份:</text>
- <text class="picker-value">{{ selectedProvince ? selectedProvince.province : '请选择' }}</text>
- <text class="picker-arrow">▶</text>
- </view>
-
- <view class="picker-row" @click="showCityPicker" v-if="selectedProvince">
- <text class="picker-label">城市:</text>
- <text class="picker-value">{{ selectedCity ? selectedCity.city : '请选择' }}</text>
- <text class="picker-arrow">▶</text>
- </view>
-
- <view class="picker-row" @click="showDistrictPicker" v-if="selectedCity">
- <text class="picker-label">区县:</text>
- <text class="picker-value">{{ selectedDistrict ? selectedDistrict.district : '请选择' }}</text>
- <text class="picker-arrow">▶</text>
- </view>
-
- <view class="picker-row" @click="showStreetPicker" v-if="selectedDistrict">
- <text class="picker-label">街道:</text>
- <text class="picker-value">{{ selectedStreet ? selectedStreet.street : '请选择' }}</text>
- <text class="picker-arrow">▶</text>
- </view>
-
- <view class="address-display" v-if="fullAddress">
- <text class="address-label">完整地址:</text>
- <text class="address-text">{{ fullAddress }}</text>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'AddressPicker',
- props: {
- value: {
- type: Number,
- default: null
- }
- },
- data() {
- return {
- provinces: [],
- cities: [],
- districts: [],
- streets: [],
- selectedProvince: null,
- selectedCity: null,
- selectedDistrict: null,
- selectedStreet: null
- }
- },
- computed: {
- fullAddress() {
- if (this.selectedStreet) {
- return this.selectedStreet.fullName
- }
- return ''
- }
- },
- watch: {
- value: {
- immediate: true,
- handler(newVal) {
- if (newVal) {
- this.loadAddressById(newVal)
- }
- }
- }
- },
- mounted() {
- this.loadProvinces()
- },
- methods: {
- async loadProvinces() {
- try {
- const res = await this.$http.get('/api/streets/provinces')
- this.provinces = res.data || []
- } catch (e) {
- console.error('加载省份失败', e)
- }
- },
-
- async loadCities(provinceId) {
- try {
- const res = await this.$http.get(`/api/streets/children/${provinceId}`)
- this.cities = res.data || []
- } catch (e) {
- console.error('加载城市失败', e)
- }
- },
-
- async loadDistricts(cityId) {
- try {
- const res = await this.$http.get(`/api/streets/children/${cityId}`)
- this.districts = res.data || []
- } catch (e) {
- console.error('加载区县失败', e)
- }
- },
-
- async loadStreets(districtId) {
- try {
- const res = await this.$http.get(`/api/streets/children/${districtId}`)
- this.streets = res.data || []
- } catch (e) {
- console.error('加载街道失败', e)
- }
- },
-
- async loadAddressById(streetId) {
- try {
- const res = await this.$http.get(`/api/streets/${streetId}/path`)
- const path = res.data || []
-
- if (path.length >= 1) this.selectedProvince = path[0]
- if (path.length >= 2) this.selectedCity = path[1]
- if (path.length >= 3) this.selectedDistrict = path[2]
- if (path.length >= 4) this.selectedStreet = path[3]
-
- // 加载下级选项
- if (this.selectedProvince) await this.loadCities(this.selectedProvince.id)
- if (this.selectedCity) await this.loadDistricts(this.selectedCity.id)
- if (this.selectedDistrict) await this.loadStreets(this.selectedDistrict.id)
- } catch (e) {
- console.error('加载地址路径失败', e)
- }
- },
-
- showProvincePicker() {
- uni.showActionSheet({
- itemList: this.provinces.map(p => p.province),
- success: async (res) => {
- this.selectedProvince = this.provinces[res.tapIndex]
- this.selectedCity = null
- this.selectedDistrict = null
- this.selectedStreet = null
- this.cities = []
- this.districts = []
- this.streets = []
-
- await this.loadCities(this.selectedProvince.id)
- this.emitChange()
- }
- })
- },
-
- showCityPicker() {
- if (this.cities.length === 0) {
- uni.showToast({ title: '请先选择省份', icon: 'none' })
- return
- }
-
- uni.showActionSheet({
- itemList: this.cities.map(c => c.city),
- success: async (res) => {
- this.selectedCity = this.cities[res.tapIndex]
- this.selectedDistrict = null
- this.selectedStreet = null
- this.districts = []
- this.streets = []
-
- await this.loadDistricts(this.selectedCity.id)
- this.emitChange()
- }
- })
- },
-
- showDistrictPicker() {
- if (this.districts.length === 0) {
- uni.showToast({ title: '请先选择城市', icon: 'none' })
- return
- }
-
- uni.showActionSheet({
- itemList: this.districts.map(d => d.district),
- success: async (res) => {
- this.selectedDistrict = this.districts[res.tapIndex]
- this.selectedStreet = null
- this.streets = []
-
- await this.loadStreets(this.selectedDistrict.id)
- this.emitChange()
- }
- })
- },
-
- showStreetPicker() {
- if (this.streets.length === 0) {
- uni.showToast({ title: '请先选择区县', icon: 'none' })
- return
- }
-
- uni.showActionSheet({
- itemList: this.streets.map(s => s.street),
- success: (res) => {
- this.selectedStreet = this.streets[res.tapIndex]
- this.emitChange()
- }
- })
- },
-
- emitChange() {
- const streetId = this.selectedStreet ? this.selectedStreet.id : null
- const addressData = {
- streetId: streetId,
- province: this.selectedProvince ? this.selectedProvince.province : null,
- city: this.selectedCity ? this.selectedCity.city : null,
- district: this.selectedDistrict ? this.selectedDistrict.district : null,
- street: this.selectedStreet ? this.selectedStreet.street : null,
- fullName: this.fullAddress
- }
-
- this.$emit('input', streetId)
- this.$emit('change', addressData)
- },
-
- reset() {
- this.selectedProvince = null
- this.selectedCity = null
- this.selectedDistrict = null
- this.selectedStreet = null
- this.cities = []
- this.districts = []
- this.streets = []
- this.emitChange()
- }
- }
- }
- </script>
- <style scoped>
- .address-picker {
- background: #fff;
- padding: 20rpx;
- }
- .picker-row {
- display: flex;
- align-items: center;
- padding: 20rpx 0;
- border-bottom: 1px solid #eee;
- }
- .picker-label {
- font-size: 28rpx;
- color: #666;
- width: 140rpx;
- }
- .picker-value {
- flex: 1;
- font-size: 28rpx;
- color: #333;
- }
- .picker-arrow {
- font-size: 24rpx;
- color: #999;
- margin-left: 10rpx;
- }
- .address-display {
- margin-top: 20rpx;
- padding: 20rpx;
- background: #f5f5f5;
- border-radius: 8rpx;
- }
- .address-label {
- font-size: 24rpx;
- color: #999;
- display: block;
- margin-bottom: 10rpx;
- }
- .address-text {
- font-size: 28rpx;
- color: #333;
- }
- </style>
|