陶杰
2025-01-09 cc1ee1fb090b8344faab02537dec80995cec93fe
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
<template>
  <div class="area-select">
    <el-button v-if="!disabled" class="mb-10" type="primary" @click="openDialog"
      >选择</el-button
    >
    <div class="tag-list">
      <el-tag
        v-for="item in value"
        :key="item.city"
        type="primary"
        :closable="!disabled"
        @close="onTagClose(item)"
        >{{ item.province }}-{{ item.city }}</el-tag
      >
    </div>
    <el-dialog
      title="选择地区"
      :visible.sync="dialogVisible"
      append-to-body
      :close-on-click-modal="false"
    >
      <div
        v-for="item in districtList"
        :key="item.code"
        class="mb-10 p-10 border-dashed border-[#eee]"
      >
        <el-bus-checkbox
          v-model="item.selected"
          has-select-all
          :from-dict="false"
          :options="item.children"
          :props="{ label: 'name', value: 'code', selectAllLabel: item.name }"
        ></el-bus-checkbox>
      </div>
      <div slot="footer" class="flex items-center justify-between">
        <el-checkbox v-model="allChecked" @change="onCheckedChange"
          >全选</el-checkbox
        >
        <div>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onConfirm">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import cloneDeep from 'lodash.clonedeep'
export default {
  props: {
    value: {
      type: Array,
      default: () => [],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      list: [],
      districtList: [],
      dialogVisible: false,
      allChecked: false,
    }
  },
  mounted() {
    this.getDistrictList()
  },
  methods: {
    async getDistrictList() {
      if (this.districtList.length === 0) {
        const { code, data } = await this.$services.base.getAreaJson()
        if (code === 0) {
          const list = JSON.parse(data)
          this.deleteRegion(list)
          this.districtList = list
        }
      }
    },
    deleteRegion(list) {
      list.forEach((province) => {
        if (Array.isArray(province.children)) {
          province.children.forEach((city) => {
            city.parentName = province.name
            if ('children' in city) {
              delete city.children
            }
          })
        }
      })
    },
    getAreaStatus() {
      return new Promise((resolve) => {
        if (this.districtList.length > 0) {
          resolve()
        } else {
          const timer = setInterval(() => {
            if (this.districtList.length > 0) {
              resolve()
              clearTimeout(timer)
            }
          }, 100)
        }
      })
    },
    async openDialog() {
      await this.getAreaStatus()
      this.setSelectedCity()
      this.dialogVisible = true
    },
    onCheckedChange(e) {
      if (e) {
        this.districtList.forEach((province) => {
          if (Array.isArray(province.children)) {
            province.selected = province.children.map((i) => i.code)
          }
        })
      } else {
        this.districtList.forEach((province) => {
          province.selected = []
        })
      }
    },
    // 根据当前value选中弹出框中的城市
    setSelectedCity() {
      this.districtList.forEach((province) => {
        const selectedCity = this.value
          .filter((i) => i.province === province.code)
          .map((i) => i.city)
        province.selected = selectedCity
      })
      this.districtList = cloneDeep(this.districtList)
    },
    onConfirm() {
      const value = this.districtList.reduce((total, current) => {
        if (Array.isArray(current.selected) && current.selected.length > 0) {
          total = total.concat(
            current.selected.reduce((t, c) => {
              t.push({ province: current.code, city: c })
              return t
            }, [])
          )
        }
        return total
      }, [])
      this.$emit('input', value)
      this.dialogVisible = false
    },
    onTagClose(item) {
      const value = this.value.filter(
        (i) => i.province !== item.province || i.city !== item.city
      )
      this.$emit('input', value)
    },
  },
}
</script>
 
<style lang="scss" scoped>
.area-select {
  width: 100%;
  .tag-list {
    .el-tag {
      margin-right: 6px;
      margin-bottom: 6px;
    }
  }
}
</style>