@@ -132,17 +136,40 @@ const sendParams = ref
({
toCompanyName: '',
fromCompanyName: '',
})
+const messageListRef = ref(null)
+const lastMessageId = ref('')
+watch(messageDetails, async (newVal, oldVal) => {
+ await nextTick()
+ // 判断是否有新消息(最后一条消息id变化)
+ const lastMsg = newVal?.[newVal.length - 1]
+ if (lastMsg && lastMsg.uuid !== lastMessageId.value) {
+ lastMessageId.value = lastMsg.uuid
+ if (messageListRef.value) {
+ messageListRef.value.scrollTop = messageListRef.value.scrollHeight
+ }
+ }
+})
// 消息列表
const getMessageListData = async () => {
const res: any = await getMessageListApi()
const data = res.data || []
- const uniqueMessages = [
- ...new Map([...data, ...messageListAll.value].map((item) => [item.uuid, item])).values(),
- ]
+ // 用 uuid 去重:先过滤掉 data 中已存在于 messageListAll 的项
+ const existingUUIDs = new Set(messageListAll.value.map((msg: any) => msg.uuid))
- messageList.value = uniqueMessages
+ const newData = data.filter((item: any) => !existingUUIDs.has(item.uuid))
+
+ // 合并新数据
+ const combined = [...newData, ...messageListAll.value]
+
+ // 再去重(为了安全起见)
+ const map = new Map()
+ combined.forEach((item) => {
+ map.set(item.uuid, item)
+ })
+
+ messageList.value = Array.from(map.values())
}
const getMessageListAllData = async () => {
@@ -188,16 +215,13 @@ const onClickMessage = async (item: any) => {
fromCompany: queryDetailsCompanyId.value,
})
- // console.log(res, '已读结果--')
-
- getMessageInfoData(queryDetailsCompanyId.value)
-
- // console.log(res, '消息已读')
if (!messageInterval.value) {
- messageInterval.value = setInterval(() => {
- getMessageInfoData(queryDetailsCompanyId.value)
- // getMessageHistoryData(queryDetailsCompanyId.value)
- }, 5000)
+ messageInterval.value = setInterval(async () => {
+ await getMessageInfoData(queryDetailsCompanyId.value)
+ }, 2000)
+ }
+ {
+ await getMessageInfoData(queryDetailsCompanyId.value)
}
}
// 点击发送
@@ -206,7 +230,6 @@ const onSendMessage = debounce(async () => {
const res: any = await sendMessageApi(sendParams.value)
if (res.code == 200) {
sendParams.value.messageContent = ''
-
const result: any = await messageIsReadApi({
toCompany: myCompanyId.value,
fromCompany: queryDetailsCompanyId.value,
@@ -234,7 +257,7 @@ onMounted(() => {
}
messageInterval.value = setInterval(() => {
getMessageInfoData(queryDetailsCompanyId.value)
- }, 5000)
+ }, 2000)
}
myCompanyId.value = store.userInfo.companyId
@@ -320,7 +343,7 @@ onUnmounted(() => {
.message-info-list {
flex: 1;
- // max-height: 500px;
+ max-height: 700px;
overflow-y: auto !important;
}