优化列表刷新加载

This commit is contained in:
Dev
2026-10-19 13:01:58 +08:00
parent 0c6f4177f1
commit 44405743bb
10 changed files with 438 additions and 274 deletions
+129 -78
View File
@@ -70,15 +70,10 @@ Page({
//图片预览
onImageView(e) {
const { images, index } = e.currentTarget.dataset;
console.log('images', images);
console.log('index', index);
wx.previewImage({
current: images[index], // 当前显示图片的http链接
urls: images, // 需要预览的图片http链接列表
success: (res) => {
console.log('预览成功');
},
complete: (res) => {
current: images[index],
urls: images,
complete: () => {
this.setData({ fromPreview: true });
}
});
@@ -87,65 +82,69 @@ Page({
// 加载需求列表数据
async loadDemandList(fresh = false, options = {}) {
const { silent = false } = options;
if (fresh) {
this.setData({
page: 1,
hasMore: true, // 刷新时重置
});
wx.pageScrollTo({
scrollTop: 0,
});
// 上拉加载:请求未结束或没有下一页时直接返回,避免并发把同一页打两次
if (!fresh && (this._loading || !this.data.hasMore)) {
return;
}
const seq = (this._listSeq || 0) + 1;
this._listSeq = seq;
this._loading = true;
const page = fresh ? 1 : this.data.page;
const { status, keywords, opType } = this.data;
if (fresh) {
wx.pageScrollTo({ scrollTop: 0 });
}
if (!silent) {
wx.showLoading({ title: '加载中', mask: true });
}
wx.nextTick(() => {
console.log(this.data.hasMore, fresh);
if (!this.data.hasMore && !fresh) return;
});
try {
const { page, status, keywords, opType } = this.data;
let params = { page, status, keywords, opType };
// 下拉刷新自带 loading 指示,不再叠加 showLoading
if (!silent) {
wx.showLoading({
title: '加载中',
});
const res = await fetchDemandList({ page, status, keywords, opType });
if (seq !== this._listSeq) {
return;
}
const res = await fetchDemandList(params);
const nextList = res.data.data;
console.log(res)
console.log(nextList)
if (!silent) {
wx.hideLoading();
if (!res || res.code !== 1 || !res.data) {
wx.showToast({ title: (res && res.msg) || '加载失败', icon: 'none' });
return;
}
// 判断是否还有更多数据
const { current_page, last_page } = res.data;
let newDemandList = fresh ? nextList : this.data.demandList.concat(nextList);
const hasMore = current_page < last_page;
// 只有在还有更多页时才自增page
const nextList = Array.isArray(res.data.data) ? res.data.data : [];
const hasMore = res.data.current_page < res.data.last_page;
const start = fresh ? 0 : this.data.demandList.length;
const patch = {
hasMore,
isEmpty: start + nextList.length === 0,
};
if (hasMore) {
this.setData({
demandList: newDemandList,
page: page + 1,
hasMore
});
patch.page = page + 1;
} else if (fresh) {
patch.page = 1;
}
if (fresh) {
patch.demandList = nextList;
} else {
this.setData({
demandList: newDemandList,
hasMore: false
nextList.forEach((item, i) => {
patch[`demandList[${start + i}]`] = item;
});
}
this.setData({
isEmpty: newDemandList.length > 0 ? false : true
});
// 检查内容是否超出最大高度,放到 nextTick 里
wx.nextTick(() => {
this.checkDescOverflow();
this.setData(patch, () => {
if (seq !== this._listSeq) {
return;
}
const indices = [];
for (let i = start; i < this.data.demandList.length; i++) {
indices.push(i);
}
this.checkDescOverflow(indices);
});
} catch (err) {
console.error('Failed to load demand list:', err);
if (seq === this._listSeq) {
console.error('Failed to load demand list:', err);
wx.showToast({ title: '网络异常,请重试', icon: 'none' });
}
} finally {
if (seq === this._listSeq) {
this._loading = false;
wx.hideLoading();
}
}
},
@@ -161,6 +160,13 @@ Page({
await this.loadDemandList(true);
return;
}
const prev = {};
demandList.forEach((item) => {
prev[item.id] = { expanded: !!item.expanded, showToggle: !!item.showToggle };
});
const seq = (this._listSeq || 0) + 1;
this._listSeq = seq;
this._loading = true;
try {
const res = await fetchDemandList({
page: 1,
@@ -169,39 +175,83 @@ Page({
keywords,
opType
});
const nextList = res.data.data || [];
//不动 page,保持原有的分页进度;条目流转出当前标签后总数会变,需同步 hasMore
if (seq !== this._listSeq) {
return;
}
if (!res || res.code !== 1 || !res.data) {
return;
}
const raw = Array.isArray(res.data.data) ? res.data.data : [];
// 保留已展开状态,避免返回列表时把「查看全文」收回去
const nextList = raw.map((item) => {
const old = prev[item.id];
return {
...item,
expanded: old ? old.expanded : false,
showToggle: old ? old.showToggle : false,
};
});
this.setData({
demandList: nextList,
hasMore: res.data.current_page < res.data.last_page,
isEmpty: nextList.length === 0
});
wx.nextTick(() => {
this.checkDescOverflow();
}, () => {
if (seq !== this._listSeq) {
return;
}
const indices = [];
nextList.forEach((item, index) => {
if (!prev[item.id]) {
indices.push(index);
}
});
this.checkDescOverflow(indices);
});
} catch (err) {
console.error('Failed to refresh demand list:', err);
if (seq === this._listSeq) {
console.error('Failed to refresh demand list:', err);
}
} finally {
if (seq === this._listSeq) {
this._loading = false;
}
}
},
// 检查每条内容是否超出最大高度,决定是否显示“查看全文”
checkDescOverflow() {
/**
* 只测量指定下标的描述高度,按路径更新 showToggle
* 不改 expanded,加载下一页时已展开的条目保持展开
* @param {number[]} indices
*/
checkDescOverflow(indices) {
if (!indices || !indices.length) {
return;
}
const seq = this._listSeq;
const query = this.createSelectorQuery();
const maxHeight = 60; // 120rpx ≈ 60px,实际可根据机型调整
this.data.demandList.forEach((item, index) => {
const maxHeight = 60; // 120rpx ≈ 60px
indices.forEach((index) => {
query.select(`#desc-${index}`).boundingClientRect();
});
query.exec((rects) => {
const newList = this.data.demandList.map((item, idx) => {
// 只有内容高度大于 maxHeight+1 时才显示“查看全文”
const showToggle = rects[idx] && rects[idx].height > (maxHeight + 1);
return {
...item,
expanded: false,
showToggle
};
if (seq !== this._listSeq || !rects) {
return;
}
const patch = {};
rects.forEach((rect, i) => {
const index = indices[i];
const item = this.data.demandList[index];
if (!item) {
return;
}
const showToggle = !!(rect && rect.height > maxHeight + 1);
if (!!item.showToggle !== showToggle) {
patch[`demandList[${index}].showToggle`] = showToggle;
}
});
this.setData({ demandList: newList });
if (Object.keys(patch).length) {
this.setData(patch);
}
});
},
@@ -312,9 +362,10 @@ Page({
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
if (this.data.hasMore) {
this.loadDemandList();
if (this._loading || !this.data.hasMore) {
return;
}
this.loadDemandList();
},
/**
@@ -44,7 +44,7 @@
{{item.expanded ? '收起' : '查看全文'}}
</view>
<view class="content-img-list">
<image wx:for="{{item.images}}" wx:for-item="img" wx:for-index="imgIndex" wx:key="index" class="content-img" mode="aspectFill" data-index="imgIndex" data-images="{{item.images}}" src="{{img}}" bind:tap="onImageView"></image>
<image wx:for="{{item.images}}" wx:for-item="img" wx:for-index="imgIndex" wx:key="*this" class="content-img" mode="aspectFill" data-index="{{imgIndex}}" data-images="{{item.images}}" src="{{img}}" bind:tap="onImageView"></image>
</view>
</view>
<view class="bottom-row">