Files
doodle-mini/cloudfunctions/worksheetsStatsUpdate/index.js
T
2026-05-06 17:28:51 +08:00

38 lines
1.0 KiB
JavaScript

const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const VALID_FIELDS = new Set(['likes', 'downloads']);
exports.main = async (event) => {
try {
const db = cloud.database();
const command = db.command;
const collection = db.collection('worksheets');
const id = String(event.id || '').trim();
const field = String(event.field || '').trim();
if (!id) throw new Error('worksheet id 不能为空');
if (!VALID_FIELDS.has(field)) throw new Error('统计字段不合法');
await collection.doc(id).update({
data: {
[field]: command.inc(1),
updatedAt: db.serverDate(),
},
});
return {
success: true,
data: { _id: id, field },
};
} catch (error) {
return {
success: false,
message:
error instanceof Error ? error.message : '更新统计失败',
};
}
};