You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
327 lines
9.7 KiB
327 lines
9.7 KiB
|
3 months ago
|
// 消息列表页面
|
||
|
|
Page({
|
||
|
|
data: {
|
||
|
|
messageList: []
|
||
|
|
},
|
||
|
|
|
||
|
|
onLoad: function() {
|
||
|
|
this.loadChatList();
|
||
|
|
// 注册全局新消息处理函数
|
||
|
|
this.registerGlobalMessageHandler();
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 注册全局新消息处理函数
|
||
|
|
*/
|
||
|
|
registerGlobalMessageHandler: function() {
|
||
|
|
try {
|
||
|
|
const app = getApp();
|
||
|
|
const that = this;
|
||
|
|
|
||
|
|
// 保存原有的处理函数(如果有)
|
||
|
|
this.originalMessageHandler = app.globalData.onNewMessage;
|
||
|
|
|
||
|
|
// 注册新的处理函数
|
||
|
|
app.globalData.onNewMessage = function(message) {
|
||
|
|
console.log('消息列表页面收到新消息:', message);
|
||
|
|
// 重新加载聊天列表
|
||
|
|
that.loadChatList();
|
||
|
|
|
||
|
|
// 调用原始处理函数(如果有)
|
||
|
|
if (that.originalMessageHandler && typeof that.originalMessageHandler === 'function') {
|
||
|
|
that.originalMessageHandler(message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('已注册全局新消息处理函数');
|
||
|
|
} catch (e) {
|
||
|
|
console.error('注册全局新消息处理函数失败:', e);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
onShow: function() {
|
||
|
|
// 每次显示页面时刷新聊天列表
|
||
|
|
this.loadChatList();
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 加载聊天列表
|
||
|
|
*/
|
||
|
|
loadChatList: function() {
|
||
|
|
try {
|
||
|
|
// 获取所有存储的聊天记录键
|
||
|
|
const storageInfo = wx.getStorageInfoSync();
|
||
|
|
const chatKeys = storageInfo.keys.filter(key => key.startsWith('chat_messages_'));
|
||
|
|
|
||
|
|
const messageList = [];
|
||
|
|
// 获取当前用户信息
|
||
|
|
const app = getApp();
|
||
|
|
const userInfo = app.globalData.userInfo || {};
|
||
|
|
const isCurrentUserManager = userInfo.userType === 'manager' || userInfo.type === 'manager';
|
||
|
|
const currentManagerId = userInfo.managerId || '';
|
||
|
|
const currentUserId = wx.getStorageSync('userId') || '';
|
||
|
|
|
||
|
|
// 临时存储已处理的会话,避免重复
|
||
|
|
const processedConversations = new Set();
|
||
|
|
|
||
|
|
// 遍历每个聊天记录
|
||
|
|
chatKeys.forEach(key => {
|
||
|
|
const chatUserId = key.replace('chat_messages_', '');
|
||
|
|
|
||
|
|
// 获取消息列表
|
||
|
|
const messages = wx.getStorageSync(key);
|
||
|
|
|
||
|
|
// 跳过空消息列表
|
||
|
|
if (!messages || messages.length === 0) return;
|
||
|
|
|
||
|
|
// 对于客服,需要特殊处理,确保能看到所有相关消息
|
||
|
|
if (isCurrentUserManager) {
|
||
|
|
// 避免处理自己与自己的聊天
|
||
|
|
if (chatUserId === currentManagerId) return;
|
||
|
|
|
||
|
|
// 客服需要看到所有有消息的对话,包括用户发送的消息
|
||
|
|
if (!processedConversations.has(chatUserId)) {
|
||
|
|
// 获取最后一条消息
|
||
|
|
const lastMessage = messages[messages.length - 1];
|
||
|
|
|
||
|
|
messageList.push({
|
||
|
|
userId: chatUserId,
|
||
|
|
userName: this.getUserNameById(chatUserId),
|
||
|
|
avatar: '',
|
||
|
|
lastMessage: this.formatMessagePreview(lastMessage),
|
||
|
|
lastMessageTime: this.formatMessageTime(lastMessage.time),
|
||
|
|
messageCount: messages.length
|
||
|
|
});
|
||
|
|
|
||
|
|
processedConversations.add(chatUserId);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// 普通用户,正常处理
|
||
|
|
// 避免处理自己与自己的聊天
|
||
|
|
if (chatUserId === currentUserId) return;
|
||
|
|
|
||
|
|
// 获取最后一条消息
|
||
|
|
const lastMessage = messages[messages.length - 1];
|
||
|
|
|
||
|
|
messageList.push({
|
||
|
|
userId: chatUserId,
|
||
|
|
userName: this.getUserNameById(chatUserId),
|
||
|
|
avatar: '',
|
||
|
|
lastMessage: this.formatMessagePreview(lastMessage),
|
||
|
|
lastMessageTime: this.formatMessageTime(lastMessage.time),
|
||
|
|
messageCount: messages.length
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// 按最后消息时间排序(最新的在前)
|
||
|
|
messageList.sort((a, b) => {
|
||
|
|
return new Date(b.lastMessageTime) - new Date(a.lastMessageTime);
|
||
|
|
});
|
||
|
|
|
||
|
|
this.setData({
|
||
|
|
messageList: messageList
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
console.error('加载聊天列表失败:', e);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据用户ID获取用户名
|
||
|
|
*/
|
||
|
|
getUserNameById: function(userId) {
|
||
|
|
try {
|
||
|
|
// 参数有效性检查
|
||
|
|
if (!userId || typeof userId === 'undefined') {
|
||
|
|
return '未知用户';
|
||
|
|
}
|
||
|
|
|
||
|
|
// 确保userId是字符串类型
|
||
|
|
const safeUserId = String(userId);
|
||
|
|
|
||
|
|
// 尝试从全局客服列表中获取用户名
|
||
|
|
const app = getApp();
|
||
|
|
|
||
|
|
// 尝试从本地缓存的客服列表中查找
|
||
|
|
const cachedCustomerServices = wx.getStorageSync('cached_customer_services') || [];
|
||
|
|
const service = cachedCustomerServices.find(item =>
|
||
|
|
item.id === safeUserId || item.managerId === safeUserId ||
|
||
|
|
String(item.id) === safeUserId || String(item.managerId) === safeUserId
|
||
|
|
);
|
||
|
|
|
||
|
|
if (service) {
|
||
|
|
return service.alias || service.name || '客服';
|
||
|
|
}
|
||
|
|
|
||
|
|
// 固定用户名映射
|
||
|
|
const userMap = {
|
||
|
|
'user1': '客服专员',
|
||
|
|
'user2': '商家客服',
|
||
|
|
'user_1': '张三',
|
||
|
|
'user_2': '李四',
|
||
|
|
'user_3': '王五',
|
||
|
|
'user_4': '赵六',
|
||
|
|
'user_5': '钱七',
|
||
|
|
'customer_service_1': '客服小王',
|
||
|
|
'customer_service_2': '客服小李',
|
||
|
|
'customer_service_3': '客服小张'
|
||
|
|
};
|
||
|
|
|
||
|
|
if (userMap[safeUserId]) {
|
||
|
|
return userMap[safeUserId];
|
||
|
|
}
|
||
|
|
|
||
|
|
// 对于manager_开头的ID,显示为客服
|
||
|
|
if (safeUserId.startsWith('manager_') || safeUserId.includes('manager')) {
|
||
|
|
return '客服-' + (safeUserId.length >= 4 ? safeUserId.slice(-4) : safeUserId);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果都没有找到,返回默认名称,避免出现undefined
|
||
|
|
if (safeUserId.length >= 4) {
|
||
|
|
return '用户' + safeUserId.slice(-4);
|
||
|
|
} else {
|
||
|
|
return '用户' + safeUserId;
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('获取用户名失败:', e);
|
||
|
|
return '未知用户';
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 格式化消息预览
|
||
|
|
*/
|
||
|
|
formatMessagePreview: function(message) {
|
||
|
|
if (message.type === 'system') {
|
||
|
|
return '[系统消息] ' + message.content;
|
||
|
|
} else {
|
||
|
|
const senderPrefix = message.sender === 'me' ? '我: ' : '';
|
||
|
|
// 限制预览长度
|
||
|
|
let preview = senderPrefix + message.content;
|
||
|
|
if (preview.length > 30) {
|
||
|
|
preview = preview.substring(0, 30) + '...';
|
||
|
|
}
|
||
|
|
return preview;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 格式化消息时间
|
||
|
|
*/
|
||
|
|
formatMessageTime: function(timeStr) {
|
||
|
|
if (!timeStr) return '';
|
||
|
|
|
||
|
|
// 假设时间格式为 "9-21 10:50" 或类似格式
|
||
|
|
const now = new Date();
|
||
|
|
let dateStr = timeStr;
|
||
|
|
|
||
|
|
// 如果没有包含年份,添加当前年份
|
||
|
|
if (!timeStr.includes('-') || timeStr.split('-').length < 3) {
|
||
|
|
const currentYear = now.getFullYear();
|
||
|
|
dateStr = timeStr.includes(' ') ?
|
||
|
|
`${currentYear}-${timeStr.split(' ')[0]} ${timeStr.split(' ')[1]}` :
|
||
|
|
`${currentYear}-${timeStr}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
const messageDate = new Date(dateStr);
|
||
|
|
const diffTime = Math.abs(now - messageDate);
|
||
|
|
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
||
|
|
|
||
|
|
if (diffDays === 0) {
|
||
|
|
// 今天的消息只显示时间
|
||
|
|
return timeStr.split(' ')[1] || timeStr;
|
||
|
|
} else if (diffDays === 1) {
|
||
|
|
// 昨天的消息
|
||
|
|
return '昨天';
|
||
|
|
} else if (diffDays < 7) {
|
||
|
|
// 一周内的消息显示星期
|
||
|
|
const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
||
|
|
return weekdays[messageDate.getDay()];
|
||
|
|
} else {
|
||
|
|
// 超过一周的消息显示日期
|
||
|
|
return timeStr.split(' ')[0] || timeStr;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 跳转到聊天详情页
|
||
|
|
*/
|
||
|
|
goToChat: function(e) {
|
||
|
|
const userId = e.currentTarget.dataset.userId;
|
||
|
|
// 查找对应的用户信息
|
||
|
|
const userInfo = this.data.messageList.find(item => item.userId === userId);
|
||
|
|
|
||
|
|
wx.navigateTo({
|
||
|
|
url: `/pages/chat-detail/index?userId=${userId}&userName=${encodeURIComponent(userInfo?.userName || '')}`
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
||
|
|
*/
|
||
|
|
onPullDownRefresh: function() {
|
||
|
|
this.loadChatList();
|
||
|
|
// 停止下拉刷新
|
||
|
|
wx.stopPullDownRefresh();
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 处理清除聊天记录
|
||
|
|
*/
|
||
|
|
onUnload: function() {
|
||
|
|
// 页面卸载时的清理工作
|
||
|
|
// 恢复原始的全局消息处理函数
|
||
|
|
try {
|
||
|
|
const app = getApp();
|
||
|
|
if (this.originalMessageHandler) {
|
||
|
|
app.globalData.onNewMessage = this.originalMessageHandler;
|
||
|
|
} else {
|
||
|
|
// 如果没有原始处理函数,则清除当前的
|
||
|
|
delete app.globalData.onNewMessage;
|
||
|
|
}
|
||
|
|
console.log('已清理全局新消息处理函数');
|
||
|
|
} catch (e) {
|
||
|
|
console.error('清理全局新消息处理函数失败:', e);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
handleClearChat: function(e) {
|
||
|
|
const userId = e.currentTarget.dataset.userId;
|
||
|
|
|
||
|
|
wx.showModal({
|
||
|
|
title: '确认清空',
|
||
|
|
content: '确定要清空与该用户的所有聊天记录吗?此操作不可恢复。',
|
||
|
|
success: (res) => {
|
||
|
|
if (res.confirm) {
|
||
|
|
this.clearChatHistory(userId);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 清空指定用户的聊天记录
|
||
|
|
*/
|
||
|
|
clearChatHistory: function(userId) {
|
||
|
|
try {
|
||
|
|
wx.removeStorageSync(`chat_messages_${userId}`);
|
||
|
|
console.log('已清空用户', userId, '的聊天记录');
|
||
|
|
|
||
|
|
// 刷新消息列表
|
||
|
|
this.loadChatList();
|
||
|
|
|
||
|
|
wx.showToast({
|
||
|
|
title: '聊天记录已清空',
|
||
|
|
icon: 'success'
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
console.error('清空聊天记录失败:', e);
|
||
|
|
wx.showToast({
|
||
|
|
title: '清空失败',
|
||
|
|
icon: 'none'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|