const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const SCAN_LIMIT = 200 const TOP_N = 10 exports.main = async (event, context) => { try { const res = await db.collection('posts') .orderBy('createdAt', 'desc') .limit(SCAN_LIMIT) .field({ hashtags: true }) .get() const counts = {} res.data.forEach(p => { ;(p.hashtags || []).forEach(tag => { if (!tag) return counts[tag] = (counts[tag] || 0) + 1 }) }) const topics = Object.keys(counts) .map(tag => ({ tag, count: counts[tag] })) .sort((a, b) => b.count - a.count) .slice(0, TOP_N) return { code: 0, data: topics } } catch (e) { return { code: -1, message: e.message } } }