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 : '更新统计失败', }; } };