import { defineStore } from 'pinia' import { listDictDataAll, listDictProblemSourceTree } from '@/api/system/dict' import { listDictContentTree } from '@/api/system/dictContent' import { departTree } from '@/api/system/depart' import { listFlowNode } from '@/api/work/flowNode' export interface CatchState { dict: Record, dictContent: any[], dictProblemSources: any[], departs: any[], flowNodes: any[] } // 判定是否已请求缓存 let departCatchFlag = true; const dictCatchFlag = {}; let dictProblemSourcesFlag = true; const useCatchStore = defineStore({ id: 'catch', state: ():CatchState => ({ dict: {}, dictContent: [], dictProblemSources: [], departs: [], flowNodes: [] }), getters: {}, actions: { setDict(key:string, list:any[]) { this.dict[key] = list }, getDicts(keys:string[]) { keys.forEach(key => { this.getDict(key); }) return this.dict; }, getDict(key:string) { if (!this.dict[key] && !dictCatchFlag[key]) { dictCatchFlag[key] = true const refresh = async () => { const res = await Promise.allSettled>([listDictDataAll(key)]) res.forEach((item) => { if (item.status == 'fulfilled') { this.setDict(key, item.value) } }) } refresh() } return this.dict[key] }, getDictContent() { if (!this.dictProblemSources.length) { const refresh = async () => { const res = await Promise.allSettled>([listDictContentTree()]) res.forEach((item) => { if (item.status == 'fulfilled') { item.value.forEach(item => { this.dictContent.push(item) }) } }) } refresh() } return this.dictContent }, getDictProblemSources() { if (!this.dictProblemSources.length && dictProblemSourcesFlag) { dictProblemSourcesFlag = false; const refresh = async () => { const res = await Promise.allSettled>([listDictProblemSourceTree()]) res.forEach((item) => { if (item.status == 'fulfilled') { item.value.forEach(item => { this.dictProblemSources.push(item) }) } }) } refresh() } return this.dictProblemSources }, getDeparts() { if (!this.departs.length && departCatchFlag) { departCatchFlag = false const refresh = async () => { const res = await Promise.allSettled>([departTree()]) res.forEach((item) => { if (item.status == 'fulfilled') { item.value.forEach(item => { this.departs.push(item) }) } }) } refresh() } return this.departs; }, getFlowNodes() { if (!this.flowNodes.length) { const refresh = async () => { const res = await Promise.allSettled>([listFlowNode()]) res.forEach((item) => { if (item.status == 'fulfilled') { item.value.forEach(item => { this.flowNodes.push(item) }) } }) } refresh() } return this.flowNodes; } } }) export default useCatchStore