// 云函数getMyOrders const cloud = require('wx-server-sdk') // 初始化cloud cloud.init({ env: 'cloud1-5gqeisa06659849a' // 使用您的云环境ID }) const db = cloud.database() const _ = db.command const $ = db.command.aggregate exports.main = async (event, context) => { const wxContext = cloud.getWXContext() const openid = wxContext.OPENID // 获取请求参数 const { type = 'all', // 订单类型:all, secondhand status = 'all', // 订单状态:all, waiting, processing, completed, cancelled role = 'buyer', // 角色:buyer(买家), publisher(卖家) pageNum = 1, pageSize = 10 } = event // 构建查询条件 let query = {} // 按角色筛选 if (role === 'buyer') { query.buyerOpenid = openid } else if (role === 'publisher') { query.publisherOpenid = openid } // 按类型筛选 if (type !== 'all') { query.type = type } // 按状态筛选 if (status !== 'all') { query.status = status } try { // 计算总数 const countResult = await db.collection('orders') .where(query) .count() const total = countResult.total // 查询订单数据 const orderResult = await db.collection('orders') .where(query) .orderBy('createTime', 'desc') .skip((pageNum - 1) * pageSize) .limit(pageSize) .get() // 获取关联的商品信息 const orders = orderResult.data const ordersWithDetails = [] for (const order of orders) { let detailCollection = '' if (order.type === 'secondhand') { detailCollection = 'secondhand_goods' } try { // 获取关联信息 if (detailCollection) { const detailResult = await db.collection(detailCollection) .doc(order.itemId) .field({ title: true, description: true, images: true, price: true }) .get() ordersWithDetails.push({ ...order, itemDetail: detailResult.data || {} }) } else { ordersWithDetails.push(order) } } catch (error) { // 如果获取详情失败,仍然返回订单基本信息 ordersWithDetails.push(order) } } return { success: true, data: ordersWithDetails, total, pageNum, pageSize, hasMore: total > pageNum * pageSize } } catch (error) { return { success: false, message: '获取订单失败', error: error.message } } }