Some checks failed
Check / lint (push) Has been cancelled
Check / typecheck (push) Has been cancelled
Check / build (build, 18.x, ubuntu-latest) (push) Has been cancelled
Check / build (build, 18.x, windows-latest) (push) Has been cancelled
Check / build (build:app, 18.x, ubuntu-latest) (push) Has been cancelled
Check / build (build:app, 18.x, windows-latest) (push) Has been cancelled
Check / build (build:mp-weixin, 18.x, ubuntu-latest) (push) Has been cancelled
Check / build (build:mp-weixin, 18.x, windows-latest) (push) Has been cancelled
127 lines
3.8 KiB
JavaScript
127 lines
3.8 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { useDialogueStore } from '@/store'
|
|
import lodash from 'lodash'
|
|
|
|
import {ref} from 'vue'
|
|
import {createGlobalState,useStorage} from '@vueuse/core'
|
|
import {uniStorage} from "@/utils/uniStorage.js"
|
|
|
|
export const useDialogueListStore = createGlobalState(() => {
|
|
const dialogueList = useStorage('dialogueList', [], uniStorage)
|
|
const zpagingRef = ref()
|
|
const virtualList = ref([])
|
|
|
|
const getDialogueList=(indexName)=>{
|
|
return dialogueList.value.find(item=>item.index_name===indexName)
|
|
}
|
|
|
|
const addDialogueRecord=(newRecords,type='add')=>{
|
|
console.log(newRecords);
|
|
|
|
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)
|
|
}
|
|
}
|
|
})
|
|
|
|
// 更新除 records 和 index_name 外的其他属性
|
|
const { index_name, records: _, ...updateProps } = dialogue
|
|
dialogueList.value[existingIndex] = {
|
|
...dialogueList.value[existingIndex],
|
|
...updateProps
|
|
}
|
|
}
|
|
}
|
|
|
|
const updateDialogueRecord=(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[recordIndex] = {
|
|
...item.records[recordIndex],
|
|
...record
|
|
}
|
|
}
|
|
const virtualIndex = virtualList.value.findIndex(item=>item.msg_id===record.msg_id)
|
|
if(virtualIndex!==-1){
|
|
virtualList.value[virtualIndex] = {
|
|
...virtualList.value[virtualIndex],
|
|
...record
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
const updateUploadProgress=(id,progress)=>{
|
|
const record = virtualList.value.find(item=>item.msg_id===id)
|
|
if(record){
|
|
record.uploadCurrent = progress
|
|
}
|
|
}
|
|
|
|
const updateZpagingRef=(params)=>{
|
|
zpagingRef.value=params
|
|
}
|
|
|
|
const zpagingComplete=(index_name)=>{
|
|
const item = getDialogueList(index_name)
|
|
zpagingRef.value?.complete(lodash.cloneDeep(item.records).reverse())
|
|
}
|
|
|
|
const addChatRecord = (indexName,item)=>{
|
|
const dialogue = lodash.cloneDeep(useDialogueStore())
|
|
if (dialogue?.index_name === indexName) {
|
|
zpagingRef.value?.addChatRecordData(item,false,false)
|
|
}
|
|
}
|
|
|
|
const batchDelDialogueRecord=(msgIds)=>{
|
|
const dialogue = lodash.cloneDeep(useDialogueStore())
|
|
const item = getDialogueList(dialogue.index_name)
|
|
item.records = item.records.filter(item=>!msgIds.includes(item.msg_id))
|
|
}
|
|
|
|
return {
|
|
dialogueList,
|
|
zpagingRef,
|
|
getDialogueList,
|
|
addDialogueRecord,
|
|
updateDialogueRecord,
|
|
deleteDialogueRecord,
|
|
updateUploadProgress,
|
|
updateZpagingRef,
|
|
zpagingComplete,
|
|
addChatRecord,
|
|
virtualList,
|
|
batchDelDialogueRecord,
|
|
}
|
|
})
|