const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const _ = db.command exports.main = async (event, context) => { const { OPENID } = cloud.getWXContext() const { type = 'following', userId } = event const targetId = userId || OPENID try { let followIds = [] if (type === 'following') { const res = await db.collection('follows') .where({ followerId: targetId }) .field({ followeeId: true }) .get() followIds = res.data.map(f => f.followeeId) } else { const res = await db.collection('follows') .where({ followeeId: targetId }) .field({ followerId: true }) .get() followIds = res.data.map(f => f.followerId) } if (followIds.length === 0) { return { code: 0, data: [] } } const usersRes = await db.collection('users') .where({ openid: _.in(followIds) }) .field({ nickName: true, openid: true, avatarUrl: true, bio: true, location: true, isOnline: true, lastSeen: true, pets: true, stats: true }) .get() // check if current user follows each const myFollowsRes = await db.collection('follows') .where({ followerId: OPENID, followeeId: _.in(followIds) }) .field({ followeeId: true }) .get() const myFollowSet = new Set(myFollowsRes.data.map(f => f.followeeId)) return { code: 0, data: usersRes.data.map(u => ({ ...u, _id: u._id, isFollowing: myFollowSet.has(u.openid) })), } } catch (e) { return { code: -1, message: e.message } } }