修改:发送和接收优化

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 { getAccessToken } from '@/utils/auth'
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
const { addMessage } = imMessageStore()
const { addMessage, setLocalMaxSeq, loadMessages } = chatStore()
//
const IMComponent = defineAsyncComponent(() => import('@/views/im/index.vue'))
@ -64,6 +65,7 @@ function initWebSocket() {
// pina imMessageStore
addMessage(message)
setLocalMaxSeq(content.sequence)
return
}
@ -75,10 +77,23 @@ function initWebSocket() {
console.log('websocket 初始化成功')
}
//
const pullParams = reactive({
sequence: 0, //
size: 10
})
async function loadImMessages() {
//
const list: ImMessageWebSocket[] = await MessageApi.pullMessageList(pullParams)
loadMessages(list)
}
// ========== =========
onMounted(() => {
// websocket
initWebSocket()
// loadMessages
loadImMessages()
})
</script>

View File

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

View File

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

View File

@ -5,7 +5,10 @@ import { messageType } from '@/constant/im'
import MessageList from './components/messageList/index.vue'
import InputBox from './components/inputBox/index.vue'
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() //
@ -15,15 +18,6 @@ const drawer = ref(false) //抽屉显隐
const handleDrawer = () => {
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({
id: 100,
@ -39,24 +33,15 @@ const nowPickInfo = ref({
}
})
//
const groupDetail = computed(() => {
return nowPickInfo.value.groupDetail
})
//
const pullParams = reactive({
sequence: null, // null
sequence: 0, //
size: 10
})
//
const messageData = ref([])
const getMessageData = async () => {
//
const imMessages = imMessageStore().getMessages
if (imMessages) {
console.log('imMessages:' + imMessages)
console.log('imMessages:' + imMessages.length)
}
messageData.value = await MessageApi.pullMessageList(pullParams)
messageData.value = messages
}
console.log(messageData)
@ -69,10 +54,6 @@ const fechHistoryMessage = (loadType) => {
console.log(loadType)
console.log('加载更多')
loadingHistoryMsg.value = true
// messageData sequence
if (messageData.value.length) {
pullParams.sequence = messageData.value[0].sequence
}
getMessageData()
loadingHistoryMsg.value = false
}
@ -80,11 +61,6 @@ const fechHistoryMessage = (loadType) => {
const scrollMessageList = (direction) => {
console.log(direction)
}
//
const inputBox = ref(null)
const reEditMessage = (msg) => (inputBox.value.textContent = msg)
//
const messageQuote = (msg) => inputBox.value.handleQuoteMessage(msg)
/** 初始化 **/
onMounted(() => {
getMessageData()