第一次调用不能获得数据,需要等待500ms再次调用才能获得数据(等待300ms都不可以)
代码如下:
onLoad: function (options) {
this.today()
setTimeout(()=>{
this.today()
}, 500)
},
today(){
this.setData({
taptype:'今日'
})
this.loadDataFromServer('today')
},
loadDataFromServer(type){
const now = new Date()
const db = wx.cloud.database({
env: 'yzktest-4g2sek3udaa6d365'
})
let clacDate;
if(type === 'today'){
clacDate = {
Day:now.getDate(),
Month:now.getMonth()+1,
Year:now.getFullYear()
}
}else if(type === 'tomonth'){
clacDate = {
Month:now.getMonth()+1,
Year:now.getFullYear()
}
}
db.collection('Tourism_UserData').aggregate().match({
payDate:clacDate,
ifPay:1,
ifrefund:1,
area:this.data.pickedArea,
acenic:this.data.pickedAcenic,
}).count('datanum').end().then(res1 =>{
if(res1.list.length === 0){
this.setData({
OrderNumber:0,
WholeIncome:0,
WholePromoteFee:Promotefees,
})
}else{
const PAGE_COUNT = 20;
const totalCount = res1.list[0].datanum;
const PageNum = Math.ceil(totalCount/PAGE_COUNT)
let Pageindex = 0;
let WholeIncome=0;
for(Pageindex=0;Pageindex<PageNum;Pageindex++){
db.collection('Tourism_UserData').aggregate().match({
payDate:clacDate,
ifPay:1,
ifrefund:1,
area:this.data.pickedArea,
acenic:this.data.pickedAcenic,
}).project({
total_fee:1,
refund_fee:1,
promotorNum:1,
promote_fee:1,
}).skip(Pageindex*PAGE_COUNT).limit(PAGE_COUNT).end().then(res2 =>{
const orderNum = res2.list.length;
let i=0;
for(i=0;i<orderNum;i++){
WholeIncome = WholeIncome+res2.list[i].total_fee-res2.list[i].refund_fee;
}
this.setData({
WholeIncome:WholeIncome,
})
})
}
this.setData({
OrderNumber:totalCount
})
}
})
},