updateSession和msgUpdateSessionService绑定到一起,保证数据同步一致性

This commit is contained in:
bob
2024-09-18 22:50:20 +08:00
parent 91a19240e6
commit fc83d6daab
3 changed files with 30 additions and 13 deletions

View File

@@ -3,7 +3,6 @@ import '@wangeditor/editor/dist/css/style.css'
import { onMounted, onBeforeUnmount, ref, shallowRef, watch } from 'vue'
import { Editor } from '@wangeditor/editor-for-vue'
import { messageStore } from '@/stores'
import { msgUpdateSessionService } from '@/api/message'
const mode = 'simple'
// 编辑器实例,必须用 shallowRef
@@ -58,9 +57,10 @@ watch(
valueHtml.value = newValue.draft || ''
// 草稿若没发生变动,则不触发存储
if (editorRef.value.getText().trim() !== oldValue.draft) {
oldValue.draft = editorRef.value.getText().trim()
messageData.updateSession(oldValue)
msgUpdateSessionService({ sessionId: oldValue.sessionId, draft: oldValue.draft })
messageData.updateSession({
...oldValue,
draft: editorRef.value.getText().trim()
})
}
},
{ deep: true }

View File

@@ -1,5 +1,6 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { msgUpdateSessionService } from '@/api/message'
// 消息功能相关需要缓存的数据
export const messageStore = defineStore(
@@ -26,6 +27,15 @@ export const messageStore = defineStore(
[session.sessionId]: session
}
}
msgUpdateSessionService({
sessionId: session.sessionId,
readMsgId: session.readMsgId,
readTime: session.readTime,
top: session.top,
muted: session.muted,
draft: session.draft
})
}
return {

View File

@@ -23,7 +23,7 @@ import InputEditor from '@/components/message/InputEditor.vue'
import MessageItem from '@/components/message/MessageItem.vue'
import { userStore, settingStore, messageStore } from '@/stores'
import backgroupImage from '@/assets/messagebx_bg.webp'
import { msgChatSessionListService, msgUpdateSessionService, msgChatPullMsgService } from '@/api/message'
import { msgChatSessionListService, msgChatPullMsgService } from '@/api/message'
import { MsgType } from '@/proto/msg'
import wsConnect from '@/js/websocket/wsConnect'
@@ -36,8 +36,8 @@ const asideWidthMin = 200
const asideWidthMax = 500
const inputBoxHeight = ref(0)
const inputBoxHeightMin = 150
const inputBoxHeightMax = 400
const inputBoxHeightMin = 350
const inputBoxHeightMax = 500
const sessionList = ref({})
const choosedSessionId = ref()
@@ -55,7 +55,7 @@ const msgListDiv = ref()
onMounted(async () => {
asideWidth.value = settingData.sessionListDrag[userData.user.account] || 200
inputBoxHeight.value = settingData.inputBoxDrag[userData.user.account] || 200
inputBoxHeight.value = settingData.inputBoxDrag[userData.user.account] || 350
const res = await msgChatSessionListService()
messageData.setSessionList(res.data.data) //入缓存
@@ -131,13 +131,20 @@ const handleIsChoosed = (session) => {
}
const handleSwitchTag = (obj) => {
msgUpdateSessionService(obj)
messageData.updateSession({
...choosedSession,
...obj
})
}
// 发送事件要做的事情
const handleExportContent = (content) => {
// TODO 这里还要考虑失败情况1消息发不出去2消息发出去了服务器不发“已发送”
wsConnect.sendMsg(showId.value, choosedSession.value.sessionType, content, (deliveredMsg) => {
// 更新到messageStore中的sessionList已读已读时间
// 更新到数据库session表中发送端
// 如果当前sessionid和这个“已发送”消息的sessionId更新到msgRecords中
messageData.addRecord(choosedSessionId.value, {
msgId: deliveredMsg.body.msgId,
fromId: userData.user.account,
@@ -154,8 +161,7 @@ const onLoadMore = () => {
}
// watch到哪个表示哪个会话被选中
watch(choosedSessionId, (newValue, oldValue) => {
console.log('====>111: ', newValue, oldValue)
watch(choosedSessionId, (newValue) => {
messageData.setLastSessionId(newValue)
choosedSession.value = sessionList.value[newValue]
@@ -174,7 +180,8 @@ watch(choosedSessionId, (newValue, oldValue) => {
choosedSession.value.readTime = now
choosedSession.value.unreadCount = 0;
msgUpdateSessionService({
messageData.updateSession({
...choosedSession,
sessionId: choosedSessionId.value,
readMsgId: res.data.data.lastMsgId,
readTime: now })