const cloud = require('wx-server-sdk'); cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); const VALID_STATUS = new Set(['draft', 'active', 'hidden']); exports.main = async (event) => { try { const db = cloud.database(); const collection = db.collection('worksheets'); const id = String(event.id || '').trim(); const status = String(event.status || '').trim(); if (!id) throw new Error('worksheet id 不能为空'); if (!VALID_STATUS.has(status)) throw new Error('状态不合法'); // Check worksheet exists try { await collection.doc(id).get(); } catch { throw new Error('worksheet 不存在'); } await collection.doc(id).update({ data: { status, updatedAt: db.serverDate(), }, }); return { success: true, data: { _id: id, status }, }; } catch (error) { return { success: false, message: error instanceof Error ? error.message : '更新状态失败', }; } };