Initial commit
This commit is contained in:
34
cloudfunctions/getPost/index.js
Normal file
34
cloudfunctions/getPost/index.js
Normal file
@@ -0,0 +1,34 @@
|
||||
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 }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user