我的云函数代码如下:
const cloud = require('wx-server-sdk');
cloud.init({ env: 'nfivecoffe-0gbqwn2x7ff0e7b2' });
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
const { outTradeNo } = event;
let resultAll = [];
try {
// 更新订单状态
await db.collection("orderUser").where({
pay_status: "wait",
orderNumber: outTradeNo
}).update({
data: {
pay_status: "订单完成"
}
});
// 查询订单信息
const res = await db.collection("orderUser").where({
orderNumber: outTradeNo
}).get();
const order = res.data[0];
if (!order) {
return {
errcode: 1,
errmsg: "订单未找到",
};
}
// 准备标签打印数据
const allOrderGroups = order.orderGroups;
const tempNowTemp = allOrderGroups.map(element => {
const orderShuXingStr = element.orderShuXing.map(el => `#${el.value}`).join('');
return {
sumNum: element.orderNum,
title: element.goodGroups.shopTitle,
tuple: `(${orderShuXingStr})`,
order: order.orderCode,
time: order.subTime,
price: element.priceAll,
style: "小程序点单"
};
});
// 准备小票打印数据
const tupleInfoArray = allOrderGroups.map(element => {
const orderShuXingStr = element.orderShuXing.map(el => `#${el.value}`).join('');
return {
tuple: orderShuXingStr,
title: element.goodGroups.shopTitle,
num: element.orderNum
};
});
const printLabelObj = {
payStyle: "小程序微信支付",
time: order.subTime,
ordercode: order.orderNumber,
takecode: order.orderCode,
info: tupleInfoArray,
price: order.order_price,
phone: order.phone,
name: order.userName,
remarks: order.orderRemarks
};
// 标签打印
const printBiaoQianResult = await cloud.callFunction({
name: 'printBiaoQian',
data: {
allOrder: tempNowTemp
}
});
resultAll.push({ name: "标签打印", value: printBiaoQianResult.result });
// 小票打印
const printerResult = await cloud.callFunction({
name: 'printer',
data: {
payStyle: printLabelObj.payStyle,
time: printLabelObj.time,
ordercode: printLabelObj.ordercode,
takecode: printLabelObj.takecode,
info: printLabelObj.info,
price: printLabelObj.price,
phone: printLabelObj.phone,
name: printLabelObj.name,
remarks: printLabelObj.remarks
}
});
resultAll.push({ name: "小票打印", value: printerResult.result });
// 返回指定格式的对象
return {
errcode: 0,
errmsg: "支付处理完成",
resultAll: resultAll,
};
} catch (error) {
console.error("支付处理出错", error);
return {
errcode: 500,
errmsg: "支付处理出错",
resultAll: resultAll
};
}
};