zjf
2023-03-08 51468f93275c2bcfcc7ad25bf05f3d6a079ff764
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<template>
  <flexbox align="stretch">
    <el-row>
      <el-col
        :span="span">
        <!--@focus="getProvinces" @focus="getCities"@focus="getAreas"-->
        <el-select
          ref="proSel"
          class='selecInput'
          size="small"
          v-model="provinceCode"
          @change="changeProvince"
          placeholder="请选择省份"
          :disabled='disabled'
          filterable>
          <el-option
            v-for="item in provinceList"
            :key="item.ProvinceID"
            :label="item.ProvinceName"
            :value="item.ProvinceID">
          </el-option>
        </el-select>
      </el-col>
      <el-col
        :span="span"
        v-if="!hideCity">
        <el-select
          ref="citySel"
          class='selecInput'
          size="small"
          v-model="cityCode"
          @change="changeCity"
          placeholder="请选择市"
          :disabled='disabled'
          filterable>
          <el-option
            v-for="item in cityList"
            :key="item.CityID"
            :label="item.CityName"
            :value="item.CityID">
          </el-option>
        </el-select>
      </el-col>
      <el-col
        :span="span"
        v-if="!hideCity && !hideArea">
        <el-select
          class='selecInput'
          size="small"
          v-model="areaCode"
          @change="changeArea"
          placeholder="请选择区/县"
          :disabled='disabled'
          filterable>
          <el-option
            v-for="item in areaList"
            :key="item.AreaId"
            :label="item.AreaName"
            :value="item.AreaId">
          </el-option>
        </el-select>
      </el-col>
    </el-row>
  </flexbox>
</template>
<script type="text/javascript">
import {GetProvincesList, GetCityList, GetAreaList} from '@/api/common'
export default {
  name: 'XhCustomerAddress', // 新建 客户位置
  components: {
  },
  props: {
    disabled: { // 禁
      type: Boolean,
      default: false
    },
    hideCity: { // 隐藏市
      type: Boolean,
      default: false
    },
    hideArea: { // 隐藏区/县
      type: Boolean,
      default: false
    },
    province: {
      type: String,
      default: ''
    },
    city: {
      type: String,
      default: ''
    },
    area: {
      type: String,
      default: ''
    },
    addressCode: null // 地址编码
  },
  data () {
    return {
      provinceList: [], // 省份列表
      cityList: [], // 城市列表
      areaList: [], // 区/县列表
      provinceCode: '', // 省份编码
      cityCode: '', // 城市编码
      areaCode: '', // 区/县编码
      cityFlag: false, // 避免重复请求的标志
      provinceFlag: false,
      areaFlag: false
    }
  },
  computed: {
    span () {
      if (this.hideCity) {
        return 24
      }
      if (this.hideArea) {
        return 12
      }
      return 8
    }
  },
  watch: {
    addressCode: {
      deep: true,
      immediate: true,
      handler (newVal) {
        if (newVal) {
          this.addressCodeToList(newVal)
        } else {
          this.$nextTick(() => {
            this.reset()
          })
        }
      }
    },
    province: {
      deep: true,
      immediate: true,
      handler (newVal) {
        if (newVal) {
          console.log(newVal)
          this.editCodeToList(newVal, 'province')
        } else {
          this.$nextTick(() => {
            this.reset()
          })
        }
      }
    },
    city: {
      deep: true,
      immediate: true,
      handler (newVal) {
        if (newVal) {
          this.editCityToList(newVal, 'city')
        } else {
          this.$nextTick(() => {
            this.reset()
          })
        }
      }
    },
    area: {
      deep: true,
      immediate: true,
      handler (newVal) {
        if (newVal) {
          this.editAreaToList(newVal, 'area')
        } else {
          this.$nextTick(() => {
            this.reset()
          })
        }
      }
    }
  },
  created () {
    this.getProList()
  },
  mounted () {
  },
  methods: {
    /**
     * 获取数据
     */
    getProList () {
      GetProvincesList().then(res => {
        this.provinceList = res.data.Result || []
        console.log(this.province)
        if (!this.province) {
          this.provinceCode = res.data.Result && res.data.Result.length > 0 ? res.data.Result[0].ProvinceID : ''
          let selected = this.idFilter(this.provinceCode, this.provinceList, 'province')
          this.$emit('province', selected.ProvinceName)
        }
        this.getCityList(this.provinceCode)
      })
    },
    getCityList (val) {
      let params = {
        provinceId: val
      }
      GetCityList(params).then(res => {
        this.cityList = res.data.Result || []
        // this.cityCode = res.data.Result && res.data.Result.length > 0 ? res.data.Result[0].CityID : ''
        if (this.city) {
          console.log('11111111', this.city)
          let selectedCity = this.nameFilter(this.city, this.cityList, 'city')
          this.cityCode = selectedCity ? selectedCity.CityID : ''
        } else {
          this.cityCode = res.data.Result && res.data.Result.length > 0 ? res.data.Result[0].CityID : ''
          let selected = this.idFilter(this.cityCode, this.cityList, 'city')
          this.$emit('city', selected.CityName)
        }
      })
    },
    getAreaList (val) {
      let params = {
        cityId: val
      }
      GetAreaList(params).then(res => {
        this.areaList = res.data.Result || []
        this.areaCode = res.data.Result && res.data.Result.length > 0 ? res.data.Result[0].AreaID : ''
      })
    },
    idFilter (e, list, name) {
      if (name === 'province') {
        return list.find((item) => {
          return item.ProvinceID === e
        })
      }
      if (name === 'city') {
        return list.find((item) => {
          return item.CityID === e
        })
      }
      if (name === 'area') {
        return list.find((item) => {
          return item.AreaID === e
        })
      }
    },
    nameFilter (e, list, name) {
      if (name === 'province') {
        return list.find((item) => {
          return item.ProvinceName === e
        })
      }
      if (name === 'city') {
        return list.find((item) => {
          return item.CityName === e
        })
      }
      if (name === 'area') {
        return list.find((item) => {
          return item.AreaName === e
        })
      }
    },
    // 根据国家编码获取省份列表
    getProvinces () {
      if (this.provinceFlag) {
        return
      }
      this.getProList()
      this.provinceFlag = true
    },
    // 省份修改,拉取对应城市列表
    changeProvince (val) {
      this.getCityList(this.provinceCode)
      this.cityFlag = true
      this.cityCode = ''
      this.areaCode = ''
      let selected = this.idFilter(val, this.provinceList, 'province')
      this.$emit('province', selected.ProvinceName)
    },
    // 根据省份编码获取城市列表
    getCities () {
      if (this.cityFlag) {
        return
      }
      if (this.provinceCode) {
        this.getCityList(this.provinceCode)
        this.cityFlag = true
      }
    },
    // 城市修改,拉取对应区域列表
    changeCity (val) {
      this.getAreaList(this.cityCode)
      this.areaFlag = true
      this.areaCode = ''
      let selected = this.idFilter(val, this.cityList, 'city')
      this.$emit('city', selected.CityName)
      // let selected = this.idFilter(this.cityCode, this.cityList, 'city')
      // this.$emit('city', selected ? selected.CityName : '')
    },
    // 根据城市编码获取区域列表
    getAreas () {
      if (this.areaFlag) {
        return
      }
      if (this.cityCode) {
        this.getAreaList(this.cityCode)
      }
    },
    // 区域修改
    changeArea (val) {
      let selected = this.idFilter(val, this.areaList, 'area')
      this.$emit('area', selected.AreaName)
    },
    // 重置省市区/县编码
    reset () {
      this.provinceCode = ''
      this.cityCode = ''
      this.areaCode = ''
    },
    editCodeToList (code, name) {
      if (!code) return false
      if (name === 'province') {
        let selectedPro = this.nameFilter(code, this.provinceList, name)
        this.provinceCode = selectedPro ? selectedPro.ProvinceID : ''
        this.getCityList(this.provinceCode)
      }
    },
    editCityToList (code, name) {
      if (!code) return false
      if (name === 'city') {
        if (this.cityList && this.cityList.length > 0) {
          let selectedCity = this.nameFilter(code, this.cityList, name)
          this.cityCode = selectedCity ? selectedCity.CityID : ''
          if (!this.hideArea) {
            this.getAreaList(this.cityCode)
          }
        }
      }
    },
    editAreaToList (code, name) {
      if (!code) return false
      if (name === 'area') {
        if (this.areaList && this.areaList.length > 0) {
          let selectedArea = this.nameFilter(code, this.areaList, name)
          this.areaCode = selectedArea ? selectedArea.AreaID : ''
        }
      }
    },
    // 地址编码转换成省市区列表
    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 => {
        })
    }
 
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.map {
  height: 150px;
  width: 100%;
  overflow: hidden;
  margin-top: 5px;
}
 
.area-title {
  font-size: 12px;
  color: #aaa;
  padding-left: 10px;
}
 
.distpicker-address-wrapper /deep/ select {
  height: 34px;
  font-size: 12px;
  border-radius: 0.1rem;
  padding: .5rem .2rem;
}
.selecInput{
  min-width: 120px;
  margin-left: 10px;
}
</style>