优化列表刷新加载
This commit is contained in:
@@ -89,62 +89,87 @@ Page({
|
||||
|
||||
// 产品列表
|
||||
async loadGoodsList(reset = false) {
|
||||
if (!reset && (this._loading || this.data.loadMoreStatus !== 0)) {
|
||||
return;
|
||||
}
|
||||
const seq = (this._listSeq || 0) + 1;
|
||||
this._listSeq = seq;
|
||||
this._loading = true;
|
||||
const page = reset ? 1 : this.data.page;
|
||||
const { pageSize, keywords, tabValue } = this.data;
|
||||
this.setData({
|
||||
loadMoreStatus: 1,
|
||||
loading: true,
|
||||
});
|
||||
try {
|
||||
const { loadMoreStatus, goodsList = [], page, pageSize, keywords, tabValue } = this.data;
|
||||
const params = {
|
||||
page: reset ? 1 : page,
|
||||
const result = await fetchGoodsList({
|
||||
page,
|
||||
categoryId: tabValue,
|
||||
pageSize,
|
||||
keywords,
|
||||
};
|
||||
if (loadMoreStatus !== 0 && !reset) return;
|
||||
this.setData({
|
||||
loadMoreStatus: 1,
|
||||
loading: true,
|
||||
});
|
||||
const result = await fetchGoodsList(params);
|
||||
console.log('fetchGoodsList', result)
|
||||
const data = result.data;
|
||||
if (result.code == 1) {
|
||||
const { spuList, totalCount = 0 } = data;
|
||||
if (totalCount === 0 && reset) {
|
||||
this.setData({
|
||||
emptyInfo: {
|
||||
tip: '抱歉,未找到相关商品',
|
||||
},
|
||||
total: totalCount,
|
||||
hasLoaded: true,
|
||||
loadMoreStatus: 0,
|
||||
loading: false,
|
||||
goodsList: [],
|
||||
});
|
||||
return;
|
||||
}
|
||||
const _goodsList = reset ? spuList : goodsList.concat(spuList);
|
||||
const _loadMoreStatus = _goodsList.length === totalCount ? 2 : 0;
|
||||
this.setData({
|
||||
total: totalCount,
|
||||
page: params.page + 1,
|
||||
goodsList: _goodsList,
|
||||
loadMoreStatus: _loadMoreStatus,
|
||||
});
|
||||
} else {
|
||||
if (seq !== this._listSeq) {
|
||||
return;
|
||||
}
|
||||
if (!result || result.code !== 1 || !result.data) {
|
||||
this.setData({
|
||||
loading: false,
|
||||
hasLoaded: true,
|
||||
loadMoreStatus: 0,
|
||||
});
|
||||
wx.showToast({
|
||||
title: '查询失败,请稍候重试',
|
||||
title: (result && result.msg) || '查询失败,请稍候重试',
|
||||
icon: 'none',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const { spuList = [], totalCount = 0 } = result.data;
|
||||
if (totalCount === 0 && reset) {
|
||||
this.setData({
|
||||
emptyInfo: {
|
||||
tip: '抱歉,未找到相关商品',
|
||||
},
|
||||
total: totalCount,
|
||||
hasLoaded: true,
|
||||
loadMoreStatus: 2,
|
||||
loading: false,
|
||||
goodsList: [],
|
||||
page: 1,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const start = reset ? 0 : this.data.goodsList.length;
|
||||
const loaded = start + spuList.length;
|
||||
const patch = {
|
||||
total: totalCount,
|
||||
page: page + 1,
|
||||
loadMoreStatus: loaded >= totalCount ? 2 : 0,
|
||||
hasLoaded: true,
|
||||
loading: false,
|
||||
};
|
||||
if (reset) {
|
||||
patch.goodsList = spuList;
|
||||
} else {
|
||||
spuList.forEach((item, i) => {
|
||||
patch[`goodsList[${start + i}]`] = item;
|
||||
});
|
||||
}
|
||||
this.setData(patch);
|
||||
} catch (error) {
|
||||
if (seq !== this._listSeq) {
|
||||
return;
|
||||
}
|
||||
console.error('loadGoodsList failed:', error);
|
||||
this.setData({
|
||||
loading: false,
|
||||
hasLoaded: true,
|
||||
loadMoreStatus: 0,
|
||||
});
|
||||
} finally {
|
||||
if (seq === this._listSeq) {
|
||||
this._loading = false;
|
||||
}
|
||||
}
|
||||
this.setData({
|
||||
hasLoaded: true,
|
||||
loading: false,
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
@@ -164,8 +189,11 @@ Page({
|
||||
},
|
||||
|
||||
onReachBottom() {
|
||||
if (this._loading || this.data.loadMoreStatus !== 0) {
|
||||
return;
|
||||
}
|
||||
const { goodsList, total } = this.data;
|
||||
if (goodsList.length === total) {
|
||||
if (goodsList.length >= total) {
|
||||
this.setData({
|
||||
loadMoreStatus: 2,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user