优化列表刷新加载
This commit is contained in:
@@ -24,17 +24,18 @@ Page({
|
||||
loading: false
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadList(true);
|
||||
},
|
||||
|
||||
loadList(refresh = false) {
|
||||
let { pageSize, list, currentType } = this.data;
|
||||
if (!refresh && (this._loading || !this.data.hasMore)) {
|
||||
return;
|
||||
}
|
||||
const seq = (this._listSeq || 0) + 1;
|
||||
this._listSeq = seq;
|
||||
this._loading = true;
|
||||
const { pageSize, currentType } = this.data;
|
||||
const page = refresh ? 1 : this.data.page;
|
||||
|
||||
let params = {
|
||||
page: page,
|
||||
pageSize: pageSize,
|
||||
const params = {
|
||||
page,
|
||||
pageSize,
|
||||
};
|
||||
//全部时不传 message_type,由后端返回全类型
|
||||
if (currentType !== '') {
|
||||
@@ -42,25 +43,52 @@ Page({
|
||||
}
|
||||
|
||||
fetchMessageList(params).then((res) => {
|
||||
let newList = res.data.data;
|
||||
let hasMore = res.data.current_page < res.data.last_page;
|
||||
let unreadMap = res.data.unread_map || { '0': 0, '1': 0, '2': 0 };
|
||||
if (seq !== this._listSeq) {
|
||||
return;
|
||||
}
|
||||
if (!res || res.code !== 1 || !res.data) {
|
||||
wx.showToast({ title: (res && res.msg) || '加载失败', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
const nextList = Array.isArray(res.data.data) ? res.data.data : [];
|
||||
const hasMore = res.data.current_page < res.data.last_page;
|
||||
const unreadMap = res.data.unread_map || { '0': 0, '1': 0, '2': 0 };
|
||||
//直接用后端统计的全量未读总数(含所有类型),
|
||||
//不再由前端累加 0/1/2,避免 message_type 超出这三个值时漏算
|
||||
let totalUnread = res.data.unread_total !== undefined && res.data.unread_total !== null
|
||||
const totalUnread = res.data.unread_total !== undefined && res.data.unread_total !== null
|
||||
? Number(res.data.unread_total) || 0
|
||||
: Number(unreadMap['0'] || 0) + Number(unreadMap['1'] || 0) + Number(unreadMap['2'] || 0);
|
||||
this.setData({
|
||||
list: refresh ? newList : list.concat(newList),
|
||||
page: page + 1,
|
||||
const patch = {
|
||||
page: hasMore ? page + 1 : page,
|
||||
unreadCount: res.data.unread_count,
|
||||
unreadMap: unreadMap,
|
||||
totalUnread: totalUnread,
|
||||
hasMore: hasMore,
|
||||
loading: false
|
||||
});
|
||||
//同步底部tabbar红点
|
||||
unreadMap,
|
||||
totalUnread,
|
||||
hasMore,
|
||||
loading: false,
|
||||
};
|
||||
if (refresh) {
|
||||
patch.list = nextList;
|
||||
} else {
|
||||
const start = this.data.list.length;
|
||||
nextList.forEach((item, i) => {
|
||||
patch[`list[${start + i}]`] = item;
|
||||
});
|
||||
}
|
||||
this.setData(patch);
|
||||
this.syncTabBarUnread(totalUnread);
|
||||
}).catch((err) => {
|
||||
if (seq !== this._listSeq) {
|
||||
return;
|
||||
}
|
||||
console.error('loadList failed:', err);
|
||||
wx.showToast({ title: '网络异常,请重试', icon: 'none' });
|
||||
}).then(() => {
|
||||
if (seq !== this._listSeq) {
|
||||
return;
|
||||
}
|
||||
this._loading = false;
|
||||
this.setData({ loading: false });
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
|
||||
@@ -99,15 +127,29 @@ Page({
|
||||
},
|
||||
|
||||
onReachBottom() {
|
||||
if (this.data.hasMore) {
|
||||
this.loadList();
|
||||
if (this._loading || !this.data.hasMore) {
|
||||
return;
|
||||
}
|
||||
this.loadList();
|
||||
},
|
||||
|
||||
onClearUnreadTap() {
|
||||
messageSetRead({ isAll: 1 }).then(() => {
|
||||
if (this._clearing) {
|
||||
return;
|
||||
}
|
||||
this._clearing = true;
|
||||
messageSetRead({ isAll: 1 }).then((res) => {
|
||||
if (!res || res.code !== 1) {
|
||||
wx.showToast({ title: (res && res.msg) || '操作失败', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
wx.showToast({ title: '全部已读', icon: 'success' });
|
||||
this.loadList(true);
|
||||
}).catch((err) => {
|
||||
console.error('messageSetRead failed:', err);
|
||||
wx.showToast({ title: '网络异常,请重试', icon: 'none' });
|
||||
}).then(() => {
|
||||
this._clearing = false;
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user