// pages/chat/index.js const API = require('../../utils/api.js'); Page({ data: { chatList: [], searchKeyword: '', filteredChatList: [] }, onLoad: function (options) { this.loadChatList(); }, loadChatList: function () { // 显示加载提示 wx.showLoading({ title: '加载中...', }); // 获取用户手机号 const users = wx.getStorageSync('users') || {}; const userId = wx.getStorageSync('userId'); let userPhone = null; // 尝试从users中获取手机号 if (userId && users[userId] && users[userId].phoneNumber) { userPhone = users[userId].phoneNumber; } else { // 尝试从全局用户信息获取 const userInfo = wx.getStorageSync('userInfo'); if (userInfo && userInfo.phoneNumber) { userPhone = userInfo.phoneNumber; } else { // 尝试从直接存储的phoneNumber获取 userPhone = wx.getStorageSync('phoneNumber'); } } // 如果没有手机号,显示错误提示 if (!userPhone) { wx.hideLoading(); wx.showToast({ title: '请先登录并绑定手机号', icon: 'none' }); return; } // 调用API获取聊天列表 API.getChatList(userPhone).then(res => { if (res && Array.isArray(res)) { // 处理每个聊天项,获取业务员信息 const chatListPromises = res.map(async (chatItem) => { if (chatItem.manager_phone) { try { // 获取业务员信息 const personnelInfo = await API.getSalesPersonnelInfo(chatItem.manager_phone); if (personnelInfo) { // 使用业务员的alias作为显示名称 chatItem.name = personnelInfo.alias || chatItem.manager_phone; chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; } } 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'; } // 确保消息内容存在 chatItem.content = chatItem.content || '暂无消息内容'; // 确保时间存在 chatItem.time = chatItem.time || '刚刚'; // 确保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 chatId = e.currentTarget.dataset.id; // 跳转到聊天详情页 wx.navigateTo({ url: '/pages/chat-detail/index?id=' + chatId, success: function () { console.log('成功跳转到聊天详情页'); }, fail: function (error) { console.error('跳转到聊天详情页失败:', error); wx.showToast({ title: '聊天功能开发中', icon: 'none' }); } }); }, onPullDownRefresh: function () { this.loadChatList(); wx.stopPullDownRefresh(); } });