Browse Source

实现长按删除聊天消息功能,使用user_phone和manager_phone参数

pull/1/head
徐飞洋 2 months ago
parent
commit
9a8ed2b07d
  1. 99
      pages/chat/index.js
  2. 1
      pages/chat/index.wxml
  3. 37
      server-example/server-mysql.js
  4. 27
      utils/api.js

99
pages/chat/index.js

@ -447,6 +447,24 @@ Page({
}); });
}, },
// 聊天项长按事件
onChatItemLongPress: function (e) {
const index = e.currentTarget.dataset.index;
const chatItem = this.data.filteredChatList[index];
// 显示删除确认对话框
wx.showModal({
title: '删除聊天',
content: '确定要删除这条聊天记录吗?',
success: (res) => {
if (res.confirm) {
// 调用删除函数
this.deleteChatMessage(chatItem, index);
}
}
});
},
// 聊天项点击事件 // 聊天项点击事件
onChatItemTap: function (e) { onChatItemTap: function (e) {
const index = e.currentTarget.dataset.index; const index = e.currentTarget.dataset.index;
@ -571,6 +589,87 @@ Page({
} }
}, },
// 删除聊天消息
deleteChatMessage: function (chatItem, index) {
// 获取用户手机号
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) {
wx.showToast({
title: '请先登录并绑定手机号',
icon: 'none'
});
return;
}
// 调用API删除消息
// 使用当前用户的电话号码作为userPhone,对方的电话号码作为managerPhone
API.deleteChatMessage(userPhone, chatItem.manager_phone).then(() => {
// 更新本地聊天列表数据
// 1. 更新filteredChatList
const updatedFilteredChatList = [...this.data.filteredChatList];
updatedFilteredChatList.splice(index, 1);
// 2. 更新chatList
const updatedChatList = [...this.data.chatList];
const originalIndex = updatedChatList.findIndex(item => item.manager_phone === chatItem.manager_phone);
if (originalIndex !== -1) {
updatedChatList.splice(originalIndex, 1);
}
// 3. 更新页面数据
this.setData({
filteredChatList: updatedFilteredChatList,
chatList: updatedChatList
});
// 显示删除成功提示
wx.showToast({
title: '删除成功',
icon: 'success'
});
}).catch(error => {
console.error('删除聊天消息失败:', error);
wx.showToast({
title: error.message || '删除失败,请稍后重试',
icon: 'none'
});
});
},
// 格式化时间显示 // 格式化时间显示
formatDateTime: function (dateString) { formatDateTime: function (dateString) {
if (!dateString) return '刚刚'; if (!dateString) return '刚刚';

1
pages/chat/index.wxml

@ -21,6 +21,7 @@
data-id="{{item.id}}" data-id="{{item.id}}"
data-index="{{index}}" data-index="{{index}}"
bindtap="onChatItemTap" bindtap="onChatItemTap"
bindlongpress="onChatItemLongPress"
> >
<!-- 头像 --> <!-- 头像 -->
<view class="avatar-container"> <view class="avatar-container">

37
server-example/server-mysql.js

@ -8081,6 +8081,43 @@ app.post('/api/chat/list', async (req, res) => {
} }
}); });
// 新增:删除聊天记录
app.post('/api/chat/delete', async (req, res) => {
try {
const { user_phone, manager_phone } = req.body;
console.log('删除聊天记录 - user_phone:', user_phone, 'manager_phone:', manager_phone);
if (!user_phone || !manager_phone) {
return res.status(400).json({
success: false,
code: 400,
message: '用户手机号和业务员手机号不能为空'
});
}
// 删除chat_list表中的记录
const deleteResult = await sequelize.query(
'DELETE FROM chat_list WHERE user_phone = ? AND manager_phone = ?',
{ replacements: [user_phone, manager_phone], type: sequelize.QueryTypes.DELETE }
);
console.log('删除聊天记录结果:', deleteResult);
return res.status(200).json({
success: true,
code: 200,
message: '删除成功'
});
} catch (error) {
console.error('删除聊天记录时出错:', error);
return res.status(500).json({
success: false,
code: 500,
message: '删除失败: ' + error.message
});
}
});
// 新增:添加聊天记录到chat_list表 // 新增:添加聊天记录到chat_list表
app.post('/api/chat/add', async (req, res) => { app.post('/api/chat/add', async (req, res) => {
try { try {

27
utils/api.js

@ -3666,7 +3666,32 @@ module.exports = {
}); });
}, },
// 获取聊天列表数据 // 删除聊天消息(单向删除,删除数据库对应的两个手机号码的数据)
deleteChatMessage: function (userPhone, managerPhone) {
console.log('API.deleteChatMessage - userPhone:', userPhone, 'managerPhone:', managerPhone);
return new Promise((resolve, reject) => {
// 验证必要参数
if (!userPhone || !managerPhone) {
reject(new Error('发送者电话和接收者电话不能为空'));
return;
}
const requestData = {
user_phone: userPhone,
manager_phone: managerPhone
};
request('/api/chat/delete', 'POST', requestData).then(res => {
console.log('删除聊天消息成功:', res);
resolve(res);
}).catch(error => {
console.error('删除聊天消息失败:', error);
reject(new Error(error.message || '删除聊天消息失败,请稍后重试'));
});
});
},
// 获取聊天列表
getChatList: function (userPhone) { getChatList: function (userPhone) {
console.log('API.getChatList - userPhone:', userPhone); console.log('API.getChatList - userPhone:', userPhone);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

Loading…
Cancel
Save