48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
//扣积分(合同审查用)
|
|
module.exports = {
|
|
main: async (event) => {
|
|
let { data = {}, userInfo, util } = event;
|
|
let { db, _ } = util;
|
|
const { amount, reason, biz_type, biz_id } = data;
|
|
|
|
if (!amount || amount <= 0) {
|
|
return { code: -1, msg: '扣减数量无效' };
|
|
}
|
|
|
|
// 查余额
|
|
const userRes = await db.collection('jjx-user-points').doc(userInfo.uid).get();
|
|
if (!userRes.data.length) {
|
|
return { code: -1, msg: '积分账户不存在,请先获取积分' };
|
|
}
|
|
|
|
const currentBalance = userRes.data[0].balance;
|
|
if (currentBalance < amount) {
|
|
return { code: -1, msg: '积分不足', data: { balance: currentBalance, needed: amount } };
|
|
}
|
|
|
|
const newBalance = currentBalance - amount;
|
|
|
|
// 扣减余额
|
|
await db.collection('jjx-user-points').doc(userInfo.uid).update({
|
|
balance: newBalance,
|
|
total_spent: _.inc(amount),
|
|
update_time: new Date()
|
|
});
|
|
|
|
// 记录流水
|
|
await db.collection('jjx-points-log').add({
|
|
user_id: userInfo.uid,
|
|
type: 'spend',
|
|
amount: -amount,
|
|
balance_after: newBalance,
|
|
reason: reason || '积分消耗',
|
|
biz_type: biz_type || 'other',
|
|
biz_id: biz_id || '',
|
|
_add_time: new Date()
|
|
});
|
|
|
|
return { code: 0, msg: 'ok', data: { balance: newBalance } };
|
|
}
|
|
};
|
|
|