品质统一在boxset设定
This commit is contained in:
@@ -164,12 +164,13 @@ async function addDataField(db, openid, field, amount) {
|
||||
* 消耗指定字段的数值
|
||||
* @param {Object} db 数据库实例
|
||||
* @param {string} openid 用户openid
|
||||
* @param {string} field 字段名
|
||||
* @param {number} amount 消耗的数量
|
||||
* @param {string|Object[]} field 字段名或字段对象数组 [{field: 字段名, amount: 数量}]
|
||||
* @param {number} [amount] 消耗的数量 (当field为字符串时使用)
|
||||
* @returns {Object} 操作结果
|
||||
*/
|
||||
async function spendDataField(db, openid, field, amount) {
|
||||
try {
|
||||
const _ = db.command;
|
||||
let user = await getOrCreaterUser(db, openid);
|
||||
if (!user) {
|
||||
return {
|
||||
@@ -178,27 +179,97 @@ async function spendDataField(db, openid, field, amount) {
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof amount !== 'number' || amount < 0) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "数量必须是正数"
|
||||
};
|
||||
// 处理单个字段的情况
|
||||
if (typeof field === 'string') {
|
||||
if (typeof amount !== 'number' || amount < 0) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "数量必须是正数"
|
||||
};
|
||||
}
|
||||
|
||||
const currentValue = user.data[field] || 0;
|
||||
if (currentValue < amount) {
|
||||
return {
|
||||
code: -6,
|
||||
msg: `${field} 不足, 当前: ${currentValue}, 需要: ${amount}`
|
||||
};
|
||||
}
|
||||
|
||||
return await addDataField(db, openid, field, -amount);
|
||||
}
|
||||
|
||||
const currentValue = user.data[field] || 0;
|
||||
if (currentValue < amount) {
|
||||
return {
|
||||
code: -6,
|
||||
msg: `${field} 不足, 当前: ${currentValue}, 需要: ${amount}`
|
||||
};
|
||||
// 处理多个字段的情况
|
||||
if (Array.isArray(field)) {
|
||||
const fieldsToSpend = field;
|
||||
|
||||
// 验证参数格式
|
||||
for (const item of fieldsToSpend) {
|
||||
if (!item.field || typeof item.amount !== 'number' || item.amount < 0) {
|
||||
return {
|
||||
code: -3,
|
||||
msg: "字段参数格式错误,需要 {field, amount} 结构,且amount必须是正数"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 检查所有资源是否足够
|
||||
for (const item of fieldsToSpend) {
|
||||
const currentValue = user.data[item.field] || 0;
|
||||
if (currentValue < item.amount) {
|
||||
return {
|
||||
code: -6,
|
||||
msg: `${item.field} 不足, 当前: ${currentValue}, 需要: ${item.amount}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 所有资源都足够,开始扣除
|
||||
const updateData = {};
|
||||
const changes = [];
|
||||
|
||||
for (const item of fieldsToSpend) {
|
||||
const currentValue = user.data[item.field] || 0;
|
||||
const newValue = Math.max(0, currentValue - item.amount);
|
||||
updateData[`data.${item.field}`] = _.set(newValue);
|
||||
|
||||
changes.push({
|
||||
field: item.field,
|
||||
old_value: currentValue,
|
||||
new_value: newValue,
|
||||
change: -item.amount
|
||||
});
|
||||
}
|
||||
|
||||
// 更新数据库
|
||||
updateData['last_save_time'] = _.set(Date.now());
|
||||
let updateRes = await db.collection(user_db_name).doc(user._id).update({
|
||||
data: updateData
|
||||
});
|
||||
|
||||
if (updateRes?.stats?.updated >= 1) {
|
||||
return {
|
||||
code: 200,
|
||||
data: changes,
|
||||
msg: "资源消耗成功"
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
code: -1,
|
||||
msg: "资源消耗失败"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return await addDataField(db, openid, field, -amount);
|
||||
return {
|
||||
code: -3,
|
||||
msg: "参数格式错误"
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`消耗 ${field} 错误:`, error);
|
||||
console.error(`消耗资源错误:`, error);
|
||||
return {
|
||||
code: -5,
|
||||
msg: `消耗 ${field} 错误: ${error.message}`
|
||||
msg: `消耗资源错误: ${error.message}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user