|
|
|
@ -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) { |
|
|
|
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) { |
|
|
|
if (!dateString) return '刚刚'; |
|
|
|
|