|
|
|
|
// 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
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 如果有传递name参数,直接使用该名称作为聊天标题
|
|
|
|
|
if (options.name) {
|
|
|
|
|
this.setData({
|
|
|
|
|
chatTitle: decodeURIComponent(options.name)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 否则从API获取聊天标题
|
|
|
|
|
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();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 格式化时间显示
|
|
|
|
|
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}`;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loadMessages: function () {
|
|
|
|
|
this.setData({ loading: true });
|
|
|
|
|
|
|
|
|
|
// 获取当前用户的手机号
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用新添加的API获取聊天记录
|
|
|
|
|
API.getChatMessages(this.data.chatId, userPhone).then(res => {
|
|
|
|
|
if (Array.isArray(res)) {
|
|
|
|
|
// 处理每条消息,确定发送者和格式化时间
|
|
|
|
|
const processedMessages = res.map(message => {
|
|
|
|
|
// 判断消息是发送还是接收
|
|
|
|
|
const sender = (message.sender_phone === userPhone) ? 'me' : 'other';
|
|
|
|
|
|
|
|
|
|
// 格式化时间
|
|
|
|
|
const time = this.formatDateTime(message.created_at);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: message.id,
|
|
|
|
|
content: message.content,
|
|
|
|
|
sender: sender,
|
|
|
|
|
time: time,
|
|
|
|
|
originalTime: message.created_at
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 按时间顺序排序(升序)
|
|
|
|
|
processedMessages.sort((a, b) => {
|
|
|
|
|
return new Date(a.originalTime) - new Date(b.originalTime);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
messages: processedMessages,
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setData({
|
|
|
|
|
messages: [],
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
console.error('加载聊天记录失败:', error);
|
|
|
|
|
this.setData({
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '加载聊天记录失败',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loadMoreMessages: function () {
|
|
|
|
|
// 加载更多历史消息
|
|
|
|
|
if (this.data.loading) return;
|
|
|
|
|
|
|
|
|
|
this.setData({ loading: true });
|
|
|
|
|
|
|
|
|
|
// 获取当前用户的手机号
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取最早的消息时间,用于分页加载
|
|
|
|
|
const earliestTime = this.data.messages.length > 0 ? this.data.messages[0].originalTime : null;
|
|
|
|
|
|
|
|
|
|
// 使用API获取更多聊天记录(带分页参数)
|
|
|
|
|
API.getChatMessages(this.data.chatId, userPhone, { before: earliestTime }).then(res => {
|
|
|
|
|
if (Array.isArray(res) && res.length > 0) {
|
|
|
|
|
// 处理每条消息,确定发送者和格式化时间
|
|
|
|
|
const processedMessages = res.map(message => {
|
|
|
|
|
// 判断消息是发送还是接收
|
|
|
|
|
const sender = (message.sender_phone === userPhone) ? 'me' : 'other';
|
|
|
|
|
|
|
|
|
|
// 格式化时间
|
|
|
|
|
const time = this.formatDateTime(message.created_at);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: message.id,
|
|
|
|
|
content: message.content,
|
|
|
|
|
sender: sender,
|
|
|
|
|
time: time,
|
|
|
|
|
originalTime: message.created_at
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 按时间顺序排序(升序)
|
|
|
|
|
processedMessages.sort((a, b) => {
|
|
|
|
|
return new Date(a.originalTime) - new Date(b.originalTime);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
messages: [...processedMessages, ...this.data.messages],
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setData({
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '没有更多消息了',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
console.error('加载更多聊天记录失败:', error);
|
|
|
|
|
this.setData({
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '加载更多消息失败',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|