if (res.data.code === this.API.SUCCESS) {
let body = res.data.body || [] array.splice(0, array.length, ...body)
}
})
.catch(err => {
console.log(err)
})
.finally(res => {
})
},
// 根据国家编码获取省份列表
getProvinces () {
if (this.provinceFlag) {
return
}
this.fetchData(this.provinceList, this.API.province, 156)
this.provinceFlag = true
},
// 省份修改,拉取对应城市列表
changeProvince (val) {
this.fetchData(this.cityList, this.API.city, this.provinceCode)
this.cityFlag = true
this.cityCode = ''
this.areaCode = ''
this.$emit('addressSelect', val)
},
// 根据省份编码获取城市列表
getCities () {
if (this.cityFlag) {
return
}
if (this.provinceCode) {
this.fetchData(this.cityList, this.API.city, this.provinceCode)
this.cityFlag = true
}
},
// 城市修改,拉取对应区域列表
changeCity (val) {
this.fetchData(this.areaList, this.API.area, this.cityCode)
this.areaFlag = true
this.areaCode = ''
this.$emit('addressSelect', val)
},
// 根据城市编码获取区域列表
getAreas () {
if (this.areaFlag) {
return
}
if (this.cityCode) {
this.fetchData(this.areaList, this.API.area, this.cityCode)
}
},
// 区域修改
changeArea (val) {
this.$emit('addressSelect', val)
},
// 重置省市区/县编码
reset () {
this.provinceCode = '',
this.cityCode = '',
this.areaCode = ''
},
// 地址编码转换成省市区列表
addressCodeToList (addressCode) {
if (!addressCode) return false
this.$http({
method: 'get',
url: this.API.addressCode + '/' + addressCode
})
.then(res => {
let data = res.data.body
if (!data) return
if (data.provinceCode) {
this.provinceCode = data.provinceCode
this.fetchData(this.cityList, this.API.city, this.provinceCode)
} else if (data.cityCode) {
this.cityCode = data.cityCode
this.fetchData(this.areaList, this.API.area, this.cityCode)
} else if (data.areaCode) {
this.areaCode = data.areaCode
}
})
.finally(res => {
})
}
},
watch: {
addressCode: {
deep: true,
immediate: true,
handler (newVal) {
if (newVal) {
this.addressCodeToList(newVal)
} else {
this.$nextTick(() => {
this.reset()
})
}
}
}
}
}
</script>
<style lang="less" scoped>
</style>
实现效果如下(动图):










