修复动态发布与互动问题

This commit is contained in:
2026-06-23 18:43:03 +08:00
parent 702b578e1e
commit cda8685956
151 changed files with 4993 additions and 914 deletions

View File

@@ -4,11 +4,25 @@ cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command
// Reading a collection that was never created throws in WeChat 云开发, and this
// function reads `matches` before it ever adds to it — so the collection would
// never get created and every "like" would fail. Create it up-front (no-op if it
// already exists) to break that deadlock.
async function ensureCollection(name) {
try {
await db.createCollection(name)
} catch (e) {
// Already-exists error is expected on every call after the first.
}
}
exports.main = async event => {
const { OPENID } = cloud.getWXContext()
const { petId } = event
if (!petId) throw new Error('petId is required')
await ensureCollection('matches')
// Get current user and their pets
const userRes = await db.collection('users').where({ openid: OPENID }).limit(1).get()
if (!userRes.data.length) throw new Error('user not found')