云文件管理系统上传组件优化
This commit is contained in:
@@ -429,9 +429,23 @@ const triggerImageUpload = () => { if (!uploading.value) imageInputRef.value?.cl
|
|||||||
const triggerFileUpload = () => { if (!uploading.value) fileInputRef.value?.click() }
|
const triggerFileUpload = () => { if (!uploading.value) fileInputRef.value?.click() }
|
||||||
|
|
||||||
const pushMessage = (msg) => {
|
const pushMessage = (msg) => {
|
||||||
const contactId = msg.isSelf ? msg.toUserId : msg.fromUserId
|
// 确定消息应该放在哪个联系人下
|
||||||
|
let contactId
|
||||||
|
if (msg.isSelf) {
|
||||||
|
// 自己发送的消息,放到接收方(toUserId)下
|
||||||
|
contactId = msg.toUserId
|
||||||
|
} else {
|
||||||
|
// 收到的消息,放到发送方(fromUserId)下
|
||||||
|
contactId = msg.fromUserId
|
||||||
|
}
|
||||||
|
|
||||||
if (!messages.value[contactId]) messages.value[contactId] = []
|
if (!messages.value[contactId]) messages.value[contactId] = []
|
||||||
|
// 使用唯一 key 避免重复
|
||||||
|
const msgKey = msg.tempType ? `temp_${msg.id}` : msg.id
|
||||||
|
const exists = messages.value[contactId].some(m => (m.tempType ? `temp_${m.id}` : m.id) === msgKey)
|
||||||
|
if (!exists) {
|
||||||
messages.value[contactId].push(msg)
|
messages.value[contactId].push(msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleImageSelect = async (e) => {
|
const handleImageSelect = async (e) => {
|
||||||
@@ -581,14 +595,24 @@ const onDialogOpen = () => {
|
|||||||
unsubscribeWs = chatService.onMessage((data) => {
|
unsubscribeWs = chatService.onMessage((data) => {
|
||||||
if (data.type === 'chat') {
|
if (data.type === 'chat') {
|
||||||
const msg = data.message
|
const msg = data.message
|
||||||
const isSelf = Number(msg.fromUserId) === Number(userStore.userId)
|
const currentUserId = Number(userStore.userId)
|
||||||
|
const msgFromUserId = Number(msg.fromUserId)
|
||||||
|
const msgToUserId = Number(msg.toUserId)
|
||||||
|
|
||||||
|
// 判断是否是自己发送的消息
|
||||||
|
const isSelf = (msgFromUserId === currentUserId)
|
||||||
msg.isSelf = isSelf
|
msg.isSelf = isSelf
|
||||||
msg.sending = false
|
msg.sending = false
|
||||||
msg.failed = false
|
msg.failed = false
|
||||||
msg.fromColor = colors[Math.abs(Number(msg.fromUserId) % colors.length)]
|
msg.fromColor = colors[Math.abs(msgFromUserId % colors.length)]
|
||||||
const targetUserId = isSelf ? msg.toUserId : msg.fromUserId
|
|
||||||
if (!messages.value[targetUserId]) messages.value[targetUserId] = []
|
// 确定消息放在哪个联系人下
|
||||||
const list = messages.value[targetUserId]
|
// 自己发的消息放到接收方(toUserId)下,收到的消息放到发送方(fromUserId)下
|
||||||
|
const contactId = isSelf ? msgToUserId : msgFromUserId
|
||||||
|
|
||||||
|
if (!messages.value[contactId]) messages.value[contactId] = []
|
||||||
|
const list = messages.value[contactId]
|
||||||
|
|
||||||
// 清除同类型的临时消息(sending 状态的)
|
// 清除同类型的临时消息(sending 状态的)
|
||||||
if (isSelf && (msg.type === 'image' || msg.type === 'file')) {
|
if (isSelf && (msg.type === 'image' || msg.type === 'file')) {
|
||||||
for (let i = list.length - 1; i >= 0; i--) {
|
for (let i = list.length - 1; i >= 0; i--) {
|
||||||
@@ -598,13 +622,17 @@ const onDialogOpen = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 去重
|
|
||||||
const exists = list.some(m => m.id === msg.id)
|
// 去重(根据消息 id 或临时消息的 tempType)
|
||||||
|
const msgKey = msg.tempType ? `temp_${msg.id}` : msg.id
|
||||||
|
const exists = list.some(m => (m.tempType ? `temp_${m.id}` : m.id) === msgKey)
|
||||||
if (!exists) list.push(msg)
|
if (!exists) list.push(msg)
|
||||||
|
|
||||||
// 滚动
|
// 滚动
|
||||||
if (currentContact.value && Number(currentContact.value.id) === Number(targetUserId)) {
|
if (currentContact.value && Number(currentContact.value.id) === contactId) {
|
||||||
nextTick(() => scrollToBottom())
|
nextTick(() => scrollToBottom())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新最近聊天
|
// 更新最近聊天
|
||||||
if (!isSelf) {
|
if (!isSelf) {
|
||||||
const contact = contacts.value.find(c => Number(c.id) === Number(msg.fromUserId))
|
const contact = contacts.value.find(c => Number(c.id) === Number(msg.fromUserId))
|
||||||
|
|||||||
Reference in New Issue
Block a user