云函数:
app.router('detail', async(ctx, next) => {
let blogId = event.blogId
let detail = await blogCollection.where({
_id: blogId
}).get().then((res) => {
return res.data
})
const countResult = await blogCollection.count()
const total = countResult.total
let commentList = {
data: []
}
if (total > 0) {
const batchTimes = Math.ceil(total / MAX_LIMIT)
const tasks = []
for (let i = 0; i < batchTimes; i++) {
let promise = db.collection('blog-comment').skip(i * MAX_LIMIT)
.limit(MAX_LIMIT).where({
blogId
}).orderBy('createTime', 'desc').get()
tasks.push(promise)
}
if (tasks.length > 0) {
commentList = (await Promise.all(tasks)).reduce((acc, cur) => {
return {
data: acc.data.concat(cur.data)
}
})
}
}
ctx.body = { /
commentList,
detail,
}
})
调用端函数:
onLoad: function (options) {
console.log(options)
this._getBlogDetail(options.blogId)
},
_getBlogDetail(blogId){
wx.showLoading({
title: '加载中',
true:true,
})
wx.cloud.callFunction({
name :'blog',
data:{
blogId,
$url:'detail',
}
}).then((res) =>{
wx.hideLoading()
console.log(res)
})
},