import { api_sysAreaQueryAllProvince, api_userReceiveAddressPage, api_userReceiveAddressRemove, api_userReceiveAddressSave, api_userReceiveAddressUpdate } from "../../api/new" // pages/address/index.ts Page({ /** * 页面的初始数据 */ data: { selectAddressId: '', // 选中地址编号 addressList: [] as any, addressShow: false, addressAfterLeave: false, showDialog: false, cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系 showArea: false, showAreaAfterLeave: false, areaList: [] as any, currentValues: [] as any, // 添加地址表单信息 id: "", name: '', phoneNumber: '', detailAddress: '', cityCode: null, cityName: "", provinceCode: null as any, provinceName: "", regionCode: null, regionName: "", }, /** * 生命周期函数--监听页面加载 */ onLoad(options: any) { if (options.receiveAddress) { this.setData({ id: options.receiveAddress }) } this.getAddress() this.getAreas() }, /** 地址列表 */ async getAddress() { try { const { data } = await api_userReceiveAddressPage({ page: 1, rows: -1 }) this.setData({ addressList: data.data.rows || [] }) } catch { // } }, /** 获取省市区 */ async getAreas() { try { const { data } = await api_sysAreaQueryAllProvince({}) const areaList: any = this.formateArea(data.data) const currentValues: any = [] if (areaList?.province_list) { // 获取第一个键值对 const firstKey = Object.keys(areaList?.province_list)[0]; // 通过键获取值 const firstValue = areaList?.province_list[firstKey]; currentValues.push({ code: firstKey, name: firstValue }) } if (areaList?.city_list) { // 获取第一个键值对 const firstKey = Object.keys(areaList?.city_list)[0]; // 通过键获取值 const firstValue = areaList?.city_list[firstKey]; currentValues.push({ code: firstKey, name: firstValue }) } if (areaList?.county_list) { // 获取第一个键值对 const firstKey = Object.keys(areaList?.county_list)[0]; // 通过键获取值 const firstValue = areaList?.county_list[firstKey]; currentValues.push({ code: firstKey, name: firstValue }) } console.log(areaList, currentValues) this.setData({ areaList, currentValues }) } 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.regionCode && this.data.cityCode) { const cityCodeObj = this.data.cacheArea.find((item: any) => { return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.cityCode }) return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : "" } return this.data.cityCode }, /** 显示选择地区 */ onShowAreaList() { this.setData({ showArea: true }) }, /** 关闭选择地区 */ onCloseAreaList() { this.setData({ showArea: false }) }, onAreaBeforeEnter() { this.setData({ showAreaAfterLeave: false }) }, onAreaAfterLeave() { this.setData({ showAreaAfterLeave: true }) }, /** 确定选择地区 */ submitArea(e: any) { const selectedOptions: any = e.detail.values this.setData({ provinceCode: selectedOptions[0].code, cityCode: selectedOptions[1].code, regionCode: selectedOptions[2]?.code || null, provinceName: selectedOptions[0].name || '', cityName: selectedOptions[1].name || '', regionName: selectedOptions[2]?.name || '', showArea: false, }) }, onShowAddress() { this.setData({ addressAfterLeave: false, addressShow: true }) }, onCloseAddress() { this.setData({ addressShow: false }) }, onAddressAfterLeave() { this.setData({ addressAfterLeave: true, selectAddressId: '', name: '', phoneNumber: '', detailAddress: '', cityCode: null, cityName: "", provinceCode: 0, provinceName: "", regionCode: null, regionName: "", }) }, /** Dialog 隐藏 */ onDialogClose() { this.setData({ showDialog: false }) }, /** 删除地址 */ onRemoveAddress(e: any) { this.setData({ showDialog: true, selectAddressId: e.target.dataset.id }) }, /** 修改地址 */ onUpdateAddress(e: any) { const id = e.target.dataset.id const addressInfo = this.data.addressList.find((item: any) => item.id === id) this.setData({ addressShow: true, addressAfterLeave: false, selectAddressId: addressInfo.id, name: addressInfo.name, phoneNumber: addressInfo.phoneNumber, detailAddress: addressInfo.detailAddress, cityCode: addressInfo.city, cityName: addressInfo.cityName, provinceCode: addressInfo.province, provinceName: addressInfo.provinceName, regionCode: addressInfo.region, regionName: addressInfo.regionName, }) }, /** 选择地址 */ onSelectAddress(e: any) { const id = e.currentTarget.dataset.id this.setData({ id }, () => { wx.navigateBack() }) }, /** Dialog 确定 */ async onDialogConfirm() { try { await api_userReceiveAddressRemove({ id: this.data.selectAddressId }) this.getAddress() // 如果删除的是已经选中的地址,则需要重置数据 if (this.data.selectAddressId === this.data.id) { this.setData({ id: '' }) } this.onDialogClose() } catch { } }, onUnload() { console.log('onUnload') const id = this.data.id const addressInfo = this.data.addressList.find((item: any) => item.id === id) const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; // 获取上一个页面实例 prevPage?.setData({ backParams: { receiveAddress: addressInfo?.id || '', receiveAddressInfo: { addressDetail: addressInfo?.id ? (addressInfo.provinceName || '') + (addressInfo.cityName || '') + (addressInfo.regionName || '') + addressInfo.detailAddress : '', name: addressInfo?.name, phoneNumber: addressInfo?.phoneNumber } } }); }, /** 创建/修改收货地址 */ async onOperationAddress() { const addressForm = this.data try { if (!addressForm.name) { wx.showToast({ title: '请输入收货人姓名', icon: "none" }) return } if (!addressForm.phoneNumber || !/^1[3456789]\d{9}$/.test(addressForm.phoneNumber)) { wx.showToast({ title: '请输入正确的手机号码', icon: "none" }) return } if (!addressForm.provinceCode || !addressForm.cityCode) { wx.showToast({ title: '请选择地区', icon: "none" }) return } if (!addressForm.detailAddress) { wx.showToast({ title: '请输入详细地址', icon: "none" }) return } const citycode = this.formateCityCode() const params = { name: addressForm.name, phoneNumber: addressForm.phoneNumber, province: addressForm.provinceCode, city: citycode, region: addressForm.regionCode || '', detailAddress: addressForm.detailAddress } if (addressForm.selectAddressId) { await api_userReceiveAddressUpdate({ id: addressForm.selectAddressId, ...params }) wx.showToast({ title: '修改成功', icon: 'none' }) } else { await api_userReceiveAddressSave({ ...params }) wx.showToast({ title: '添加成功', icon: 'none' }) } this.getAddress() this.onCloseAddress() } catch (e) { // console.log(e, '1212') } }, })