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

100 lines
3.0 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'
2024-12-10 03:31:36 +00:00
import {ref} from 'vue'
import {createGlobalState,useStorage} from '@vueuse/core'
import {uniStorage} from "@/utils/uniStorage.js"
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
export const useDialogueListStore = createGlobalState(() => {
const dialogueList = useStorage('dialogueList', [], uniStorage)
const zpagingRef = ref()
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const getDialogueList=(indexName)=>{
return dialogueList.value.find(item=>item.index_name===indexName)
}
const addDialogueRecord=(newRecords,type='add')=>{
console.log(newRecords);
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const dialogue = lodash.cloneDeep(useDialogueStore())
if (!dialogue || typeof dialogue !== 'object') return
// 检查是否已存在相同 index_name 的对话
const existingIndex = dialogueList.value.findIndex(item => item.index_name === dialogue.index_name)
if (existingIndex === -1) {
// 如果不存在,直接添加
dialogueList.value.push(dialogue)
} else {
// 如果对话存在,处理 records 数组
const { records = [] } = dialogue
newRecords.forEach(newRecord => {
const recordIndex = dialogueList.value[existingIndex].records.findIndex(
record => record.msg_id === newRecord.msg_id
)
if (recordIndex === -1) {
// 如果记录不存在,添加到 records 数组
if(type==='add'){
dialogueList.value[existingIndex].records.push(newRecord)
}else{
dialogueList.value[existingIndex].records.unshift(newRecord)
}
2024-12-06 08:55:15 +00:00
}
2024-12-10 03:31:36 +00:00
})
// 更新除 records 和 index_name 外的其他属性
const { index_name, records: _, ...updateProps } = dialogue
dialogueList.value[existingIndex] = {
...dialogueList.value[existingIndex],
...updateProps
2024-12-06 08:55:15 +00:00
}
2024-12-10 03:31:36 +00:00
}
}
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const deleteDialogueRecord=(record)=>{
const dialogue = lodash.cloneDeep(useDialogueStore())
const item = getDialogueList(dialogue.index_name)
const recordIndex = item.records.findIndex(item=>item.msg_id===record.msg_id)
if(recordIndex!==-1){
item.records.splice(recordIndex,1)
}
}
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const updateUploadProgress=(id,progress)=>{
const dialogue = lodash.cloneDeep(useDialogueStore())
const item = getDialogueList(dialogue.index_name)
const record = item.records.find(item=>item.msg_id===id)
if(record){
record.uploadCurrent = progress
}
}
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const updateZpagingRef=(params)=>{
zpagingRef.value=params
}
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const zpagingComplete=(index_name)=>{
const item = getDialogueList(index_name)
zpagingRef.value?.complete(item.records.toReversed())
}
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
const addChatRecord = (indexName,item)=>{
const dialogue = lodash.cloneDeep(useDialogueStore())
if (dialogue?.index_name === indexName) {
zpagingRef.value?.addChatRecordData(item.records)
}
}
2024-12-06 08:55:15 +00:00
2024-12-10 03:31:36 +00:00
return {
dialogueList,
zpagingRef,
getDialogueList,
addDialogueRecord,
deleteDialogueRecord,
updateUploadProgress,
updateZpagingRef,
zpagingComplete,
addChatRecord,
2024-12-06 08:55:15 +00:00
}
})