import { models, db, _ } from '../../utils/cloudbase.js' Page({ data: { categoriesindex: 1, categories: [ { title: '全部', type: 1, }, { title: '待支付', type: 2, }, { title: '待收货', type: 3, }, { title: '已完成', type: 4, } ], searchText: '', // 搜索关键词 orders: [], souimg: '', pageNumber: 1, pageSize: 10, hasMore: true, // 是否还有更多数据 isLoading: false, // 防止多次触发 }, onLoad(options) { const type = Number(options.type) || 1; console.log('收到的 type 参数:', type); // 根据 type 加载数据 this.setData({ categoriesindex: type }, () => { this.getdatalist(); // 确保 this 指向正确 }); 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); }, // 模糊搜索 onSearchInput(e) { const value = e.detail.value.trim(); this.setData({ searchText: value, pageNumber: 1, hasMore: true, orders: [] }, () => { this.getdatalist(); }); }, async getdatalist(isLoadMore = false) { if (this.data.isLoading || !this.data.hasMore) return; this.setData({ isLoading: true }); const { pageNumber, pageSize, categoriesindex, searchText, orders } = this.data; let dbRegExp = null; let jsRegExp = null; if (searchText) { dbRegExp = db.RegExp({ regexp: searchText, options: 'i' }); jsRegExp = new RegExp(searchText, 'i'); } // 构造订单表的筛选条件 // 获取本地存储的用户信息 const userInfo = wx.getStorageSync('userInfo'); const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; // 根据你的userInfo结构取ID let orderWhere = { user_id: userId, // 每次必传 school_id: schoolId }; if (categoriesindex !== 1) { orderWhere.status = categoriesindex - 2; } if (dbRegExp) { orderWhere = _.and([ orderWhere, { order_id: dbRegExp } ]); } try { // 第一步:查订单表 const { data } = await models.orders.list({ filter: { where: orderWhere }, pageSize, pageNumber, getCount: true, envType: 'prod' }); const orderList = data.records || []; if (orderList.length === 0) { this.setData({ hasMore: false, isLoading: false }); return; } // 第二步:查商品表 const fileManagerIds = orderList.map(item => item.merchandise_id); const fileDetailPromises = fileManagerIds.map(id => { return models.wx_merchandise.list({ filter: { where: { _id: id } }, envType: 'prod' }).then(res => res.data?.records?.[0] || null) .catch(err => { console.error(`商品获取失败: ${id}`, err); return null; }); }); const fileDetails = await Promise.all(fileDetailPromises); // 第三步:合并数据,并进行模糊搜索筛选(匹配一个就保留) const mergedOrders = []; for (let i = 0; i < orderList.length; i++) { const order = orderList[i]; const merch = fileDetails[i]; if (!merch) continue; if (jsRegExp) { const matchOrderId = jsRegExp.test(order._id || ''); const matchName = jsRegExp.test(merch.name || ''); if (!matchOrderId && !matchName) { continue; // 都没匹配上,跳过 } } // 先把 _id 改名为 order_id const { _id, school_id, ...restOrder } = order; const orderRenamed = { ...restOrder, orders_id: _id, schools_id: school_id }; mergedOrders.push({ ...orderRenamed, ...merch }); } // 第四步:更新数据 this.setData({ orders: isLoadMore ? orders.concat(mergedOrders) : mergedOrders, pageNumber: pageNumber + 1, hasMore: orderList.length === pageSize, isLoading: false }); } catch (err) { console.error('获取数据失败:', err); this.setData({ isLoading: false }); } }, tabcategories(e) { const type = e.currentTarget.dataset.type; console.log('type:', type); this.setData({ categoriesindex: type, searchText: '', // ✅ 清空搜索关键词 pageNumber: 1, // 重置页码 hasMore: true, // 重置是否有更多数据 orders: [], // 清空旧数据 }, () => { console.log('categoriesindex:', this.data.categoriesindex); this.getdatalist(false); }); }, // 处理第一个按钮的点击事件 handleAction1: function (event) { const index = event.currentTarget.dataset.index; // 获取点击的订单索引 const status = this.data.orders[index].status; // 获取订单状态 switch (status) { case 1: console.log("查看详情", index); const item = this.data.orders[index]; wx.navigateTo({ url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item)) }); // 执行查看详情的逻辑 break; case 0: console.log("取消订单", index); // 执行取消订单的逻辑 break; case 2: console.log("申请售后", index); // 执行申请售后的逻辑 const items = this.data.orders[index]; console.log(items, 'itemsitemsitemsitems'); wx.navigateTo({ url: '/subpackages/afterservice/afterservice?data=' + encodeURIComponent(JSON.stringify(items)) }); break; default: console.log("未知状态"); } }, // 处理第二个按钮的点击事件 handleAction2: function (event) { const index = event.currentTarget.dataset.index; // 获取点击的订单索引 const status = this.data.orders[index].status; // 获取订单状态 switch (status) { case 1: console.log("确认收货", index); this.getyesordels(this.data.orders[index]) // 执行确认收货的逻辑 break; case 0: console.log("立即支付", index); // 执行立即支付的逻辑 break; case 2: console.log("再来一单", index); // 执行再来一单的逻辑 break; default: console.log("未知状态"); } }, // 确认收货 async getyesordels (list) { console.log(list, 'list'); const userInfo = wx.getStorageSync('userInfo'); const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; // 根据你的userInfo结构取ID const { data } = await models.orders.update({ data: { status: 2, // 状态 }, filter: { where: { $and: [ { merchandise_id: { $eq: list._id, // 推荐传入_id数据标识进行操作 }, }, { user_id: { $eq: userId, // 推荐传入_id数据标识进行操作 }, }, { school_id: { $eq: schoolId, // 推荐传入_id数据标识进行操作 }, }, ] } }, envType: "prod", }); console.log(data.count, 'data.count'); if (Number(data.count) > 0) { wx.showToast({ title: '确认收货成功', icon: 'success', duration: 1500 }); // ✅ 重新获取数据 this.setData({ pageNumber: 1, hasMore: true, orders: [] }, () => { this.getdatalist(false); }); } else { wx.showToast({ title: '确认收货失败', icon: 'error', duration: 1500 }); } }, goToGoodsList (e) { const index = e.currentTarget.dataset.type; const item = this.data.orders[index]; // console.log(e.currentTarget, 'item'); wx.navigateTo({ url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item)) }); }, // async adds () { // const { data } = await models.orders.create({ // data: { // adresses_id: "BWSLJAGVZA", // 地址id // sys_updateby_id: "", // 更新人 // merchandise_id: "BULGAD2DA8", // 商品id // real_money: 223.12, // 实际付款金额 // updat_adress: "", // 修改后地址 // update_phone: "", // 修改后电话 // delete: 0, // 逻辑删除 // way: 1, // 收货方式 // update_name: "", // 修改后收货人 // school_id: "BU5N9CFX2Q", // 学校_id // user_id: "BWYRRKQVNY", // 用户_id // num_index: 1, // 商品数据 // tracking_number: "物流单号888", // 物流单号 // order_id: "订单号888", // 订单号 // status: 2, // 状态 // }, // // envType: pre 体验环境, prod 正式环境 // envType: "prod", // }); // // 返回创建的数据 id // console.log(data); // } });