|
|
// pages/message/message.js
|
|
|
Page({
|
|
|
data: {
|
|
|
messages: [], // 消息列表
|
|
|
unreadCount: 0, // 未读消息数量
|
|
|
loading: false, // 是否正在加载
|
|
|
hasMore: false, // 没有更多消息
|
|
|
pageNum: 1, // 当前页码
|
|
|
messageType: 'all' // 消息类型,'all'全部,'unread'未读,'system'系统消息,'match'匹配消息
|
|
|
},
|
|
|
|
|
|
onLoad: function() {
|
|
|
// 使用异步方式初始化,避免阻塞页面加载
|
|
|
setTimeout(() => {
|
|
|
// 检查用户是否已登录
|
|
|
this.checkLoginStatus();
|
|
|
|
|
|
// 加载消息列表
|
|
|
this.loadMessages();
|
|
|
}, 50);
|
|
|
},
|
|
|
|
|
|
onShow: function() {
|
|
|
// 页面显示时,刷新消息列表
|
|
|
this.setData({
|
|
|
pageNum: 1,
|
|
|
hasMore: false
|
|
|
});
|
|
|
this.loadMessages();
|
|
|
},
|
|
|
|
|
|
// 检查登录状态(简化版,不强制要求登录)
|
|
|
checkLoginStatus: function() {
|
|
|
const app = getApp();
|
|
|
// 不强制要求登录,使用模拟数据继续运行
|
|
|
// 如果用户未登录,我们仍然允许浏览消息页面,只是使用默认的模拟数据
|
|
|
if (!app.globalData.hasUserInfo) {
|
|
|
console.log('用户未登录,使用模拟数据显示消息');
|
|
|
// 如果需要,可以设置一个默认的用户信息
|
|
|
if (!app.globalData.userInfo) {
|
|
|
app.globalData.userInfo = { nickName: '访客', avatarUrl: '/images/default_avatar.svg' };
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 确保消息列表已初始化
|
|
|
if (!app.globalData.pendingClaimMessages) {
|
|
|
// 尝试从本地存储加载
|
|
|
try {
|
|
|
const storedMessages = wx.getStorageSync('pendingClaimMessages');
|
|
|
if (storedMessages && Array.isArray(storedMessages)) {
|
|
|
app.globalData.pendingClaimMessages = storedMessages;
|
|
|
} else {
|
|
|
app.globalData.pendingClaimMessages = [];
|
|
|
}
|
|
|
} catch (e) {
|
|
|
console.error('加载消息失败:', e);
|
|
|
app.globalData.pendingClaimMessages = [];
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 切换消息类型
|
|
|
switchMessageType: function(e) {
|
|
|
const type = e.currentTarget.dataset.type;
|
|
|
this.setData({
|
|
|
messageType: type,
|
|
|
pageNum: 1,
|
|
|
hasMore: false
|
|
|
});
|
|
|
this.loadMessages();
|
|
|
},
|
|
|
|
|
|
// 加载消息列表
|
|
|
loadMessages: function() {
|
|
|
if (this.data.loading) return;
|
|
|
|
|
|
this.setData({ loading: true });
|
|
|
|
|
|
// 获取全局应用实例
|
|
|
const app = getApp();
|
|
|
|
|
|
// 确保消息列表已初始化(从本地存储加载)
|
|
|
if (!app.globalData.pendingClaimMessages) {
|
|
|
try {
|
|
|
const storedMessages = wx.getStorageSync('pendingClaimMessages');
|
|
|
if (storedMessages && Array.isArray(storedMessages)) {
|
|
|
app.globalData.pendingClaimMessages = storedMessages;
|
|
|
console.log('从本地存储加载消息:', storedMessages.length, '条');
|
|
|
} else {
|
|
|
app.globalData.pendingClaimMessages = [];
|
|
|
console.log('本地存储中没有消息,初始化空数组');
|
|
|
}
|
|
|
} catch (e) {
|
|
|
console.error('加载消息失败:', e);
|
|
|
app.globalData.pendingClaimMessages = [];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 从全局数据中获取消息列表
|
|
|
let messages = [];
|
|
|
if (app && app.globalData && app.globalData.pendingClaimMessages) {
|
|
|
messages = [...app.globalData.pendingClaimMessages];
|
|
|
console.log('当前消息总数:', messages.length);
|
|
|
|
|
|
// 按时间戳倒序排序(最新的在前)
|
|
|
messages.sort((a, b) => {
|
|
|
const timeA = a.timestamp || 0;
|
|
|
const timeB = b.timestamp || 0;
|
|
|
return timeB - timeA; // 倒序
|
|
|
});
|
|
|
|
|
|
// 格式化时间显示
|
|
|
messages = messages.map(msg => {
|
|
|
// 如果消息已经有格式化时间,直接使用;否则使用时间戳格式化
|
|
|
if (!msg.time && msg.timestamp) {
|
|
|
msg.time = app.formatMessageTime ? app.formatMessageTime(new Date(msg.timestamp)) : '刚刚';
|
|
|
} else if (!msg.time) {
|
|
|
msg.time = '刚刚';
|
|
|
}
|
|
|
return msg;
|
|
|
});
|
|
|
} else {
|
|
|
console.log('全局数据中没有消息列表');
|
|
|
}
|
|
|
|
|
|
// 根据消息类型过滤
|
|
|
let filteredMessages = messages;
|
|
|
if (this.data.messageType === 'unread') {
|
|
|
// 未读消息:status为unread或pending,或isRead为false
|
|
|
filteredMessages = messages.filter(msg =>
|
|
|
msg.status === 'unread' || msg.status === 'pending' || !msg.isRead
|
|
|
);
|
|
|
console.log('未读消息数量:', filteredMessages.length);
|
|
|
} else if (this.data.messageType !== 'all') {
|
|
|
// 按类型过滤
|
|
|
filteredMessages = messages.filter(msg => msg.type === this.data.messageType);
|
|
|
console.log('过滤后消息数量:', filteredMessages.length, '类型:', this.data.messageType);
|
|
|
} else {
|
|
|
console.log('显示全部消息:', filteredMessages.length);
|
|
|
}
|
|
|
|
|
|
// 计算未读消息数量(所有消息中的未读数)
|
|
|
const unreadCount = messages.filter(msg =>
|
|
|
msg.status === 'unread' || msg.status === 'pending' || !msg.isRead
|
|
|
).length;
|
|
|
|
|
|
console.log('未读消息总数:', unreadCount);
|
|
|
console.log('过滤后的消息列表:', filteredMessages);
|
|
|
|
|
|
setTimeout(() => {
|
|
|
this.setData({
|
|
|
messages: filteredMessages,
|
|
|
unreadCount: unreadCount,
|
|
|
loading: false,
|
|
|
hasMore: false // 没有更多消息
|
|
|
});
|
|
|
|
|
|
// 更新tabBar角标
|
|
|
if (app && app.updateTabBarBadge) {
|
|
|
app.updateTabBarBadge();
|
|
|
}
|
|
|
}, 300); // 轻微延迟,保持良好的用户体验
|
|
|
},
|
|
|
|
|
|
// 加载更多消息
|
|
|
onReachBottom: function() {
|
|
|
this.loadMessages();
|
|
|
},
|
|
|
|
|
|
// 下拉刷新
|
|
|
onPullDownRefresh: function() {
|
|
|
this.setData({
|
|
|
pageNum: 1,
|
|
|
hasMore: false
|
|
|
});
|
|
|
this.loadMessages();
|
|
|
|
|
|
// 停止下拉刷新动画
|
|
|
wx.stopPullDownRefresh();
|
|
|
},
|
|
|
|
|
|
// 标记消息为已读
|
|
|
markAsRead: function(e) {
|
|
|
const { id } = e.currentTarget.dataset;
|
|
|
const app = getApp();
|
|
|
|
|
|
if (app && app.globalData && app.globalData.pendingClaimMessages) {
|
|
|
const messageIndex = app.globalData.pendingClaimMessages.findIndex(msg => msg.id === id);
|
|
|
if (messageIndex !== -1) {
|
|
|
app.globalData.pendingClaimMessages[messageIndex].status = 'read';
|
|
|
app.globalData.pendingClaimMessages[messageIndex].isRead = true;
|
|
|
|
|
|
// 持久化存储
|
|
|
try {
|
|
|
wx.setStorageSync('pendingClaimMessages', app.globalData.pendingClaimMessages);
|
|
|
} catch (e) {
|
|
|
console.error('保存消息状态失败:', e);
|
|
|
}
|
|
|
|
|
|
// 更新tabBar角标
|
|
|
if (app.updateTabBarBadge) {
|
|
|
app.updateTabBarBadge();
|
|
|
}
|
|
|
|
|
|
// 刷新消息列表
|
|
|
this.loadMessages();
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 点击消息
|
|
|
onMessageClick: function(e) {
|
|
|
const { id, itemId, itemType } = e.currentTarget.dataset;
|
|
|
|
|
|
// 标记消息为已读
|
|
|
this.markAsRead(e);
|
|
|
|
|
|
// 如果是匹配消息,跳转到匹配页面
|
|
|
const app = getApp();
|
|
|
const message = app.globalData.pendingClaimMessages.find(msg => msg.id === id);
|
|
|
if (message && message.type === 'match') {
|
|
|
// 跳转到匹配页面
|
|
|
wx.switchTab({
|
|
|
url: '/pages/match/match'
|
|
|
});
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 如果消息关联了物品,跳转到物品详情页,并传递来源标志
|
|
|
if (itemId && itemType) {
|
|
|
wx.navigateTo({
|
|
|
url: `/pages/detail/detail?id=${itemId}&type=${itemType}&fromNotification=true`
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 点击匹配项卡片
|
|
|
onMatchItemClick: function(e) {
|
|
|
const { matchId, matchType } = e.currentTarget.dataset;
|
|
|
|
|
|
// 注意:已使用 catchtap 阻止事件冒泡,不需要手动调用 stopPropagation
|
|
|
// 微信小程序的事件对象不支持 stopPropagation 方法
|
|
|
|
|
|
// 跳转到匹配项详情页
|
|
|
if (matchId && matchType) {
|
|
|
wx.navigateTo({
|
|
|
url: `/pages/detail/detail?id=${matchId}&type=${matchType}&fromMatch=true`
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 匹配项图片加载错误处理
|
|
|
onMatchImageError: function(e) {
|
|
|
const { matchId } = e.currentTarget.dataset;
|
|
|
console.log('匹配项图片加载失败:', matchId);
|
|
|
// 图片加载失败时,使用默认图片显示
|
|
|
// WXML中已经设置了默认图片,这里只需要记录日志
|
|
|
},
|
|
|
|
|
|
// 一键已读
|
|
|
markAllAsRead: function() {
|
|
|
const app = getApp();
|
|
|
|
|
|
if (app && app.globalData && app.globalData.pendingClaimMessages) {
|
|
|
app.globalData.pendingClaimMessages.forEach(msg => {
|
|
|
msg.status = 'read';
|
|
|
msg.isRead = true;
|
|
|
});
|
|
|
|
|
|
// 持久化存储
|
|
|
try {
|
|
|
wx.setStorageSync('pendingClaimMessages', app.globalData.pendingClaimMessages);
|
|
|
} catch (e) {
|
|
|
console.error('保存消息状态失败:', e);
|
|
|
}
|
|
|
|
|
|
// 更新tabBar角标
|
|
|
if (app.updateTabBarBadge) {
|
|
|
app.updateTabBarBadge();
|
|
|
}
|
|
|
|
|
|
// 刷新消息列表
|
|
|
this.loadMessages();
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '已全部标记为已读',
|
|
|
icon: 'success',
|
|
|
duration: 1500
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 删除消息
|
|
|
deleteMessage: function(e) {
|
|
|
const { id } = e.currentTarget.dataset;
|
|
|
const app = getApp();
|
|
|
|
|
|
wx.showModal({
|
|
|
title: '确认删除',
|
|
|
content: '确定要删除这条消息吗?删除后将无法恢复。',
|
|
|
success: (res) => {
|
|
|
if (res.confirm) {
|
|
|
if (app && app.globalData && app.globalData.pendingClaimMessages) {
|
|
|
const messageIndex = app.globalData.pendingClaimMessages.findIndex(msg => msg.id === id);
|
|
|
if (messageIndex !== -1) {
|
|
|
app.globalData.pendingClaimMessages.splice(messageIndex, 1);
|
|
|
|
|
|
// 持久化存储
|
|
|
try {
|
|
|
wx.setStorageSync('pendingClaimMessages', app.globalData.pendingClaimMessages);
|
|
|
} catch (e) {
|
|
|
console.error('保存消息状态失败:', e);
|
|
|
}
|
|
|
|
|
|
// 更新tabBar角标
|
|
|
if (app.updateTabBarBadge) {
|
|
|
app.updateTabBarBadge();
|
|
|
}
|
|
|
|
|
|
// 刷新消息列表
|
|
|
this.loadMessages();
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '消息已删除',
|
|
|
icon: 'success',
|
|
|
duration: 1500
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 阻止事件冒泡
|
|
|
e.stopPropagation();
|
|
|
},
|
|
|
|
|
|
// 清空所有消息
|
|
|
clearAllMessages: function() {
|
|
|
wx.showModal({
|
|
|
title: '确认清空',
|
|
|
content: '确定要清空所有消息吗?此操作无法恢复。',
|
|
|
success: (res) => {
|
|
|
if (res.confirm) {
|
|
|
const app = getApp();
|
|
|
if (app && app.globalData) {
|
|
|
app.globalData.pendingClaimMessages = [];
|
|
|
|
|
|
// 持久化存储
|
|
|
try {
|
|
|
wx.setStorageSync('pendingClaimMessages', []);
|
|
|
} catch (e) {
|
|
|
console.error('清空消息失败:', e);
|
|
|
}
|
|
|
|
|
|
// 更新tabBar角标
|
|
|
if (app.updateTabBarBadge) {
|
|
|
app.updateTabBarBadge();
|
|
|
}
|
|
|
|
|
|
// 刷新消息列表
|
|
|
this.loadMessages();
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '已清空所有消息',
|
|
|
icon: 'success',
|
|
|
duration: 1500
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 测试创建消息(用于调试)
|
|
|
createTestMessage: function() {
|
|
|
const app = getApp();
|
|
|
if (app && app.createSystemMessage) {
|
|
|
app.createSystemMessage(
|
|
|
'system',
|
|
|
'测试消息',
|
|
|
'这是一条测试消息,用于验证消息通知功能是否正常工作。',
|
|
|
null,
|
|
|
null
|
|
|
);
|
|
|
|
|
|
// 刷新消息列表
|
|
|
this.loadMessages();
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '测试消息已创建',
|
|
|
icon: 'success',
|
|
|
duration: 1500
|
|
|
});
|
|
|
} else {
|
|
|
wx.showToast({
|
|
|
title: '创建消息失败',
|
|
|
icon: 'none',
|
|
|
duration: 1500
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}); |