| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <template>
- <view class="address-page">
- <view class="picker-content">
- <!-- 市 -->
- <scroll-view scroll-y class="column city">
- <view v-for="(city, i) in filteredCities" :key="i" class="item"
- :class="{ cityActive: selectedCity && selectedCity.code === city.code }" @click="selectCity(city)">
- {{ city.name }}
- </view>
- </scroll-view>
- <!-- 区 -->
- <scroll-view scroll-y class="column district">
- <view v-for="(area, i) in filteredAreas" :key="i" class="item"
- :class="{ districtActive: selectedArea && selectedArea.code === area.code }" @click="selectArea(area)">
- {{ area.name }}
- </view>
- </scroll-view>
- <!-- 街道 -->
- <scroll-view scroll-y class="column street">
- <view v-for="(street, i) in filteredStreets" :key="i" class="item"
- :class="{ streetActive: selectedStreet && selectedStreet.code === street.code }"
- @click="selectStreet(street)">
- {{ street.name }}
- </view>
- </scroll-view>
- </view>
- <!-- 底部按钮 -->
- <view class="picker-footer">
- <!-- <button class="btn-clear" @click="clear">清除</button> -->
- <button class="btn-confirm" @click="handleConfirm">确定</button>
- </view>
- </view>
- </template>
- <script>
- import addressData from './level4_address.js'
- export default {
- name: "AddressPicker",
- props: {
- city: {
- type: [String, Number],
- default: ''
- }
- },
- data() {
- return {
- addressData,
- selectedProvince: null,
- selectedCity: null,
- selectedArea: {
- code: 'all',
- name: '全部'
- },
- selectedStreet: {
- code: 'all',
- name: '全部'
- }
- }
- },
- computed: {
- filteredProvinces() {
- return this.addressData || []
- },
- filteredCities() {
- return this.selectedProvince && this.selectedProvince.children ?
- this.selectedProvince.children :
- []
- },
- filteredAreas() {
- if (!this.selectedCity) return []
- // 市为“全部”或者无子级,区返回一个“全部”
- const areas = this.selectedCity.children || []
- return [{
- code: 'all',
- name: '全部'
- }, ...areas]
- },
- filteredStreets() {
- if (!this.selectedArea) return []
- // 区为“全部”或者无子级,街道返回一个“全部”
- const streets = this.selectedArea.children || []
- return [{
- code: 'all',
- name: '全部'
- }, ...streets]
- }
- },
- mounted() {
- this.initByCity()
- },
- methods: {
- /** 根据 city code 或名称自动定位 */
- initByCity() {
- if (!this.addressData.length) return
- for (const province of this.addressData) {
- for (const city of province.children || []) {
- if (city.code == this.city || city.name.indexOf(this.city) !== -1) {
- this.selectedProvince = province
- this.selectedCity = city
- this.selectedArea = {
- code: 'all',
- name: '全部'
- }
- this.selectedStreet = {
- code: 'all',
- name: '全部'
- }
- return
- }
- }
- }
- // 没找到就选第一个省市
- this.selectedProvince = this.addressData[0]
- this.selectedCity = this.addressData[0]?.children?.[0] || null
- this.selectedArea = {
- code: 'all',
- name: '全部'
- }
- this.selectedStreet = {
- code: 'all',
- name: '全部'
- }
- },
- /** 市选择 */
- selectCity(city) {
- this.selectedCity = city
- this.selectedArea = {
- code: 'all',
- name: '全部'
- }
- this.selectedStreet = {
- code: 'all',
- name: '全部'
- }
- },
- /** 区选择 */
- selectArea(area) {
- this.selectedArea = area
- this.selectedStreet = {
- code: 'all',
- name: '全部'
- }
- },
- /** 街道选择 */
- selectStreet(street) {
- this.selectedStreet = street
- },
- // /** 清除选择 */
- // clear() {
- // this.selectedCity = this.selectedProvince?.children?.[0] || null
- // this.selectedArea = {
- // code: 'all',
- // name: '全部'
- // }
- // this.selectedStreet = {
- // code: 'all',
- // name: '全部'
- // }
- // },
- /** 确认选择 */
- handleConfirm() {
- const result = []
- if (this.selectedProvince)
- result.push({
- code: this.selectedProvince.code,
- name: this.selectedProvince.name
- })
- if (this.selectedCity)
- result.push({
- code: this.selectedCity.code,
- name: this.selectedCity.name
- })
- if (this.selectedArea)
- result.push({
- code: this.selectedArea.code,
- name: this.selectedArea.name
- })
- if (this.selectedStreet)
- result.push({
- code: this.selectedStreet.code,
- name: this.selectedStreet.name
- })
- this.$emit('confirm', result)
- }
- }
- }
- </script>
- <style scoped>
- .address-page {
- display: flex;
- flex-direction: column;
- height: 100%;
- background: #FBFBFB;
- margin: 0 16rpx;
- }
- .picker-content {
- display: flex;
- flex: 1;
- overflow: auto;
- gap: 16rpx;
- }
- .column {
- padding: 40rpx 0;
- box-sizing: border-box;
- border-radius: 12rpx;
- }
- .city{
- width: 200rpx;
- background: rgba(245, 245, 245, 1);
- box-sizing: border-box;
- }
- .district{
- flex: 1;
- background: rgba(255, 255, 255, 1);
- }
- .street{
- flex: 1;
- background: linear-gradient(to right, rgba(251, 251, 251, 1), rgba(255, 255, 255, 1));
- }
- .item {
- padding: 15rpx 50rpx;
- font-family: DM Sans;
- font-size: 32rpx;
- font-weight: 400;
- margin-bottom: 16rpx;
- }
- .item.cityActive {
- color: rgba(1, 107, 246, 1);
- }
- .item.districtActive{
- background: rgba(153, 196, 250, 0.4);
- color: rgba(1, 107, 246, 1);
- }
- .item.streetActive{
- background: rgba(1, 107, 246, 1);
- color: rgba(255, 255, 255, 1);
- border-radius: 0px 12rpx 12rpx 0px;
- }
- .picker-footer {
- display: flex;
- padding: 30rpx 0;
- height: 140rpx;
- box-sizing: border-box;
- margin: 0 4rpx;
- }
- .btn-clear,
- .btn-confirm {
- flex: 1;
- height: 80rpx;
- line-height: 80rpx;
- border-radius: 999px;
- font-family: DM Sans;
- font-size: 32rpx;
- font-weight: 400;
- color: rgba(255, 255, 255, 1);
- }
- .btn-clear {
- background: #f5f5f5;
- color: #333;
- margin-right: 20rpx;
- }
- .btn-confirm {
- box-shadow: 0px 2px 4px 0px rgba(9, 196, 116, 0.3);
- background: linear-gradient(90.00deg, rgba(13, 39, 247, 1),rgba(19, 193, 234, 1) 100%);
- }
- uni-button:after{
- border: none;
- }
- </style>
|