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.
189 lines
4.6 KiB
189 lines
4.6 KiB
// pages/chat-detail/index.js
|
|
const API = require('../../utils/api.js');
|
|
|
|
Page({
|
|
data: {
|
|
chatId: null,
|
|
messages: [],
|
|
inputValue: '',
|
|
loading: false,
|
|
chatTitle: '聊天对象',
|
|
managerPhone: null
|
|
},
|
|
|
|
onLoad: function (options) {
|
|
if (options.id) {
|
|
this.setData({
|
|
chatId: options.id
|
|
});
|
|
// 获取聊天标题
|
|
this.loadChatTitle();
|
|
}
|
|
this.loadMessages();
|
|
},
|
|
|
|
// 加载聊天标题
|
|
loadChatTitle: function () {
|
|
// 从本地存储获取聊天列表
|
|
const users = wx.getStorageSync('users') || {};
|
|
const userId = wx.getStorageSync('userId');
|
|
let userPhone = null;
|
|
|
|
// 尝试从users中获取手机号
|
|
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) {
|
|
if (wx.getStorageSync('phoneNumber')) {
|
|
userPhone = wx.getStorageSync('phoneNumber');
|
|
} else if (wx.getStorageSync('phone')) {
|
|
userPhone = wx.getStorageSync('phone');
|
|
}
|
|
}
|
|
|
|
if (userPhone) {
|
|
// 获取聊天列表
|
|
API.getChatList(userPhone).then(chatList => {
|
|
if (Array.isArray(chatList)) {
|
|
// 找到当前聊天项
|
|
const currentChat = chatList.find(item => item.id === this.data.chatId);
|
|
if (currentChat && currentChat.manager_phone) {
|
|
this.setData({ managerPhone: currentChat.manager_phone });
|
|
// 获取业务员信息
|
|
API.getSalesPersonnelInfo(currentChat.manager_phone).then(personnelInfo => {
|
|
if (personnelInfo && personnelInfo.alias) {
|
|
this.setData({ chatTitle: personnelInfo.alias });
|
|
}
|
|
}).catch(error => {
|
|
console.error('获取业务员信息失败:', error);
|
|
});
|
|
}
|
|
}
|
|
}).catch(error => {
|
|
console.error('获取聊天列表失败:', error);
|
|
});
|
|
}
|
|
},
|
|
|
|
onBack: function () {
|
|
wx.navigateBack();
|
|
},
|
|
|
|
loadMessages: function () {
|
|
this.setData({ loading: true });
|
|
|
|
// 模拟加载聊天记录
|
|
setTimeout(() => {
|
|
const messages = [
|
|
{
|
|
id: 1,
|
|
content: '您好,有什么可以帮助您的吗?',
|
|
sender: 'other',
|
|
time: '刚刚'
|
|
},
|
|
{
|
|
id: 2,
|
|
content: '你好,我想咨询一下产品信息',
|
|
sender: 'me',
|
|
time: '刚刚'
|
|
},
|
|
{
|
|
id: 3,
|
|
content: '当然可以,请问您想了解哪种产品?',
|
|
sender: 'other',
|
|
time: '5分钟前'
|
|
}
|
|
];
|
|
|
|
this.setData({
|
|
messages: messages,
|
|
loading: false
|
|
});
|
|
}, 1000);
|
|
},
|
|
|
|
loadMoreMessages: function () {
|
|
// 加载更多历史消息
|
|
if (this.data.loading) return;
|
|
|
|
this.setData({ loading: true });
|
|
|
|
// 模拟加载更多历史消息
|
|
setTimeout(() => {
|
|
const moreMessages = [
|
|
{
|
|
id: 4,
|
|
content: '你好,我想了解一下你们的鸡蛋产品',
|
|
sender: 'me',
|
|
time: '1小时前'
|
|
},
|
|
{
|
|
id: 5,
|
|
content: '您好,欢迎咨询我们的鸡蛋产品',
|
|
sender: 'other',
|
|
time: '1小时前'
|
|
}
|
|
];
|
|
|
|
this.setData({
|
|
messages: [...moreMessages, ...this.data.messages],
|
|
loading: false
|
|
});
|
|
}, 1000);
|
|
},
|
|
|
|
onInputChange: function (e) {
|
|
this.setData({
|
|
inputValue: e.detail.value
|
|
});
|
|
},
|
|
|
|
sendMessage: function () {
|
|
const content = this.data.inputValue.trim();
|
|
if (!content) return;
|
|
|
|
const newMessage = {
|
|
id: Date.now(),
|
|
content: content,
|
|
sender: 'me',
|
|
time: '刚刚'
|
|
};
|
|
|
|
this.setData({
|
|
messages: [...this.data.messages, newMessage],
|
|
inputValue: ''
|
|
});
|
|
|
|
// 模拟对方回复
|
|
setTimeout(() => {
|
|
const reply = {
|
|
id: Date.now() + 1,
|
|
content: '这是一条自动回复',
|
|
sender: 'other',
|
|
time: '刚刚'
|
|
};
|
|
|
|
this.setData({
|
|
messages: [...this.data.messages, reply]
|
|
});
|
|
}, 1000);
|
|
}
|
|
});
|