chat-app/src/store/modules/dialogueList.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-12-06 08:55:15 +00:00
import { defineStore } from 'pinia'
import { useDialogueStore } from '@/store'
import lodash from 'lodash'
export const useDialogueListStore = defineStore('dialogueList', {
persist: true,
state: () => {
return {
dialogueList:[]
}
},
getters: {
getDialogueList:(state)=>{
return (indexName) => state.dialogueList.find(item=>item.index_name===indexName)
}
},
actions: {
// 添加对话
addDialogueRecord(newRecords,type='add') {
console.log(newRecords);
const dialogue = lodash.cloneDeep(useDialogueStore())
if (!dialogue || typeof dialogue !== 'object') return
// 检查是否已存在相同 index_name 的对话
const existingIndex = this.$state.dialogueList.findIndex(item => item.index_name === dialogue.index_name)
if (existingIndex === -1) {
// 如果不存在,直接添加
this.$state.dialogueList.push(dialogue)
} else {
// 如果对话存在,处理 records 数组
const { records = [] } = dialogue
newRecords.forEach(newRecord => {
const recordIndex = this.$state.dialogueList[existingIndex].records.findIndex(
record => record.msg_id === newRecord.msg_id
)
if (recordIndex === -1) {
// 如果记录不存在,添加到 records 数组
if(type==='add'){
this.$state.dialogueList[existingIndex].records.push(newRecord)
}else{
this.$state.dialogueList[existingIndex].records.unshift(newRecord)
}
}
})
// 更新除 records 和 index_name 外的其他属性
const { index_name, records: _, ...updateProps } = dialogue
this.$state.dialogueList[existingIndex] = {
...this.$state.dialogueList[existingIndex],
...updateProps
}
}
},
// // 更新上传图片进度
// updateUploadProgress(msgId,progress){
// console.log('updateUploadProgress');
// const dialogue = lodash.cloneDeep(useDialogueStore())
// const item = this.getDialogueList(dialogue.index_name)
// const record = item.records.find(item=>item.msg_id===msgId)
// if(record){
// record.uploadCurrent = progress
// console.log(record.uploadCurrent);
// }
// }
// 自增好友键盘输入事件
// triggerKeyboard() {
// this.keyboard = true
// clearTimeout(keyboardTimeout)
// keyboardTimeout = setTimeout(() => (this.keyboard = false), 2000)
// },
}
})