|
|
@ -951,8 +951,8 @@ module.exports = { |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 添加聊天记录 - 为用户和客服创建双向聊天记录,避免重复创建
|
|
|
// 添加聊天记录 - 为用户和客服创建双向聊天记录,避免重复创建
|
|
|
addChatRecord: function (user_phone, manager_phone) { |
|
|
addChatRecord: function (user_phone, manager_phone, unread = 0) { |
|
|
console.log('API.addChatRecord - user_phone:', user_phone, 'manager_phone:', manager_phone); |
|
|
console.log('API.addChatRecord - user_phone:', user_phone, 'manager_phone:', manager_phone, 'unread:', unread); |
|
|
if (!user_phone || !manager_phone) { |
|
|
if (!user_phone || !manager_phone) { |
|
|
return Promise.reject(new Error('用户手机号和客服手机号不能为空')); |
|
|
return Promise.reject(new Error('用户手机号和客服手机号不能为空')); |
|
|
} |
|
|
} |
|
|
@ -976,7 +976,8 @@ module.exports = { |
|
|
// 2. 如果不存在,则调用服务器API创建聊天记录
|
|
|
// 2. 如果不存在,则调用服务器API创建聊天记录
|
|
|
return request('/api/chat/add', 'POST', { |
|
|
return request('/api/chat/add', 'POST', { |
|
|
user_phone: user_phone, |
|
|
user_phone: user_phone, |
|
|
manager_phone: manager_phone |
|
|
manager_phone: manager_phone, |
|
|
|
|
|
unread: unread // 使用传入的未读消息数或默认值0
|
|
|
}).then(res => { |
|
|
}).then(res => { |
|
|
console.log('添加聊天记录服务器响应:', res); |
|
|
console.log('添加聊天记录服务器响应:', res); |
|
|
// 服务器返回200状态码或success=true都表示成功,包括"聊天记录已存在"的情况
|
|
|
// 服务器返回200状态码或success=true都表示成功,包括"聊天记录已存在"的情况
|
|
|
@ -998,7 +999,8 @@ module.exports = { |
|
|
console.log('获取聊天列表失败,尝试直接创建聊天记录'); |
|
|
console.log('获取聊天列表失败,尝试直接创建聊天记录'); |
|
|
return request('/api/chat/add', 'POST', { |
|
|
return request('/api/chat/add', 'POST', { |
|
|
user_phone: user_phone, |
|
|
user_phone: user_phone, |
|
|
manager_phone: manager_phone |
|
|
manager_phone: manager_phone, |
|
|
|
|
|
unread: unread // 使用传入的未读消息数或默认值0
|
|
|
}).then(res => { |
|
|
}).then(res => { |
|
|
console.log('添加聊天记录服务器响应:', res); |
|
|
console.log('添加聊天记录服务器响应:', res); |
|
|
if (res && (res.success || res.code === 200)) { |
|
|
if (res && (res.success || res.code === 200)) { |
|
|
@ -3889,6 +3891,82 @@ module.exports = { |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 更新聊天列表的未读消息数
|
|
|
|
|
|
updateUnreadCount: function (managerPhone, increment = 1) { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
console.log('API.updateUnreadCount - managerPhone:', managerPhone, 'increment:', increment); |
|
|
|
|
|
|
|
|
|
|
|
// 验证必要参数
|
|
|
|
|
|
if (!managerPhone) { |
|
|
|
|
|
const error = new Error('聊天对象手机号不能为空'); |
|
|
|
|
|
console.error('API.updateUnreadCount - 错误:', error); |
|
|
|
|
|
reject(error); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取当前用户的手机号
|
|
|
|
|
|
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) { |
|
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber') || wx.getStorageSync('phone'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
|
|
const error = new Error('用户手机号不能为空'); |
|
|
|
|
|
console.error('API.updateUnreadCount - 错误:', error); |
|
|
|
|
|
reject(error); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const requestData = { |
|
|
|
|
|
user_phone: userPhone, |
|
|
|
|
|
manager_phone: managerPhone, |
|
|
|
|
|
increment: increment |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
console.log('API.updateUnreadCount - 请求数据:', requestData); |
|
|
|
|
|
|
|
|
|
|
|
request('/api/chat/updateUnread', 'POST', requestData).then(res => { |
|
|
|
|
|
console.log('API.updateUnreadCount - 响应数据:', res); |
|
|
|
|
|
|
|
|
|
|
|
// 处理不同格式的响应
|
|
|
|
|
|
if (res && (res.success || res.code === 200)) { |
|
|
|
|
|
resolve(res); |
|
|
|
|
|
} else { |
|
|
|
|
|
const errorMessage = res && res.message ? res.message : '更新未读消息数失败'; |
|
|
|
|
|
reject(new Error(errorMessage)); |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
|
|
|
console.error('API.updateUnreadCount - 请求失败:', error); |
|
|
|
|
|
reject(error); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
// 删除聊天消息(单向删除,删除数据库对应的两个手机号码的数据)
|
|
|
// 删除聊天消息(单向删除,删除数据库对应的两个手机号码的数据)
|
|
|
deleteChatMessage: function (userPhone, managerPhone) { |
|
|
deleteChatMessage: function (userPhone, managerPhone) { |
|
|
console.log('API.deleteChatMessage - userPhone:', userPhone, 'managerPhone:', managerPhone); |
|
|
console.log('API.deleteChatMessage - userPhone:', userPhone, 'managerPhone:', managerPhone); |
|
|
|