import { api_sysAreaQueryAllProvince, api_userReceiveAddressUpdate, api_userReceiveAddressSave, api_userReceiveAddressDetail } from "../../api/login"; Component({ options: { styleIsolation: 'shared' }, properties: { popupShow: { type: Boolean, value: false }, editId: { type: String, value: "" } }, data: { name: "", phoneNumber: "", cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系 showArea: false, areaList: [] as any, // 省市区 province: "", city: "", region: "", provinceName: "", cityName: "", regionName: "", detailAddress: "" }, pageLifetimes: { show() { this.getAreas() }, }, observers: { async editId(newVal: any) { if (newVal) { try { const { data } = await api_userReceiveAddressDetail(newVal) if (data.code === 200) { const params = data.data this.setData({ phoneNumber: params.phoneNumber, name: params.name, province: params.province, city: params.city, region: params.region || "", provinceName: params.provinceName, cityName: params.cityName, regionName: params.regionName, detailAddress: params.detailAddress }) // 回显市区 this.setData({ city: this.formateCityCode(true) }) } } catch (e: any) { console.log(e, 888) } } } }, methods: { onDialogClose() { this.setData({ popupShow: false, name: "", phoneNumber: "", province: "", city: "", region: "", provinceName: "", cityName: "", regionName: "", detailAddress: "" }) }, /** 显示选择地区 */ onShowAreaList() { this.setData({ showArea: true }) }, /** 关闭选择地区 */ onCloseAreaList() { this.setData({ showArea: false }) }, /** 确定选择地区 */ submitArea(e: any) { const selectedOptions: any = e.detail.values if (!selectedOptions || !selectedOptions[0]) { wx.showToast({ title: '未选中值', icon: 'none' }) return } if (selectedOptions[0].code == this.data.province && selectedOptions[1].code == this.data.city && (selectedOptions[2]?.code || "") == this.data.region) { this.setData({ showArea: false }) return } this.setData({ province: selectedOptions[0].code, city: selectedOptions[1].code, region: selectedOptions[2]?.code || "", provinceName: selectedOptions[0].name, cityName: selectedOptions[1].name, regionName: selectedOptions[2]?.name || "", showArea: false }) }, /** 获取省市区 */ async getAreas() { try { const { data } = await api_sysAreaQueryAllProvince({}) this.setData({ areaList: this.formateArea(data.data) }) } catch { // } }, formateArea(area: any[]) { const province_list: { [_: string]: string } = {}; const city_list: { [_: string]: string } = {}; const county_list: { [_: string]: string } = {}; area.forEach((item: any) => { province_list[item.code] = item.name; }); area.forEach((item: any) => { item.areas && item.areas.forEach((city: any, index: number) => { let code = city.code + "" // 某些数据不标准 这里需要转换一下 if (code[4] !== "0" || code[5] !== "0") { // 现在把区域的数据改为市的 const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00"; this.data.cacheArea.push({ cityCode: code, shiftCityCode: newCode }) code = newCode } city_list[code] = city.name; }); }); area.forEach((item: any) => { item.areas && item.areas.forEach((city: any) => { city.areas && city.areas.forEach((county: any) => { county_list[county.code] = county.name; }); }); }); return { province_list, city_list, county_list }; }, // 转换 formateCityCode(reverse?: boolean) { if (!this.data.region && this.data.city) { const cityCodeObj = this.data.cacheArea.find((item: any) => { return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.city }) return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : "" } return this.data.city }, /** 最终提交 */ async onSubmit() { try { const params = this.data if (!params.name) { wx.showToast({ title: '请输入收货人', icon: "none" }) return } if (!params.phoneNumber || !/^1[3456789]\d{9}$/.test(params.phoneNumber)) { wx.showToast({ title: '请输入正确的手机号', icon: "none" }) return } if (!params.province || !params.city) { wx.showToast({ title: '请选择地区', icon: "none" }) return } if (!params.detailAddress) { wx.showToast({ title: '请输入详细地址', icon: "none" }) return } wx.showLoading({ mask: true, title: "", }); // 转换CityCode const citycode = this.formateCityCode() // 编辑 let id if (params.editId) { id = params.editId await api_userReceiveAddressUpdate({ id: params.editId, phoneNumber: params.phoneNumber, name: params.name, province: params.province, city: citycode, region: params.region, detailAddress: params.detailAddress, defaultStatus: false, postCode: "", }) } else { const { data } = await api_userReceiveAddressSave({ phoneNumber: params.phoneNumber, name: params.name, province: params.province, city: citycode, region: params.region, detailAddress: params.detailAddress, defaultStatus: false, postCode: "", }) id = data.data } wx.hideLoading() wx.showToast({ title: '保存成功', icon: 'none' }) this.triggerEvent('addAddress', { addressInfo: { id, name: params.name, phoneNumber: params.phoneNumber, addressDes: params.provinceName + params.cityName + (params.regionName || "") + params.detailAddress } }, {}) this.onDialogClose() } catch { wx.hideLoading() // } }, } })