// import { models, db, _ } from '../../utils/cloudbase.js' import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js' Page({ data: { addresses: [], souimg: '', shanchu: '', xiugaiimg: '', dingweiimg: '', keyword: '', // 搜索关键词 editId: '', // 当前编辑地址ID }, onShow() { const addressInfo = wx.getStorageSync('addressInfo') if (addressInfo) { if (this.data.editId) { // 编辑 console.log('编辑编辑'); this.updateAddress(addressInfo, this.data.editId) } else { // 新增 this.dizhi(addressInfo) } wx.removeStorageSync('addressInfo') this.setData({ editId: '' }) } this.getdatalist() }, async onLoad(options) { // const title = options.title ? decodeURIComponent(options.title) : '商品列表'; // this.setData({ title }); // wx.setNavigationBarTitle({ title }); const fileIDs = [ 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/shanchu.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiugai.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/dingwei.png' ]; const fileList = await getTempFileURLs(fileIDs) this.setData({ souimg: fileList[0].tempFileURL, shanchu: fileList[1].tempFileURL, xiugaiimg: fileList[2].tempFileURL, dingweiimg: fileList[3].tempFileURL, }) // // 并发下载多个 fileID // Promise.all( // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID })) // ).then(results => { // // 每个 result 对应一个下载结果 // const tempFilePaths = results.map(r => r.tempFilePath); // console.log('全部下载成功:', tempFilePaths); // this.setData({ // souimg: tempFilePaths[0], // shanchu: tempFilePaths[1], // xiugaiimg: tempFilePaths[2], // dingweiimg: tempFilePaths[3], // }); // }).catch(err => { // console.error('有文件下载失败:', err); // }); }, // 模糊搜索 onSearchInput(e) { const keyword = e.detail.value.trim(); this.setData({ keyword }); this.getdatalist(); // 每次输入更新列表(也可节流优化) }, // 地址数据 async getdatalist() { const models = await getModels() const keyword = this.data.keyword; // 构建模糊查询 const filter = { where: {} }; if (keyword) { filter.where.$or = [ { province: { $regex_ci: keyword } }, { municipality: { $regex_ci: keyword } }, { district: { $regex_ci: keyword } }, { street: { $regex_ci: keyword } }, { detailed_address: { $regex_ci: keyword } }, { name: { $regex_ci: keyword } }, { phone: { $regex_ci: keyword } }, ]; } const { data } = await models.adresses.list({ filter, pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效 pageNumber: 1, // 第几页 getCount: true, // 开启用来获取总数 // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 返回查询到的数据列表 records 和 总数 total console.log(data); this.setData({ addresses: data.records }) }, handleEdit(e) { const index = e.currentTarget.dataset.index; // 处理编辑逻辑 }, handleDelete(e) { const index = e.currentTarget.dataset.index; // 处理删除逻辑 }, async dizhi(list) { const models = await getModels() const isLoggedIn = wx.getStorageSync('userInfo') || null; console.log(list, isLoggedIn, 'list'); // 解析省市区 const province = list.area?.[0]?.name || ''; const city = list.area?.[1]?.name || ''; const district = list.area?.[2]?.name || ''; const street = list.area?.[3]?.name || ''; // 街道可以是第四项,也可以自定义 const detailed_address = list.address || ''; // 插件返回的详细地址 try { const { data } = await models.adresses.create({ data: { detailed_address: detailed_address, // 详细地址 address: `${province}${city}${district}${detailed_address}`, // 拼接完整地址 province: province, municipality: city, district: district, street: street, phone: list.phone || '', // 手机号 name: list.receiver || '', // 收货人 default: list.default ? 0 : 1, // 默认地址 wx_user_id: isLoggedIn?._id || '', // 用户ID // label: list.label || '', // 标签 }, envType: "prod", }); console.log(data); if (data.id) { wx.showToast({ title: '新增成功', icon: 'success', }); this.getdatalist(); // 重新获取地址列表 } else { wx.showToast({ title: '新增失败', icon: 'none', }); } } catch (err) { console.error('新增地址失败', err); wx.showToast({ title: '新增失败', icon: 'none', }); } }, // 添加地址 async handleAddAddress() { console.log('添加地址'); wx.navigateTo({ url: 'plugin://address-form/index' }); }, // 删除地址 async onDelete(event) { const models = await getModels() const _ids = event.currentTarget.dataset.id; console.log(_ids, '_ids'); const { data } = await models.adresses.delete({ filter: { where: { $and: [ { _id: { $eq: _ids, // 推荐传入_id数据标识进行操作 }, }, ] } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 返回删除成功的条数 console.log(data); // 删除成功后,更新页面数据 if (data.count > 0) { wx.showToast({ title: '删除成功', icon: 'success', }); this.getdatalist(); // 重新获取地址列表 } else { wx.showToast({ title: '删除失败', icon: 'none', }); } }, // 编辑 onEdit(event) { console.log('编辑地址'); const item = event.currentTarget.dataset.id; console.log(item, '_ids'); this.setData({ editId: item._id }) // 构建 edit 对象 const editInfo = { receiver: item.name, phone: item.phone, address: item.detailed_address, area: [ { name: item.province }, { name: item.municipality }, { name: item.district }, { name: item.street } ], default: item.default === 0 ? true : false, label: item.label || '' }; console.log(editInfo, 'editInfo'); // ⚠️ 直接传 JSON 字符串,不要 encodeURIComponent wx.navigateTo({ url: `plugin://address-form/index?edit=${JSON.stringify(editInfo)}` }); }, async updateAddress(list, bianji_id) { const models = await getModels() const isLoggedIn = wx.getStorageSync('userInfo') || null; console.log(list, isLoggedIn, bianji_id, 'list'); // 解析省市区 const province = list.area?.[0]?.name || ''; const city = list.area?.[1]?.name || ''; const district = list.area?.[2]?.name || ''; const street = list.area?.[3]?.name || ''; // 街道可以是第四项,也可以自定义 const detailed_address = list.address || ''; // 插件返回的详细地址 try { const { data } = await models.adresses.update({ data: { detailed_address: detailed_address, // 详细地址 address: `${province}${city}${district}${detailed_address}`, // 拼接完整地址 province: province, municipality: city, district: district, street: street, phone: list.phone || '', // 手机号 name: list.receiver || '', // 收货人 default: list.default ? 0 : 1, // 默认地址 wx_user_id: isLoggedIn?._id || '', // 用户ID // label: list.label || '', // 标签 }, filter: { where: { $and: [ { _id: { $eq: bianji_id, // 推荐传入_id数据标识进行操作 }, }, ] } }, envType: "prod", }); console.log(data); if (data.count > 0) { wx.showToast({ title: '编辑成功', icon: 'success', }); this.getdatalist(); // 重新获取地址列表 } else { wx.showToast({ title: '编辑失败', icon: 'none', }); } } catch (err) { console.error('新增地址失败', err); wx.showToast({ title: '编辑失败', icon: 'none', }); } }, onSelect(e) { const item = e.currentTarget.dataset.item; const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; // 上一页 // 方式一:直接赋值给上一页 data prevPage.setData({ selectedAddress: item }); // 返回上一页 wx.navigateBack({ delta: 1 }); } });