// pages/chat/index.js const API = require('../../utils/api.js'); Page({ data: { chatList: [], searchKeyword: '', filteredChatList: [], timer: null, debugCount: 0, // 调试信息输出计数器 loginToastShown: false, // 登录提示是否已显示 lastLoadTime: 0 // 用于节流的时间戳 }, onLoad: function (options) { this.loadChatList(); }, // 检查用户是否已登录(与loadChatList中的手机号检查逻辑保持一致) isUserLoggedIn: function () { const users = wx.getStorageSync('users') || {}; const userId = wx.getStorageSync('userId'); let userPhone = null; // 与loadChatList中的手机号检查逻辑完全一致 if (userId && users[userId]) { if (users[userId].phoneNumber) { userPhone = users[userId].phoneNumber; } else if (users[userId].phone) { userPhone = users[userId].phone; } } if (!userPhone) { const userInfo = wx.getStorageSync('userInfo'); if (userInfo) { if (userInfo.phoneNumber) { userPhone = userInfo.phoneNumber; } else if (userInfo.phone) { userPhone = userInfo.phone; } } } if (!userPhone) { userPhone = wx.getStorageSync('phoneNumber') || wx.getStorageSync('phone'); } if (!userPhone) { const loginInfo = wx.getStorageSync('loginInfo'); if (loginInfo) { if (loginInfo.phoneNumber) { userPhone = loginInfo.phoneNumber; } else if (loginInfo.phone) { userPhone = loginInfo.phone; } } } // 只有获取到手机号才视为已登录 return !!userPhone; }, onShow: function () { // 只有已登录用户才执行操作 if (this.isUserLoggedIn()) { // 从聊天详情页返回时,强制刷新列表,绕过节流限制 // 保存当前时间戳,用于强制刷新 const forcedLoadTime = Date.now(); this.setData({ lastLoadTime: forcedLoadTime - 3000, // 设置为3秒前,确保能绕过2秒的节流限制 }, () => { // 重新加载聊天列表,实现从聊天详情页返回时立即更新 this.loadChatList(); }); // 启动定时器 if (!this.data.timer) { this.startTimer(); } } }, startTimer: function () { // 确保只有一个定时器在运行 if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: null }); } // 设置30秒刷新定时器 this.setData({ timer: setInterval(() => { this.loadChatList(); }, 30000) }); }, loadChatList: function () { // 节流逻辑:避免短时间内多次调用 const currentTime = Date.now(); if (currentTime - this.data.lastLoadTime < 2000) { // 2秒内不重复调用 console.log('API调用节流中,距离上次调用仅' + (currentTime - this.data.lastLoadTime) + 'ms'); return; } // 更新上次加载时间 this.setData({ lastLoadTime: currentTime }); // 增加调试信息计数器 const newDebugCount = this.data.debugCount + 1; this.setData({ debugCount: newDebugCount }); // 获取用户手机号 - 增强版,增加更多获取途径和调试信息 const users = wx.getStorageSync('users') || {}; const userId = wx.getStorageSync('userId'); let userPhone = null; // 添加调试信息,显示当前存储的用户信息 console.log('调试信息 - 登录用户 (第' + newDebugCount + '次):', { userId: userId, users: users, userInfo: wx.getStorageSync('userInfo'), phoneNumber: wx.getStorageSync('phoneNumber'), phone: wx.getStorageSync('phone'), debugCount: newDebugCount }); // 尝试从users中获取手机号(支持不同的键名) if (userId && users[userId]) { if (users[userId].phoneNumber) { userPhone = users[userId].phoneNumber; console.log('从users[userId].phoneNumber获取到手机号:', userPhone); } else if (users[userId].phone) { userPhone = users[userId].phone; console.log('从users[userId].phone获取到手机号:', userPhone); } } // 如果还没有获取到,尝试从全局用户信息获取 if (!userPhone) { const userInfo = wx.getStorageSync('userInfo'); if (userInfo) { if (userInfo.phoneNumber) { userPhone = userInfo.phoneNumber; console.log('从userInfo.phoneNumber获取到手机号:', userPhone); } else if (userInfo.phone) { userPhone = userInfo.phone; console.log('从userInfo.phone获取到手机号:', userPhone); } } } // 如果还没有获取到,尝试从直接存储获取(支持不同的键名) if (!userPhone) { if (wx.getStorageSync('phoneNumber')) { userPhone = wx.getStorageSync('phoneNumber'); console.log('从storage.phoneNumber获取到手机号:', userPhone); } else if (wx.getStorageSync('phone')) { userPhone = wx.getStorageSync('phone'); console.log('从storage.phone获取到手机号:', userPhone); } } // 如果还没有获取到,尝试从更多可能的存储位置获取 if (!userPhone) { const loginInfo = wx.getStorageSync('loginInfo'); if (loginInfo) { if (loginInfo.phoneNumber) { userPhone = loginInfo.phoneNumber; console.log('从loginInfo.phoneNumber获取到手机号:', userPhone); } else if (loginInfo.phone) { userPhone = loginInfo.phone; console.log('从loginInfo.phone获取到手机号:', userPhone); } } } // 如果没有手机号,显示错误提示 if (!userPhone) { wx.hideLoading(); // 登录提示只显示一次 if (!this.data.loginToastShown) { wx.showToast({ title: '请先登录并绑定手机号', icon: 'none' }); this.setData({ loginToastShown: true }); } // 调试信息输出3次后返回首页并关闭定时器 if (newDebugCount >= 3) { console.log('调试信息已输出3次,准备返回首页并关闭定时器'); // 关闭定时器 if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: null }); } // 返回首页 wx.navigateBack({ delta: 1, success: function() { console.log('已成功返回首页'); } }); } return; } // 调用API获取聊天列表 API.getChatList(userPhone).then(res => { if (res && Array.isArray(res)) { // 创建业务员信息缓存,避免重复调用API const personnelCache = {}; // 处理每个聊天项,获取业务员信息 const chatListPromises = res.map(async (chatItem) => { if (chatItem.manager_phone) { try { // 先检查缓存中是否已有该业务员信息 if (personnelCache[chatItem.manager_phone]) { const cachedInfo = personnelCache[chatItem.manager_phone]; chatItem.name = cachedInfo.alias || chatItem.manager_phone; chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; console.log('使用缓存的业务员信息:', chatItem.manager_phone); } else { // 获取业务员信息 const personnelInfo = await API.getSalesPersonnelInfo(chatItem.manager_phone); if (personnelInfo) { // 缓存业务员信息 personnelCache[chatItem.manager_phone] = personnelInfo; // 使用业务员的alias作为显示名称 chatItem.name = personnelInfo.alias || chatItem.manager_phone; chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; console.log('获取并缓存业务员信息:', chatItem.manager_phone); } } } catch (error) { console.error('获取业务员信息失败:', error); // 如果获取失败,使用默认名称 chatItem.name = chatItem.manager_phone; chatItem.avatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; } } else { // 系统消息或其他没有manager_phone的消息 chatItem.name = chatItem.name || '系统消息'; chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; } // 修复:对所有没有消息内容的聊天项获取最新消息内容 if (!chatItem.content) { try { const messages = await API.getChatMessages(chatItem.manager_phone, userPhone, { limit: 1 }); if (messages.length > 0) { chatItem.content = messages[0].content || '暂无消息内容'; // 同时更新时间为最新消息的时间 chatItem.time = messages[0].created_at || null; } else { chatItem.content = '暂无消息内容'; } } catch (error) { console.error('获取聊天消息失败:', error); chatItem.content = '暂无消息内容'; } } // 格式化时间 chatItem.time = this.formatDateTime(chatItem.time || null); // 确保unread字段存在 chatItem.unread = chatItem.unread || false; return chatItem; }); // 等待所有聊天项处理完成 Promise.all(chatListPromises).then(processedChatList => { this.setData({ chatList: processedChatList, filteredChatList: processedChatList }); wx.hideLoading(); }).catch(error => { console.error('处理聊天列表失败:', error); wx.hideLoading(); wx.showToast({ title: '加载聊天列表失败', icon: 'none' }); }); } else { wx.hideLoading(); wx.showToast({ title: '加载聊天列表失败', icon: 'none' }); } }).catch(error => { console.error('获取聊天列表失败:', error); wx.hideLoading(); wx.showToast({ title: error.message || '加载聊天列表失败', icon: 'none' }); }); }, // 搜索功能 onSearchInput: function (e) { const keyword = e.detail.value; this.setData({ searchKeyword: keyword }); this.filterChatList(keyword); }, // 过滤聊天列表 filterChatList: function (keyword) { if (!keyword) { this.setData({ filteredChatList: this.data.chatList }); return; } const filteredList = this.data.chatList.filter(item => { return item.name.includes(keyword) || item.content.includes(keyword); }); this.setData({ filteredChatList: filteredList }); }, // 聊天项点击事件 onChatItemTap: function (e) { const index = e.currentTarget.dataset.index; const chatItem = this.data.filteredChatList[index]; // 使用对方的电话号码作为聊天ID,而不是会话ID const chatId = chatItem.manager_phone; // 跳转到聊天详情页,传递chatId和name参数 wx.navigateTo({ url: '/pages/chat-detail/index?id=' + chatId + '&name=' + encodeURIComponent(chatItem.name), success: function () { console.log('成功跳转到聊天详情页'); }, fail: function (error) { console.error('跳转到聊天详情页失败:', error); wx.showToast({ title: '聊天功能开发中', icon: 'none' }); } }); }, onPullDownRefresh: function () { this.loadChatList(); wx.stopPullDownRefresh(); }, onHide: function () { if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: null }); } }, onUnload: function () { if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: null }); } }, // 格式化时间显示 formatDateTime: function (dateString) { if (!dateString) return '刚刚'; const now = new Date(); const msgDate = new Date(dateString); const diffMs = now - msgDate; const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMs / 3600000); const diffDays = Math.floor(diffMs / 86400000); if (diffMins < 1) { return '刚刚'; } else if (diffMins < 60) { return `${diffMins}分钟前`; } else if (diffHours < 24) { return `${diffHours}小时前`; } else if (diffDays < 7) { return `${diffDays}天前`; } else { // 超过一周显示具体日期 const year = msgDate.getFullYear(); const month = (msgDate.getMonth() + 1).toString().padStart(2, '0'); const day = msgDate.getDate().toString().padStart(2, '0'); const hours = msgDate.getHours().toString().padStart(2, '0'); const minutes = msgDate.getMinutes().toString().padStart(2, '0'); return `${year}-${month}-${day} ${hours}:${minutes}`; } } });