"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
|