const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() exports.main = async (event, context) => { const { OPENID } = cloud.getWXContext() const { userId } = event try { let userRes if (userId) { // Look up by DB _id userRes = await db.collection('users').doc(userId).get().catch(() => null) if (!userRes?.data) { // Fallback: look up by openid const res = await db.collection('users').where({ openid: userId }).get() userRes = res.data.length > 0 ? { data: res.data[0] } : null } } else { const res = await db.collection('users').where({ openid: OPENID }).get() userRes = res.data.length > 0 ? { data: res.data[0] } : null } if (!userRes?.data) return { code: -1, message: '用户不存在' } const user = userRes.data // Check follow status const followRes = await db.collection('follows') .where({ followerId: OPENID, followeeId: user.openid }) .count() return { code: 0, data: { ...user, isFollowing: followRes.total > 0 }, } } catch (e) { return { code: -1, message: e.message } } }