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, showArea: false, areaList: [] as any, currentValues: [] as any, // 添加地址表单信息 id: "", name: '', phoneNumber: '', detailAddress: '', cityCode: 0, cityName: "", provinceCode: 0, provinceName: "", regionCode: '', 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 = [] 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) => { city_list[city.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 }; }, /** 显示选择地区 */ onShowAreaList() { this.setData({ showArea: true }) }, /** 关闭选择地区 */ onCloseAreaList() { this.setData({ showArea: false }) }, /** 确定选择地区 */ submitArea(e: any) { const selectedOptions: any = e.detail.values this.setData({ provinceCode: selectedOptions[0].code, cityCode: selectedOptions[1].code, regionCode: selectedOptions[2].code, 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, name: '', phoneNumber: '', detailAddress: '', cityCode: 0, cityName: "", provinceCode: 0, provinceName: "", regionCode: '', 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, id: 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 || !addressForm.regionCode) { wx.showToast({ title: '请选择地区', icon: "none" }) return } if (!addressForm.detailAddress) { wx.showToast({ title: '请输入详细地址', icon: "none" }) return } const params = { name: addressForm.name, phoneNumber: addressForm.phoneNumber, province: addressForm.provinceCode, city: addressForm.cityCode, region: addressForm.regionCode, detailAddress: addressForm.detailAddress } if (addressForm.id) { await api_userReceiveAddressUpdate({ id: addressForm.id, ...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') } }, })