import { models, db, _ } from '../../utils/cloudbase.js' Page({ data: { cartItems: [], souimg: '', pageNumber: 1, pageSize: 10, hasMore: true, // 是否还有更多数据 isLoading: false, // 防止多次触发 }, onShow() { // 列表数据 this.setData({ pageNumber: 1, hasMore: true, cartItems: [] }, () => { this.getdatalist() }); }, onLoad() { // // 列表数据 // this.setData({ // pageNumber: 1, // hasMore: true, // cartItems: [] // }, () => { // this.getdatalist() // }); const fileIDs = [ 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png', ]; // 并发下载多个 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], }); }).catch(err => { console.error('有文件下载失败:', err); }); }, onReachBottom() { // 上拉触底事件的处理函数 this.loadMore(); }, loadMore() { // 加载更多数据的逻辑 console.log('加载更多'); this.getdatalist(true); }, // 获取列表数据 async getdatalist(isLoadMore = false) { if (this.data.isLoading || !this.data.hasMore) return; this.setData({ isLoading: true }); const { pageNumber, pageSize } = this.data; try { const { data } = await models.shopping_cart.list({ filter: { where: {} }, pageSize, pageNumber, getCount: true, // 开启用来获取总数 // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 返回查询到的数据列表 records 和 总数 total console.log(data); const collectList = data.records || []; if (collectList.length === 0) { this.setData({ hasMore: false, isLoading: false }); return; } const fileManagerIds = collectList.map(item => item.merchandise_id); // 第二步:循环获取每个文件数据(单条查询) const fileDetailPromises = fileManagerIds.map(id => { return models.wx_merchandise.list({ filter: { where: { _id: id } }, envType: "prod" }).then(res => { const record = res.data?.records?.[0]; if (!record) { console.warn(`未找到 _id 为 ${id} 的文件`); } return record || null; }) .catch(err => { console.error(`获取文件 ${id} 失败`, err); return null; }); }); // 第三步:等待所有请求完成 const fileDetails = await Promise.all(fileDetailPromises); console.log(fileDetails, 'fileDetails'); // 第四步:过滤无效项,并添加 checked 字段 const newFiles = fileDetails .filter(Boolean) // 去掉 null 或 undefined .map((file, index) => { const historyRecord = collectList[index]; // 获取原 download_history 的记录 if (!file) return null; return { ...file, checked: false, download_history_id: historyRecord._id, specs_index: Number(historyRecord.specs_index), num: historyRecord.num // 关键:存 download_history 的 id }; }); this.setData({ cartItems: isLoadMore ? this.data.cartItems.concat(newFiles) : newFiles, pageNumber: pageNumber + 1, hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底 isLoading: false }, () => { this.updateTotalPrice(); }); } catch (err) { console.error('获取收藏文件失败:', err); this.setData({ isLoading: false }); } }, handleCheckboxChange(e) { const { value } = e.detail; const cartItems = this.data.cartItems.map(item => ({ ...item, checked: value.includes(item.download_history_id) })); this.setData({ cartItems }); this.updateTotalPrice(); }, handleAllCheckboxChange(e) { const { value } = e.detail; const allChecked = value.includes('all'); const cartItems = this.data.cartItems.map(item => ({ ...item, checked: allChecked })); this.setData({ cartItems, allChecked }); this.updateTotalPrice(); }, updateTotalPrice() { const totalPrice = this.data.cartItems.reduce((total, item) => { if (item.checked) { return total + item.price * item.num; } return total; }, 0); this.setData({ totalPrice }); }, handleCheckout() { // 过滤出已选中的商品 const selectedItems = this.data.cartItems.filter(item => item.checked); if (selectedItems.length === 0) { wx.showToast({ title: '请选择要结算的商品', icon: 'none' }); return; } // 将数据存入本地缓存 wx.setStorageSync('checkoutItems', selectedItems); wx.navigateTo({ url: '/subpackages/submitorder/submitorder' }); }, // 购买数量 onChange(e) { const { id } = e.currentTarget.dataset; const { detail } = e; const cartItems = this.data.cartItems.map(item => { if (item._id === id) { return { ...item, num: detail }; } return item; }); this.setData({ cartItems }, () => { this.updateTotalPrice(); }); } });