|
|
|
@ -3,17 +3,27 @@ const cloud = require('wx-server-sdk')
|
|
|
|
|
|
|
|
|
|
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
|
|
|
|
|
|
|
|
|
|
//获取老师集合
|
|
|
|
|
const teacher = cloud.database().collection("teacher_Data")
|
|
|
|
|
const db = cloud.database()
|
|
|
|
|
const MAX_LIMIT = 100
|
|
|
|
|
|
|
|
|
|
// 云函数入口函数
|
|
|
|
|
exports.main = async (event, context) => {
|
|
|
|
|
const wxContext = cloud.getWXContext()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
event,
|
|
|
|
|
openid: wxContext.OPENID,
|
|
|
|
|
appid: wxContext.APPID,
|
|
|
|
|
unionid: wxContext.UNIONID,
|
|
|
|
|
// 先取出集合记录总数
|
|
|
|
|
const countResult = await db.collection('teacher_Data').count()
|
|
|
|
|
const total = countResult.total
|
|
|
|
|
// 计算需分几次取
|
|
|
|
|
const batchTimes = Math.ceil(total / 100)
|
|
|
|
|
// 承载所有读操作的 promise 的数组
|
|
|
|
|
const tasks = []
|
|
|
|
|
for (let i = 0; i < batchTimes; i++) {
|
|
|
|
|
const promise = db.collection('teacher_Data').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
|
|
|
|
|
tasks.push(promise)
|
|
|
|
|
}
|
|
|
|
|
// 等待所有
|
|
|
|
|
return (await Promise.all(tasks)).reduce((acc, cur) => {
|
|
|
|
|
return {
|
|
|
|
|
data: acc.data.concat(cur.data),
|
|
|
|
|
errMsg: acc.errMsg,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|