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.

214 lines
6.8 KiB

// 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;
// 添加调试信息,显示当前存储的用户信息
console.log('调试信息 - 登录用户:', {
userId: userId,
users: users,
userInfo: wx.getStorageSync('userInfo'),
phoneNumber: wx.getStorageSync('phoneNumber'),
phone: wx.getStorageSync('phone')
});
// 尝试从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();
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();
}
});