修改:发送和接收优化

This commit is contained in:
安浩浩 2024-06-02 17:09:08 +08:00
parent 294fb80b05
commit b8466fc7e7
4 changed files with 41 additions and 41 deletions

View File

@ -3,10 +3,11 @@ import { Dialog } from '@/components/Dialog'
import { shallowRef, defineAsyncComponent, DefineComponent } from 'vue' import { shallowRef, defineAsyncComponent, DefineComponent } from 'vue'
import { getAccessToken } from '@/utils/auth' import { getAccessToken } from '@/utils/auth'
import { useWebSocket } from '@vueuse/core' import { useWebSocket } from '@vueuse/core'
import { imMessageStore, ImMessageWebSocket } from '@/store/modules/imMessageStore' import { chatStore, ImMessageWebSocket } from '@/store/modules/chatStore'
import * as MessageApi from '@/api/im/message'
// imMessageStore addMessage // imMessageStore addMessage
const { addMessage } = imMessageStore() const { addMessage, setLocalMaxSeq, loadMessages } = chatStore()
// //
const IMComponent = defineAsyncComponent(() => import('@/views/im/index.vue')) const IMComponent = defineAsyncComponent(() => import('@/views/im/index.vue'))
@ -64,6 +65,7 @@ function initWebSocket() {
// pina imMessageStore // pina imMessageStore
addMessage(message) addMessage(message)
setLocalMaxSeq(content.sequence)
return return
} }
@ -75,10 +77,23 @@ function initWebSocket() {
console.log('websocket 初始化成功') console.log('websocket 初始化成功')
} }
//
const pullParams = reactive({
sequence: 0, //
size: 10
})
async function loadImMessages() {
//
const list: ImMessageWebSocket[] = await MessageApi.pullMessageList(pullParams)
loadMessages(list)
}
// ========== ========= // ========== =========
onMounted(() => { onMounted(() => {
// websocket // websocket
initWebSocket() initWebSocket()
// loadMessages
loadImMessages()
}) })
</script> </script>

View File

@ -1,3 +1,4 @@
import { store } from '../index'
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
export interface ImMessageWebSocket { export interface ImMessageWebSocket {
@ -13,23 +14,33 @@ export interface ImMessageWebSocket {
sequence: number // 序号 sequence: number // 序号
} }
export const imMessageStore = defineStore({ export const chatStore = defineStore({
id: 'imMessage', id: 'imMessage',
state: () => ({ state: () => ({
messages: [] as ImMessageWebSocket[] messages: [] as ImMessageWebSocket[],
localMaxSeq: 0
}), }),
getters: { getters: {
getMessages(): ImMessageWebSocket[] { getMessages(): ImMessageWebSocket[] {
return this.messages return this.messages
},
getLocalMaxSeq(): number {
return this.localMaxSeq
} }
}, },
actions: { actions: {
loadMessages(messages: ImMessageWebSocket[]) {
this.messages = messages
},
addMessage(message: ImMessageWebSocket) { addMessage(message: ImMessageWebSocket) {
this.messages.push(message) this.messages.push(message)
},
setLocalMaxSeq(seq: number) {
this.localMaxSeq = seq
} }
} }
}) })
export function useImMessageStore() { export const useImMessageStore = () => {
return imMessageStore() return chatStore(store)
} }

View File

@ -38,7 +38,7 @@ const handleMsgTimeShow = (sendTime, index) => {
} }
/* computed-- 消息来源是否为自己 */ /* computed-- 消息来源是否为自己 */
const isMyself = (msgBody) => { const isMyself = (msgBody) => {
return msgBody.senderId === userStore.user.id || msgBody.receiverId === userStore.user.id return msgBody.senderId === userStore.user.id
} }
/* 文本中是否包含link */ /* 文本中是否包含link */
const isLink = computed(() => { const isLink = computed(() => {
@ -52,9 +52,7 @@ const loginUserInfo = {
} }
// //
const handleNickName = (msgBody) => { const handleNickName = (msgBody) => {
return msgBody.senderId === userStore.user.id || msgBody.receiverId === userStore.user.id return msgBody.senderId === userStore.user.id ? '我' : '对方'
? '我'
: '对方'
} }
// //
let clickQuoteMsgId = ref('') let clickQuoteMsgId = ref('')

View File

@ -5,7 +5,10 @@ import { messageType } from '@/constant/im'
import MessageList from './components/messageList/index.vue' import MessageList from './components/messageList/index.vue'
import InputBox from './components/inputBox/index.vue' import InputBox from './components/inputBox/index.vue'
import * as MessageApi from '@/api/im/message' import * as MessageApi from '@/api/im/message'
import { imMessageStore } from '@/store/modules/imMessageStore' import { chatStore, useImMessageStore } from '@/store/modules/chatStore'
const { addMessage, setLocalMaxSeq } = chatStore()
const { messages } = useImMessageStore()
const { query } = useRoute() // const { query } = useRoute() //
@ -15,15 +18,6 @@ const drawer = ref(false) //抽屉显隐
const handleDrawer = () => { const handleDrawer = () => {
drawer.value = !drawer.value drawer.value = !drawer.value
} }
//
const delTheFriend = () => {
console.log(nowPickInfo.value)
if (nowPickInfo.value?.id) {
const targetId = nowPickInfo.value.id
// EaseChatClient.deleteContact(targetId)
// ElMessage({ type: 'success', center: true, message: '~' })
}
}
// //
const nowPickInfo = ref({ const nowPickInfo = ref({
id: 100, id: 100,
@ -39,24 +33,15 @@ const nowPickInfo = ref({
} }
}) })
//
const groupDetail = computed(() => {
return nowPickInfo.value.groupDetail
})
// //
const pullParams = reactive({ const pullParams = reactive({
sequence: null, // null sequence: 0, //
size: 10 size: 10
}) })
//
const messageData = ref([]) const messageData = ref([])
const getMessageData = async () => { const getMessageData = async () => {
// messageData.value = messages
const imMessages = imMessageStore().getMessages
if (imMessages) {
console.log('imMessages:' + imMessages)
console.log('imMessages:' + imMessages.length)
}
messageData.value = await MessageApi.pullMessageList(pullParams)
} }
console.log(messageData) console.log(messageData)
@ -69,10 +54,6 @@ const fechHistoryMessage = (loadType) => {
console.log(loadType) console.log(loadType)
console.log('加载更多') console.log('加载更多')
loadingHistoryMsg.value = true loadingHistoryMsg.value = true
// messageData sequence
if (messageData.value.length) {
pullParams.sequence = messageData.value[0].sequence
}
getMessageData() getMessageData()
loadingHistoryMsg.value = false loadingHistoryMsg.value = false
} }
@ -80,11 +61,6 @@ const fechHistoryMessage = (loadType) => {
const scrollMessageList = (direction) => { const scrollMessageList = (direction) => {
console.log(direction) console.log(direction)
} }
//
const inputBox = ref(null)
const reEditMessage = (msg) => (inputBox.value.textContent = msg)
//
const messageQuote = (msg) => inputBox.value.handleQuoteMessage(msg)
/** 初始化 **/ /** 初始化 **/
onMounted(() => { onMounted(() => {
getMessageData() getMessageData()