35 lines
946 B
JavaScript
35 lines
946 B
JavaScript
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 { postId } = event
|
|
if (!postId) return { code: -1, message: '缺少 postId' }
|
|
|
|
try {
|
|
const post = await db.collection('posts').doc(postId).get()
|
|
if (!post.data) return { code: -1, message: '帖子不存在' }
|
|
|
|
const authorRes = await db.collection('users')
|
|
.where({ openid: post.data.authorId })
|
|
.field({ nickName: true, avatarUrl: true, openid: true, pets: true })
|
|
.get()
|
|
|
|
const likeRes = await db.collection('likes')
|
|
.where({ userId: OPENID, postId })
|
|
.count()
|
|
|
|
return {
|
|
code: 0,
|
|
data: {
|
|
...post.data,
|
|
author: authorRes.data[0] || null,
|
|
isLiked: likeRes.total > 0,
|
|
},
|
|
}
|
|
} catch (e) {
|
|
return { code: -1, message: e.message }
|
|
}
|
|
}
|