<template>
|
<view>
|
<uv-picker :immediateChange="false" visibleItemCount="5" round="16" ref="Dome" :columns="addressList" :loading="loading" keyName="title" @change="change" @confirm="confirm" ></uv-picker>
|
</view>
|
</template>
|
<script>
|
import {getCityList} from "@/api/modules/user";
|
|
export default {
|
data() {
|
return {
|
loading: true,
|
provinces: [], //省
|
citys: [], //市
|
areas: [], //区
|
pickerValue: [0, 0],
|
defaultValue: [0, 0]
|
}
|
},
|
created() {
|
this.getData();
|
},
|
computed: {
|
addressList() {
|
return [this.provinces, this.citys];
|
}
|
},
|
methods: {
|
getData() {
|
const _this = this;
|
uni.request({
|
url: _this.$webHost + "/api/v2/city/getCityList",
|
success(res) {
|
this.provinces = data.sort((left, right) => (Number(left.code) > Number(right.code) ? 1 : -1));
|
this.handlePickValueDefault();
|
this.loading = false;
|
}
|
})
|
},
|
handlePickValueDefault() {
|
// 设置省
|
const provinceIndex = this.provinces.findIndex(item => Number(item.id) === this.defaultValue[0]);
|
this.pickerValue[0] = provinceIndex >= 0 ? provinceIndex : 0;
|
|
// 设置市
|
const cityList = this.provinces[this.pickerValue[0]]?.children || [];
|
const cityIndex = cityList.findIndex(item => Number(item.id) === this.defaultValue[1]);
|
this.citys = cityList;
|
this.pickerValue[1] = cityIndex >= 0 ? cityIndex : 0;
|
|
// 设置区
|
const areaList = cityList[this.pickerValue[1]]?.children || [];
|
const areaIndex = areaList.findIndex(item => Number(item.id) === this.defaultValue[2]);
|
this.areas = areaList;
|
// this.pickerValue[2] = areaIndex >= 0 ? areaIndex : 0;
|
|
// 重置下位置
|
this.$refs.Dome.setIndexs(this.pickerValue, true);
|
},
|
change(e) {
|
if (this.loading) return;
|
const { columnIndex, index, indexs } = e;
|
|
if (columnIndex === 0) {
|
this.citys = this.provinces[index]?.children || [];
|
this.areas = this.citys[0]?.children || [];
|
this.$refs.Dome.setIndexs([index, 0, 0], true);
|
} else if (columnIndex === 1) {
|
this.areas = this.citys[index]?.children || [];
|
this.$refs.Dome.setIndexs(indexs, true);
|
}
|
},
|
open() {
|
this.$refs.Dome.open();
|
this.handlePickValueDefault();
|
},
|
confirm(e) {
|
this.$emit('confirm', e);
|
}
|
}
|
}
|
</script>
|