Files
code-bang/frontend/miniapp/pages/message/list/index.js
T
2026-10-19 16:15:03 +08:00

167 lines
4.7 KiB
JavaScript

// pages/message/index.js
import { fetchMessageList, messageSetRead } from '../../../services/message/fetchMessage';
// 消息类型 tab:与后端 message_type 对齐(''=全部)
const TYPE_TABS = [
{ key: '', label: '全部' },
{ key: '0', label: '系统消息' },
{ key: '1', label: '公告消息' },
{ key: '2', label: '需求消息' },
];
Page({
data: {
tabs: TYPE_TABS,
currentType: '', //当前筛选的消息类型:''=全部
currentTabIndex: 0,
unreadMap: { '0': 0, '1': 0, '2': 0 }, //分类未读数
totalUnread: 0, //总未读数
list: [],
page: 1,
pageSize: 10,
unreadCount: 0,
hasMore: true,
loading: false
},
loadList(refresh = false) {
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;
const params = {
page,
pageSize,
};
//全部时不传 message_type,由后端返回全类型
if (currentType !== '') {
params.message_type = currentType;
}
fetchMessageList(params).then((res) => {
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 超出这三个值时漏算
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);
const patch = {
page: hasMore ? page + 1 : page,
unreadCount: res.data.unread_count,
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();
});
},
/**
* 同步底部tabbar消息红点
* @param {number} count 总未读数
*/
syncTabBarUnread(count) {
const tabBar = typeof this.getTabBar === 'function' && this.getTabBar();
if (tabBar && typeof tabBar.setUnread === 'function') {
tabBar.setUnread(count);
}
},
//切换消息类型tab
onTabsChange(e) {
const index = Number(e.detail.value);
const tab = this.data.tabs[index] || this.data.tabs[0];
this.setData({
currentTabIndex: index,
currentType: tab.key
});
this.loadList(true);
},
// 点击消息进入详情页(关闭交给小程序的返回:左上角/右滑手势/物理返回键)
onMessageTap(e) {
const id = e.currentTarget.dataset.id;
wx.navigateTo({
url: `/pages/message/detail/index?id=${id}`
});
},
onPullDownRefresh() {
this.loadList(true);
},
onReachBottom() {
if (this._loading || !this.data.hasMore) {
return;
}
this.loadList();
},
onClearUnreadTap() {
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;
});
},
onShow() {
// 自定义 tabBar 每个 Tab 页各有一份实例,切过来后要按当前路由重设选中项
const tabBar = typeof this.getTabBar === 'function' && this.getTabBar();
if (tabBar) {
tabBar.init();
}
this.loadList(true);
},
onShareAppMessage() { }
});