小程序云开发-云函数分页

2022-07-25  本文已影响0人  hao_developer
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  const {
    pageIndex,
    pageSize
  } = event;
  const wxContext = cloud.getWXContext();
  const openid = wxContext.OPENID;
  const countResult = await db.collection('takePhoto').where({
    openid: openid
  }).count(); //获取集合的总计录数
  const total = countResult.total; //得到总记录数
  const totalPage = total / 10; //计算需要多少页
  let hasMore = pageIndex >= totalPage ? false : true; //提示前端是否还有数据
  console.log('云相册',total,totalPage,hasMore);
  return db.collection('takePhoto')
    .where({
      openid: openid
    })
    .orderBy('photoTime','asc')
    .skip((pageIndex - 1) * pageSize)
    .limit(pageSize)
    .get()
    .then(res => {
      res.hasMore = hasMore
      return res;
    });
}
上一篇下一篇

猜你喜欢

热点阅读