feat: 完成分龄页开发
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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 : '更新统计失败',
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "worksheets-stats-update",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"wx-server-sdk": "^3.0.4"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user