Page({
data: {
dataArray: [],
pageSize: 14, // 每页数据
pageNum: 1, // 分页
totalRecords: "", //已加载的数据
total: 0,
loading: false
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.loadData()
},
// 加载数据
loadData() {
let _this = this
wx.showLoading({
title: messAge.messAge.loading
})
listlModel.getGoodsList({
productCategoryId: _this.data.catId,
pageSize: _this.data.pageSize,
pageNum: _this.data.pageNum,
order: 1
})
.then(res => {
console.log(res)
wx.hideLoading()
if (res.code == 200) {
_this.setData({
total: res.data.total
})
_this.setMoreData(res.data.list)
_this.setTotal(res.data.total)
}
})
},
// 加载更多数据
setMoreData(dataArray) {
const tempArray = this.data.dataArray.concat(dataArray)
console.log(tempArray)
this.setData({
dataArray: tempArray
})
},
// 已加载的条数
setTotal(total) {
console.log(total)
console.log(this.data.totalRecords)
this.data.totalRecords = total
console.log(this.data.totalRecords)
if (total != 0) {
this.nextPage()
}
},
// 下一页
nextPage() {
this.data.pageNum += 1;
},
// loading
isLocked() {
return this.data.loading ? true : false
},
// loading
locked() {
this.setData({
loading: true
})
},
// loading
unLocked() {
this.setData({
loading: false
})
},
// 是否还有数据
hasMore() {
console.log(this.data.dataArray.length)
console.log(this.data.totalRecords)
console.log(this.data.dataArray.length >= this.data.totalRecords)
if (this.data.dataArray.length >= this.data.totalRecords) {
return false
} else {
return true
}
},
// 下拉刷加载数据
loadMore() {
let _this = this
console.log(this.data.catId)
if (this.isLocked()) {
return
}
if (this.hasMore()) {
_this.locked()
wx.showLoading({
title: messAge.messAge.loading
})
listlModel.getGoodsList({
pageSize: _this.data.pageSize,
pageNum: _this.data.pageNum,
order: 1
})
.then(res => {
console.log(res)
wx.hideLoading()
if (res.code == 200) {
console.log(res)
_this.setMoreData(res.data.list)
_this.unLocked()
_this.nextPage()
}
}, () => {
_this.unLocked()
})
// 死锁
}
},
/**
* 生命周期函数--监听用户上拉触底事件
*/
onReachBottom: function(e) {
this.loadMore()
}
})
【微信小程序】实现分页 && 重复加载的问题2(锁 && 避免死锁)
最新推荐文章于 2022-08-05 10:27:43 发布