Files
code-bang/frontend/miniapp/pages/message/list/index.js
T
2026-09-17 16:25:31 +08:00

98 lines
2.1 KiB
JavaScript

// pages/message/index.js
import { fetchMessageList, messageSetRead } from '../../../services/message/fetchMessage';
Page({
data: {
list: [],
page: 1,
pageSize: 10,
unreadCount: 0,
hasMore: true,
loading: false,
showTextAndTitle: false,
currentMessage: {},
confirmBtn: '关闭'
},
onLoad() {
this.loadList(true);
},
loadList(refresh = false) {
let { pageSize, list } = this.data;
const page = refresh ? 1 : this.data.page;
let params = {
page: page,
message_type: 0,
pageSize: pageSize,
};
fetchMessageList(params).then((res) => {
console.log(res)
let newList = res.data.data;
let hasMore = res.data.current_page < res.data.last_page;
this.setData({
list: refresh ? newList : list.concat(newList),
page: page + 1,
unreadCount: res.data.unread_count,
hasMore: hasMore,
loading: false
});
});
},
showDialog(e) {
const id = e.currentTarget.dataset.id;
const item = this.data.list.find(msg => msg.id == id);
this.setData({
showTextAndTitle: true,
currentMessage: item || {}
});
messageSetRead({ id: id }).then((res) => {
console.log(res)
// 把查找到的列表中的元素的is_read字段设为1
if (item) {
const idx = this.data.list.findIndex(msg => msg.id == id);
if (idx !== -1) {
const key = `list[${idx}].is_read`;
this.setData({ [key]: 1 });
}
}
});
},
closeDialog() {
this.setData({ showTextAndTitle: false });
},
onPullDownRefresh() {
this.loadList(true);
},
onReachBottom() {
if (this.data.hasMore) {
this.loadList();
}
},
onClearUnreadTap() {
messageSetRead({ isAll: 1 }).then((res) => {
console.log(res)
wx.showToast({ title: '全部已读', icon: 'success' });
this.loadList(true);
});
},
onMessageTap(e) {
const { type, id } = e.currentTarget.dataset;
wx.navigateTo({
url: `/pages/message/detail/index?type=${type}&id=${id}`
});
},
onShow() {
this.loadList(true);
},
onShareAppMessage() { }
});