AreaSelect.vue 6.96 KB
<template>
  <el-form-item :label="label"
                :prop="prop">
    <el-select v-model="provinceId"
               filterable
               placeholder="请选择"
               size="small"
               class="el-slect"
               :disabled="disabled"
               @change="povinceChange">
      <el-option v-for="item in provinceOpts"
                 :key="item.id"
                 :label="item.name"
                 :value="item.id" />
    </el-select>
    <el-select v-show="!isHideCity"
               v-model="cityId"
               placeholder=""
               size="small"
               class="el-slect"
               :disabled="disabled"
               @change="cityChange">
      <el-option v-for="item in cityOpts"
                 :key="item.id"
                 :label="item.name"
                 :value="item.id" />
    </el-select>
    <el-select v-show="!isHideArea"
               v-model="areaId"
               placeholder=""
               size="small"
               class="el-slect"
               :disabled="disabled"
               @change="areaChange">
      <el-option v-for="item in areaOpts"
                 :key="item.id"
                 :label="item.name"
                 :value="item.id" />
    </el-select>
  </el-form-item>
</template>
<script>
// import chinaData from './china'
export default {
  props: ['label', 'prop', 'id', 'init', 'disabled', 'fixArea'],
  data() {
    // const provinceOptions = chinaData.province
    // const slectedProvince = provinceOptions[0]

    return {
      chinaData: '',
      province: '',
      provinceId: '',
      // provinceOpts: provinceOptions,
      provinceOpts: [],
      city: '',
      cityId: '',
      cityOpts: [],
      isHideCity: true,
      isHideArea: true,
      area: '',
      areaId: '',
      areaOpts: []
    }
  },
  computed: {
    areaStr: {
      get() {
        return this.province + this.city + this.area
      }
    },
    areaStrSplit: {
      get() {
        let str = this.province
        if (this.city) {
          str += `,${this.city}`
        }
        if (this.area) {
          str += `,${this.area}`
        }
        return str
      }
    }
  },
  watch: {
    id(newVal) {
      if (this.init) {
        if (newVal) {
          console.log('newVal', newVal)
          console.log('fixArea', this.fixArea)
          console.log('area', this.chinaData.area)
          let cityId = '' // 地区所属城市id
          let provinceId = '' // 城市所属省份id
          // 根据区域id反查城市id
          for (const cityKey in this.chinaData.area) {
            const areaInfo = this.chinaData.area[cityKey]
            if (areaInfo.find((item) => item.id === newVal)) {
              console.log('cityKey', cityKey)
              cityId = cityKey
              break
            }
          }
          // 当城市id没找到时代表该城市下没有区域信息,因此区域id存储时存的是上一级的城市id,将获取的区域id赋值给城市id去查找省份
          if (!cityId) {
            cityId = newVal
          }
          // 根据城市id反查省份id
          for (const provinceKey in this.chinaData.city) {
            const cityInfo = this.chinaData.city[provinceKey]
            if (cityInfo.find((item) => item.id === cityId)) {
              console.log('provinceKey', provinceKey)
              provinceId = provinceKey
              break
            }
          }

          // const s1 = newVal.substr(0, 2) + '0000'
          // const s2 = newVal.substr(0, 4) + '00'

          // this.provinceId = s1
          this.provinceId = provinceId
          this.povinceChange(this.provinceId)
          // this.cityId = s2
          this.cityId = cityId
          this.pickCity(this.cityId)
          this.areaId = newVal
        } else {
          this.provinceId = ''
          this.cityId = ''
          this.areaId = ''
          this.isHideCity = true
          this.isHideArea = true
        }
        this.$emit('areaChangeInit')
      }
    }
  },
  mounted() {
    this.getArea()
  },
  methods: {
    getArea() {
      const url = new URL('https:/img01.glsx.com.cn/js/area.json')
      fetch(url)
        // fetch('area.json')
        .then(response => response.json())
        .then(data => {
          console.log(data)
          this.chinaData = data
          this.provinceOpts = this.chinaData.province
        })
        .catch(error => {
          console.error('Error:', error)
        })
    },
    povinceChange(provinceId) {
      console.log(provinceId, 'provinceId') // this.$emit('onChangArea'
      this.provinceId = provinceId
      // const cityOptions = chinaData.city[provinceId]
      const cityOptions = this.chinaData.city[provinceId]

      if (cityOptions) {
        const selectedCity = cityOptions[0]
        this.province = selectedCity.province
        this.cityId = selectedCity.id
        this.city = selectedCity.name
        this.cityOpts = cityOptions
        this.cityChange()
        if (selectedCity.name == '市辖区') {
          this.isHideCity = true
        } else {
          this.isHideCity = false
        }
      } else {
        this.isHideCity = true
        this.isHideArea = true
        // const provinceItem = chinaData.province.find((item) => item.id === provinceId)
        const provinceItem = this.chinaData.province.find((item) => item.id === provinceId)
        this.province = provinceItem.name
        this.city = ''
        this.area = ''
        this.$emit('onAreaSelectChange', this.areaStr, this.provinceId, this.areaStrSplit)
      }
    },
    pickCity(cityId) {
      this.cityId = cityId
      // const areaOptions = chinaData.area[cityId]
      const areaOptions = this.chinaData.area[cityId]

      if (areaOptions.length) {
        if (areaOptions[0].name == '市辖区') {
          areaOptions.splice(0, 1)
        }
        this.areaOpts = areaOptions
        const selectedArea = areaOptions[0]
        this.city = selectedArea.city
        this.areaId = this.id || selectedArea.id
        this.area = selectedArea.name
        this.isHideArea = false
        this.$emit('onAreaSelectChange', this.areaStr, this.areaId, this.areaStrSplit)
      } else {
        this.isHideArea = true
        // const cityItem = chinaData.city[this.provinceId].find((item) => item.id === cityId)
        const cityItem = this.chinaData.city[this.provinceId].find((item) => item.id === cityId)
        this.city = cityItem.name
        this.area = ''
        this.areaId = ''
        this.$emit('onAreaSelectChange', this.areaStr, this.cityId, this.areaStrSplit)
      }
    },
    cityChange() {
      this.pickCity(this.cityId)
    },
    areaChange() {
      // const areaOptions = chinaData.area[this.cityId]
      const areaOptions = this.chinaData.area[this.cityId]
      const selectedArea = areaOptions.find((o) => {
        return o.id == this.areaId
      })
      this.area = selectedArea.name
      this.$emit('onAreaSelectChange', this.areaStr, this.areaId, this.areaStrSplit)
    }
  }
}
</script>

<style scoped>
.el-select {
  width: 120px;
}
</style>