|
|
@@ -11,6 +11,37 @@ import {
|
|
|
deleteAddressMethod,
|
|
|
updateAddressMethod,
|
|
|
} from "../../request";
|
|
|
+
|
|
|
+// 将 @vant/area-data 扁平格式转为 TDesign Cascader 树形格式
|
|
|
+function convertAreaDataToTree(areaList: { province_list: Record<string, string>; city_list: Record<string, string>; county_list: Record<string, string> }) {
|
|
|
+ const { province_list, city_list, county_list } = areaList;
|
|
|
+
|
|
|
+ const cityMap: Record<string, any[]> = {};
|
|
|
+ for (const cityCode in city_list) {
|
|
|
+ const prefix = cityCode.substring(0, 2);
|
|
|
+ if (!cityMap[prefix]) cityMap[prefix] = [];
|
|
|
+ cityMap[prefix].push({ label: city_list[cityCode], value: cityCode, children: [] });
|
|
|
+ }
|
|
|
+
|
|
|
+ const countyMap: Record<string, any[]> = {};
|
|
|
+ for (const countyCode in county_list) {
|
|
|
+ const prefix = countyCode.substring(0, 4);
|
|
|
+ if (!countyMap[prefix]) countyMap[prefix] = [];
|
|
|
+ countyMap[prefix].push({ label: county_list[countyCode], value: countyCode });
|
|
|
+ }
|
|
|
+
|
|
|
+ const result: any[] = [];
|
|
|
+ for (const provinceCode in province_list) {
|
|
|
+ const children = (cityMap[provinceCode.substring(0, 2)] || []).map((city: any) => ({
|
|
|
+ ...city,
|
|
|
+ children: countyMap[city.value.substring(0, 4)] || [],
|
|
|
+ }));
|
|
|
+ if (children.length > 0) {
|
|
|
+ result.push({ label: province_list[provinceCode], value: provinceCode, children });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
// module/diet/pages/delivery-address/delivery-address.ts
|
|
|
// import parseAddress from '../../utils/smart-parse-address';
|
|
|
Page({
|
|
|
@@ -20,6 +51,7 @@ Page({
|
|
|
data: {
|
|
|
type: "",
|
|
|
areaList,
|
|
|
+ areaTreeData: convertAreaDataToTree(areaList),
|
|
|
id: "",
|
|
|
formData: {
|
|
|
id: "",
|
|
|
@@ -52,21 +84,23 @@ Page({
|
|
|
this.setData({ detail: "" });
|
|
|
},
|
|
|
onRegionConfirm(e: any) {
|
|
|
+ const selectedOptions = e.detail.selectedOptions;
|
|
|
+ if (!selectedOptions || selectedOptions.length < 3) return;
|
|
|
this.setData({
|
|
|
region:
|
|
|
- e.detail.values[0].name +
|
|
|
+ selectedOptions[0].label +
|
|
|
", " +
|
|
|
- e.detail.values[1].name +
|
|
|
+ selectedOptions[1].label +
|
|
|
"," +
|
|
|
- e.detail.values[2].name,
|
|
|
- regionValue: e.detail.values,
|
|
|
+ selectedOptions[2].label,
|
|
|
+ regionValue: e.detail.value,
|
|
|
regionPickerVisible: false,
|
|
|
- "formData.provinceCode": e.detail.values[0].code,
|
|
|
- "formData.provinceName": e.detail.values[0].name,
|
|
|
- "formData.cityCode": e.detail.values[1].code,
|
|
|
- "formData.cityName": e.detail.values[1].name,
|
|
|
- "formData.areaCode": e.detail.values[2].code,
|
|
|
- "formData.areaName": e.detail.values[2].name,
|
|
|
+ "formData.provinceCode": selectedOptions[0].value,
|
|
|
+ "formData.provinceName": selectedOptions[0].label,
|
|
|
+ "formData.cityCode": selectedOptions[1].value,
|
|
|
+ "formData.cityName": selectedOptions[1].label,
|
|
|
+ "formData.areaCode": selectedOptions[2].value,
|
|
|
+ "formData.areaName": selectedOptions[2].label,
|
|
|
});
|
|
|
},
|
|
|
onRegionCancel(e: any) {
|