56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
//积分充值
|
|
module.exports = {
|
|
main: async (event) => {
|
|
let { data = {}, userInfo, util } = event;
|
|
let { db, _ } = util;
|
|
const { order_id } = data;
|
|
|
|
if (!order_id) {
|
|
return { code: -1, msg: '缺少订单ID' };
|
|
}
|
|
|
|
// 查订单
|
|
const orderRes = await db.collection('vk-pay-orders').doc(order_id).get();
|
|
if (!orderRes.data.length) {
|
|
return { code: -1, msg: '订单不存在' };
|
|
}
|
|
|
|
const order = orderRes.data[0];
|
|
if (order.status !== 1) {
|
|
return { code: -1, msg: '订单未支付' };
|
|
}
|
|
|
|
// 解析充值积分数量
|
|
const attachedData = JSON.parse(order.attached_data || '{}');
|
|
const points = attachedData.points;
|
|
if (!points) {
|
|
return { code: -1, msg: '订单数据异常' };
|
|
}
|
|
|
|
// 增加余额
|
|
await db.collection('jjx-user-points').doc(userInfo.uid).update({
|
|
balance: _.inc(points),
|
|
total_earned: _.inc(points),
|
|
update_time: new Date()
|
|
});
|
|
|
|
// 查最新余额
|
|
const userRes = await db.collection('jjx-user-points').doc(userInfo.uid).get();
|
|
const balance = userRes.data[0].balance;
|
|
|
|
// 记录流水
|
|
await db.collection('jjx-points-log').add({
|
|
user_id: userInfo.uid,
|
|
type: 'earn',
|
|
amount: points,
|
|
balance_after: balance,
|
|
reason: '积分充值(' + (order.total_fee / 100) + '元)',
|
|
biz_type: 'recharge',
|
|
biz_id: order_id,
|
|
_add_time: new Date()
|
|
});
|
|
|
|
return { code: 0, msg: 'ok', data: { balance, points_added: points } };
|
|
}
|
|
};
|