美文网首页
小程序云开发-云函数分页

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

作者: hao_developer | 来源:发表于2022-07-25 13:32 被阅读0次
    // 云函数入口文件
    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;
        });
    }
    

    相关文章

      网友评论

          本文标题:小程序云开发-云函数分页

          本文链接:https://www.haomeiwen.com/subject/qapgirtx.html