WXL
2 天以前 c80bc467a41daa6cbae4e5515a300a8ca98cfeaa
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
"use strict";
const common_vendor = require("../../common/vendor.js");
if (!Array) {
  const _easycom_u_picker2 = common_vendor.resolveComponent("u-picker");
  _easycom_u_picker2();
}
const _easycom_u_picker = () => "../../uni_modules/uview-plus/components/u-picker/u-picker.js";
if (!Math) {
  _easycom_u_picker();
}
const _sfc_main = {
  __name: "index",
  props: {
    modelValue: {
      type: Object,
      default: () => ({})
    }
  },
  emits: ["update:modelValue", "change"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const pickerShow = common_vendor.ref(false);
    const columns = common_vendor.ref([[], [], []]);
    const pickerRef = common_vendor.ref(null);
    const provinceList = common_vendor.ref([]);
    const cityMap = common_vendor.ref(/* @__PURE__ */ new Map());
    const districtMap = common_vendor.ref(/* @__PURE__ */ new Map());
    const displayText = common_vendor.computed(() => {
      const { sheng, shi, qu } = props.modelValue;
      if (sheng && shi && qu)
        return `${sheng} ${shi} ${qu}`;
      if (sheng && shi)
        return `${sheng} ${shi}`;
      if (sheng)
        return sheng;
      return "";
    });
    const loadRegionData = async () => {
      try {
        const res = await common_vendor.index.$uapi.get("/project/dict/treeselect");
        if (res.code === 200 && res.data) {
          buildMappings(res.data);
        } else {
          common_vendor.index.__f__("error", "at components/AreaSelect/index.vue:57", "获取地区数据失败", res);
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at components/AreaSelect/index.vue:60", "地区数据接口异常", error);
      }
    };
    const buildMappings = (treeData) => {
      provinceList.value = [];
      cityMap.value.clear();
      districtMap.value.clear();
      treeData.forEach((province) => {
        const provinceName = province.areaname;
        const provinceCode = province.areacode;
        const cities = [];
        if (province.subarea && province.subarea.length) {
          province.subarea.forEach((city) => {
            const cityName = city.areaname;
            const cityCode = city.areacode;
            const districts = [];
            if (city.subarea && city.subarea.length) {
              city.subarea.forEach((district) => {
                districts.push({
                  name: district.areaname,
                  code: district.areacode
                });
              });
            }
            districtMap.value.set(cityCode, districts);
            cities.push({
              name: cityName,
              code: cityCode,
              districts
            });
          });
        }
        provinceList.value.push({
          name: provinceName,
          code: provinceCode,
          cities
        });
        cityMap.value.set(provinceCode, cities);
      });
      const firstColumn = provinceList.value.map((p) => p.name);
      columns.value = [firstColumn, [], []];
    };
    const onChange = (e) => {
      const { columnIndex, value, index, picker = pickerRef.value } = e;
      if (columnIndex === 0) {
        const province = provinceList.value[index];
        if (province && province.cities) {
          const cityNames = province.cities.map((c) => c.name);
          picker.setColumnValues(1, cityNames);
          picker.setColumnValues(2, []);
        } else {
          picker.setColumnValues(1, []);
          picker.setColumnValues(2, []);
        }
      } else if (columnIndex === 1) {
        const provinceIdx = e.values[0] ? provinceList.value.findIndex((p) => p.name === e.values[0]) : -1;
        if (provinceIdx !== -1) {
          const province = provinceList.value[provinceIdx];
          const city = province.cities[index];
          if (city && city.districts) {
            const districtNames = city.districts.map((d) => d.name);
            picker.setColumnValues(2, districtNames);
          } else {
            picker.setColumnValues(2, []);
          }
        } else {
          picker.setColumnValues(2, []);
        }
      }
    };
    const openPicker = async () => {
      pickerShow.value = true;
      await common_vendor.nextTick$1();
      const picker = pickerRef.value;
      if (!picker)
        return;
      const { sheng, shi, qu } = props.modelValue;
      if (sheng) {
        const provinceIdx = provinceList.value.findIndex((p) => p.name === sheng);
        if (provinceIdx !== -1) {
          const province = provinceList.value[provinceIdx];
          picker.setIndexes([provinceIdx, 0, 0]);
          const cityNames = province.cities.map((c) => c.name);
          picker.setColumnValues(1, cityNames);
          if (shi) {
            const cityIdx = province.cities.findIndex((c) => c.name === shi);
            if (cityIdx !== -1) {
              picker.setIndexes([provinceIdx, cityIdx, 0]);
              const city = province.cities[cityIdx];
              const districtNames = city.districts.map((d) => d.name);
              picker.setColumnValues(2, districtNames);
              if (qu) {
                const districtIdx = city.districts.findIndex((d) => d.name === qu);
                if (districtIdx !== -1) {
                  picker.setIndexes([provinceIdx, cityIdx, districtIdx]);
                }
              }
            }
          }
        } else {
          picker.setColumnValues(1, []);
          picker.setColumnValues(2, []);
          picker.setIndexes([0, 0, 0]);
        }
      } else {
        picker.setColumnValues(1, []);
        picker.setColumnValues(2, []);
        picker.setIndexes([0, 0, 0]);
      }
    };
    const onConfirm = (e) => {
      const { value: values, indexes } = e;
      const provinceName = values[0];
      const cityName = values[1];
      const districtName = values[2];
      const province = provinceList.value.find((p) => p.name === provinceName);
      if (province) {
        const city = province.cities.find((c) => c.name === cityName);
        const district = city ? city.districts.find((d) => d.name === districtName) : null;
        const newValue = {
          sheng: province.name,
          shi: city ? city.name : "",
          qu: district ? district.name : "",
          provinceCode: province.code,
          cityCode: city ? city.code : "",
          districtCode: district ? district.code : ""
        };
        emit("update:modelValue", newValue);
        emit("change", newValue);
      }
      pickerShow.value = false;
    };
    __expose({ refresh: loadRegionData });
    common_vendor.onMounted(() => {
      loadRegionData();
    });
    return (_ctx, _cache) => {
      return {
        a: common_vendor.t(displayText.value || "请选择省/市/区"),
        b: !displayText.value ? 1 : "",
        c: common_vendor.o(openPicker),
        d: common_vendor.sr(pickerRef, "6b7e097d-0", {
          "k": "pickerRef"
        }),
        e: common_vendor.o(onConfirm),
        f: common_vendor.o(onChange),
        g: common_vendor.o(($event) => pickerShow.value = false),
        h: common_vendor.p({
          show: pickerShow.value,
          columns: columns.value,
          keyName: "name",
          title: "选择地区"
        }),
        i: common_vendor.gei(_ctx, "")
      };
    };
  }
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-6b7e097d"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/components/AreaSelect/index.js.map