云文件管理系统上传组件优化

This commit is contained in:
2026-04-02 18:04:01 +08:00
parent ef251130a2
commit de7af347f6

View File

@@ -429,9 +429,23 @@ const triggerImageUpload = () => { if (!uploading.value) imageInputRef.value?.cl
const triggerFileUpload = () => { if (!uploading.value) fileInputRef.value?.click() }
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] = []
messages.value[contactId].push(msg)
// 使用唯一 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)
}
}
const handleImageSelect = async (e) => {
@@ -581,14 +595,24 @@ const onDialogOpen = () => {
unsubscribeWs = chatService.onMessage((data) => {
if (data.type === 'chat') {
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.sending = false
msg.failed = false
msg.fromColor = colors[Math.abs(Number(msg.fromUserId) % colors.length)]
const targetUserId = isSelf ? msg.toUserId : msg.fromUserId
if (!messages.value[targetUserId]) messages.value[targetUserId] = []
const list = messages.value[targetUserId]
msg.fromColor = colors[Math.abs(msgFromUserId % colors.length)]
// 确定消息放在哪个联系人下
// 自己发的消息放到接收方toUserId收到的消息放到发送方fromUserId)下
const contactId = isSelf ? msgToUserId : msgFromUserId
if (!messages.value[contactId]) messages.value[contactId] = []
const list = messages.value[contactId]
// 清除同类型的临时消息sending 状态的)
if (isSelf && (msg.type === 'image' || msg.type === 'file')) {
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 (currentContact.value && Number(currentContact.value.id) === Number(targetUserId)) {
if (currentContact.value && Number(currentContact.value.id) === contactId) {
nextTick(() => scrollToBottom())
}
// 更新最近聊天
if (!isSelf) {
const contact = contacts.value.find(c => Number(c.id) === Number(msg.fromUserId))