增加对两个视角已读未读消息的处理,并调整触发时机;增加map维护已读数量

This commit is contained in:
wangyifeng 2025-04-17 17:05:42 +08:00
parent 676ad46ab5
commit 29dcfac775

View File

@ -678,6 +678,37 @@ const state = ref({
tempWaitDoCheck: [], //
})
// Map
const recordReadsMap = ref(new Map())
// Map UI
watch(
recordReadsMap,
(newMap) => {
console.error(newMap, 'newMap')
requestAnimationFrame(() => {
console.error('requestAnimationFrame')
newMap.forEach((readNum, msgId) => {
console.error(readNum, 'readNum')
console.error(msgId, 'msgId')
const element = document.getElementById(`zp-id-${msgId}`)
if (element) {
console.error(element, 'element')
element.dataset.readNum = readNum
// const readNumElement = element.querySelector('.read-num')
// if (readNumElement) {
// console.error(readNumElement, 'readNumElement')
// readNumElement.textContent = readNum
// }
}
})
})
},
{
deep: true
}
)
uniOnload(async (options) => {
console.log('onLoad' + JSON.stringify(options))
if (options.sessionId) {
@ -1376,6 +1407,39 @@ watch(
state.value.localPageLoadDone = false
}
}
//
if (oldValue && oldValue.length > 0) {
const lastOldMsgId = oldValue[0].msg_id
const lastIndex = newValue.findIndex(
(msg) => msg.msg_id === lastOldMsgId,
)
console.error(lastIndex, 'lastIndex')
if (lastIndex === -1) {
// lastOldMsgId
newValue.forEach((msg) => {
nextTick(() => {
const element = document.getElementById(`zp-id-${msg.msg_id}`)
if (element) {
messageRecordElementRefs.value.unshift(element)
}
})
})
} else if (lastIndex > 0) {
// lastOldMsgId
for (let i = 0; i < lastIndex; i++) {
const msg = newValue[i]
console.error(msg, '新消息msg')
nextTick(() => {
const element = document.getElementById(`zp-id-${msg.msg_id}`)
if (element) {
messageRecordElementRefs.value.unshift(element)
}
})
}
}
}
}
},
{
@ -1383,6 +1447,7 @@ watch(
},
)
const onScrollToLower = async () => {
if (state.value.useCustomLoadMore) {
const tempVirtualList = lodash.cloneDeep(virtualList.value).reverse()
@ -1927,8 +1992,8 @@ const checkVisibleElements = () => {
state.value.visibleElements.forEach((el) => {
// console.log(el)
const msgId = el.dataset.msgid
const talkType = el.dataset.talktype
const receiverId = el.dataset.receiverid
const talkType = Number(el.dataset.talktype)
const receiverId = Number(el.dataset.receiverid)
// console.log(msgId, talkType, receiverId)
if (waitDoRead.length === 0) {
waitDoRead.push({
@ -1937,20 +2002,19 @@ const checkVisibleElements = () => {
receiver_id: receiverId,
})
} else {
waitDoRead.forEach((doReadItem) => {
if (
doReadItem.talk_type === talkType &&
doReadItem.receiver_id === receiverId
) {
doReadItem.msg_ids.push(msgId)
} else {
waitDoRead.push({
msg_ids: [msgId],
talk_type: talkType,
receiver_id: receiverId,
})
}
})
const existingItem = waitDoRead.find(
(item) =>
item.talk_type === talkType && item.receiver_id === receiverId,
)
if (existingItem) {
existingItem.msg_ids.push(msgId)
} else {
waitDoRead.push({
msg_ids: [msgId],
talk_type: talkType,
receiver_id: receiverId,
})
}
}
})
// console.error(waitDoRead)
@ -1962,10 +2026,10 @@ const checkVisibleElements = () => {
prev.talk_type === doReadItem.talk_type &&
prev.receiver_id === doReadItem.receiver_id,
)
// msg_ids
// msg_ids
if (
!prevItem ||
prevItem.msg_ids.length !== doReadItem.msg_ids.length
!doReadItem.msg_ids.every((id) => prevItem.msg_ids.includes(id))
) {
console.error('====发送了已读回执=====', doReadItem)
//
@ -1986,8 +2050,8 @@ const checkVisibleOutElements = () => {
state.value.visibleOutElements.forEach((el) => {
// console.log(el)
const msgId = el.dataset.msgid
const talkType = el.dataset.talktype
const receiverId = el.dataset.receiverid
const talkType = Number(el.dataset.talktype)
const receiverId = Number(el.dataset.receiverid)
if (waitDoCheck.length === 0) {
waitDoCheck.push({
msg_ids: [msgId],
@ -1995,46 +2059,66 @@ const checkVisibleOutElements = () => {
receiver_id: receiverId,
})
} else {
waitDoCheck.forEach((doReadItem) => {
if (
doReadItem.talk_type === talkType &&
doReadItem.receiver_id === receiverId
) {
doReadItem.msg_ids.push(msgId)
} else {
waitDoCheck.push({
msg_ids: [msgId],
talk_type: talkType,
receiver_id: receiverId,
})
}
})
const existingItem = waitDoCheck.find(
(item) =>
item.talk_type === talkType && item.receiver_id === receiverId,
)
if (existingItem) {
existingItem.msg_ids.push(msgId)
} else {
waitDoCheck.push({
msg_ids: [msgId],
talk_type: talkType,
receiver_id: receiverId,
})
}
}
})
// console.error(waitDoCheck)
if (waitDoCheck.length > 0) {
waitDoCheck.forEach((doCheckItem) => {
//
const prevItem = state.value.tempWaitDoCheck.find(
(prev) =>
prev.talk_type === doCheckItem.talk_type &&
prev.receiver_id === doCheckItem.receiver_id,
)
// msg_ids
if (
!prevItem ||
prevItem.msg_ids.length !== doCheckItem.msg_ids.length
) {
// const prevItem = state.value.tempWaitDoCheck.find(
// (prev) =>
// prev.talk_type === doCheckItem.talk_type &&
// prev.receiver_id === doCheckItem.receiver_id,
// )
// // msg_ids
// if (
// !prevItem ||
// !doCheckItem.msg_ids.every((id) => prevItem.msg_ids.includes(id))
// ) {
console.error('====调用了已读回执查询接口=====', doCheckItem)
// ServeReadConditionList({
// type: 'list', //listdetail
// talkType: talkParams.type,
// receiverId: talkParams.receiver_id,
// msgIds: waitDoCheck,
// }).then(({ code, data }) => {
// console.log(data)
// })
}
let params = Object.assign({}, doCheckItem, {
talkType: doCheckItem.talk_type, //12
receiverId: doCheckItem.talk_type === 1 ? talkParams.uid : talkParams.receiver_id, //idid
msgIds: doCheckItem.msg_ids,
type: 'list', //listdetail
})
const resp = ServeReadConditionList(params)
// console.log(resp)
resp.then(({ code, data }) => {
// console.log(data)
if (code == 200) {
//
if (Array.isArray(data.data)) {
console.error('处理批量更新', data.data)
data.data.forEach(item => {
if (item.msgId && item.readNum !== undefined) {
console.error('修改recordReadsMap', item)
recordReadsMap.value.set(item.msgId, item.readNum)
}
})
} else if (data.data && data.data.readNum !== undefined) {
console.error('处理单个更新', data.data)
doCheckItem.msg_ids.forEach(msgId => {
recordReadsMap.value.set(msgId, data.data.readNum)
})
}
}
}).catch(() => {})
// }
})
}
//
@ -2056,7 +2140,8 @@ const observeElement = (el, index) => {
//
const handleIntersection = (entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
if (entry.isIntersecting && entry.intersectionRatio >= 0.5) {
//
//
let elData = entry.target.dataset
const msgType = elData.msgtype
@ -2069,6 +2154,10 @@ const handleIntersection = (entries) => {
//
state.value.visibleOutElements.add(entry.target)
}
} else {
//
state.value.visibleElements.delete(entry.target)
state.value.visibleOutElements.delete(entry.target)
}
})
}
@ -2571,4 +2660,26 @@ const onTextAreaMention = (user) => {
:deep(.mention) {
color: #1890ff;
}
//
[data-msgid] {
position: relative;
&::after {
content: attr(data-read-num);
position: absolute;
right: 10px;
bottom: 5px;
font-size: 12px;
color: #999;
background: rgba(0, 0, 0, 0.1);
padding: 2px 6px;
border-radius: 10px;
display: none;
}
&[data-read-num]:not([data-read-num=""])::after {
display: block;
}
}
</style>