You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.5 KiB
TypeScript

import { defineStore } from 'pinia'
import { store } from '../index'
// @ts-ignore
import { DictDataVO } from '@/api/system/dict/types'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache('sessionStorage')
import { getSimpleDictDataList } from '@/api/system/dict/dict.data'
export interface DictValueType {
value: any
label: string
labelEn?: string
clorType?: string
cssClass?: string
}
export interface DictTypeType {
dictType: string
dictValue: DictValueType[]
}
export interface DictState {
dictMap: Map<string, any>
isSetDict: boolean
}
export const useDictStore = defineStore('dict', {
state: (): DictState => ({
dictMap: new Map<string, any>(),
isSetDict: false
}),
getters: {
getDictMap(): Recordable {
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
if (dictMap) {
this.dictMap = dictMap
}
return this.dictMap
},
getIsSetDict(): boolean {
return this.isSetDict
}
},
actions: {
async setDictMap() {
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
if (dictMap) {
const dictTypes = Object.keys(dictMap || {})
const firstDictType = dictTypes[0]
const firstList = firstDictType ? dictMap[firstDictType] : undefined
const needsUpgrade =
Array.isArray(firstList) && firstList.length > 0 && !('labelEn' in (firstList[0] || {}))
if (!needsUpgrade) {
this.dictMap = dictMap
this.isSetDict = true
return
}
}
try {
const res = await getSimpleDictDataList()
const dictDataMap = new Map<string, any>()
res.forEach((dictData: DictDataVO) => {
const enumValueObj = dictDataMap[dictData.dictType]
if (!enumValueObj) {
dictDataMap[dictData.dictType] = []
}
dictDataMap[dictData.dictType].push({
value: dictData.value,
label: dictData.label,
labelEn: (dictData as any).labelEn,
colorType: dictData.colorType,
cssClass: dictData.cssClass
})
})
this.dictMap = dictDataMap
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 })
} catch (error) {
console.error('加载数据字典失败', error)
this.dictMap = new Map<string, any>()
} finally {
this.isSetDict = true
}
},
getDictByType(type: string) {
if (!this.isSetDict) {
this.setDictMap()
}
return this.dictMap[type]
},
async resetDict() {
wsCache.delete(CACHE_KEY.DICT_CACHE)
try {
const res = await getSimpleDictDataList()
const dictDataMap = new Map<string, any>()
res.forEach((dictData: DictDataVO) => {
const enumValueObj = dictDataMap[dictData.dictType]
if (!enumValueObj) {
dictDataMap[dictData.dictType] = []
}
dictDataMap[dictData.dictType].push({
value: dictData.value,
label: dictData.label,
labelEn: (dictData as any).labelEn,
colorType: dictData.colorType,
cssClass: dictData.cssClass
})
})
this.dictMap = dictDataMap
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 })
} catch (error) {
console.error('重置数据字典失败', error)
this.dictMap = new Map<string, any>()
} finally {
this.isSetDict = true
}
}
}
})
export const useDictStoreWithOut = () => {
return useDictStore(store)
}