消息模块完善
This commit is contained in:
@@ -1,7 +1,21 @@
|
||||
// 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,
|
||||
@@ -18,51 +32,99 @@ Page({
|
||||
},
|
||||
|
||||
loadList(refresh = false) {
|
||||
let { pageSize, list } = this.data;
|
||||
let { pageSize, list, currentType } = this.data;
|
||||
const page = refresh ? 1 : this.data.page;
|
||||
|
||||
let params = {
|
||||
page: page,
|
||||
message_type: 0,
|
||||
pageSize: pageSize,
|
||||
};
|
||||
//全部时不传 message_type,由后端返回全类型
|
||||
if (currentType !== '') {
|
||||
params.message_type = currentType;
|
||||
}
|
||||
|
||||
fetchMessageList(params).then((res) => {
|
||||
console.log(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 };
|
||||
let totalUnread = Number(unreadMap['0'] || 0) + Number(unreadMap['1'] || 0) + Number(unreadMap['2'] || 0);
|
||||
this.setData({
|
||||
list: refresh ? newList : list.concat(newList),
|
||||
page: page + 1,
|
||||
unreadCount: res.data.unread_count,
|
||||
unreadMap: unreadMap,
|
||||
totalUnread: totalUnread,
|
||||
hasMore: hasMore,
|
||||
loading: false
|
||||
});
|
||||
//同步底部tabbar红点
|
||||
this.syncTabBarUnread(totalUnread);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 同步底部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);
|
||||
},
|
||||
|
||||
showDialog(e) {
|
||||
const id = e.currentTarget.dataset.id;
|
||||
const item = this.data.list.find(msg => msg.id == id);
|
||||
const jumpPage = item?.extra_json?.page || '';
|
||||
this.setData({
|
||||
showTextAndTitle: true,
|
||||
currentMessage: item || {}
|
||||
currentMessage: item || {},
|
||||
//携带跳转页面时提供「前往查看」
|
||||
confirmBtn: jumpPage ? '前往查看' : '关闭'
|
||||
});
|
||||
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 });
|
||||
}
|
||||
messageSetRead({ id: id }).then(() => {
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
const idx = this.data.list.findIndex(msg => msg.id == id);
|
||||
if (idx === -1) {
|
||||
return;
|
||||
}
|
||||
//原来未读的消息:本地未读数-1并同步tabbar红点
|
||||
if (!this.data.list[idx].is_read) {
|
||||
const typeKey = String(item.message_type);
|
||||
const unreadMap = { ...this.data.unreadMap };
|
||||
unreadMap[typeKey] = Math.max(Number(unreadMap[typeKey] || 0) - 1, 0);
|
||||
const totalUnread = Math.max(this.data.totalUnread - 1, 0);
|
||||
this.setData({ unreadMap, totalUnread });
|
||||
this.syncTabBarUnread(totalUnread);
|
||||
}
|
||||
this.setData({ [`list[${idx}].is_read`]: 1 });
|
||||
});
|
||||
},
|
||||
|
||||
closeDialog() {
|
||||
//弹窗确认:有跳转页面则前往,否则仅关闭
|
||||
onDialogConfirm() {
|
||||
const jumpPage = this.data.currentMessage?.extra_json?.page || '';
|
||||
this.setData({ showTextAndTitle: false });
|
||||
if (jumpPage) {
|
||||
wx.navigateTo({
|
||||
url: '/' + String(jumpPage).replace(/^\//, '')
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
@@ -76,8 +138,7 @@ Page({
|
||||
},
|
||||
|
||||
onClearUnreadTap() {
|
||||
messageSetRead({ isAll: 1 }).then((res) => {
|
||||
console.log(res)
|
||||
messageSetRead({ isAll: 1 }).then(() => {
|
||||
wx.showToast({ title: '全部已读', icon: 'success' });
|
||||
this.loadList(true);
|
||||
});
|
||||
@@ -95,4 +156,4 @@ Page({
|
||||
},
|
||||
|
||||
onShareAppMessage() { }
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user