提交“
This commit is contained in:
1 parent
69ee0f505c
commit
9b88581450
614 files changed
+90318
-689022
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { fetchAllDictData } from '@/api/admin'
|
||||
|
||||
interface DictItem {
|
||||
label: string
|
||||
value: string
|
||||
tagType: string
|
||||
}
|
||||
|
||||
export const useDictStore = defineStore('dictStore', {
|
||||
state: () => ({
|
||||
dictMap: {} as Record<string, DictItem[]>,
|
||||
loaded: false,
|
||||
}),
|
||||
|
||||
getters: {
|
||||
getLabel: (state) => (dictType: string, value: string): string => {
|
||||
const items = state.dictMap[dictType]
|
||||
if (!items) return value || '—'
|
||||
const item = items.find((i) => i.value === value)
|
||||
return item?.label ?? value ?? '—'
|
||||
},
|
||||
getTagType: (state) => (dictType: string, value: string): string => {
|
||||
const items = state.dictMap[dictType]
|
||||
if (!items) return 'info'
|
||||
const item = items.find((i) => i.value === value)
|
||||
return item?.tagType || 'info'
|
||||
},
|
||||
getOptions: (state) => (dictType: string): DictItem[] => {
|
||||
return state.dictMap[dictType] || []
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
async loadAllDict() {
|
||||
if (this.loaded) return
|
||||
try {
|
||||
const res = await fetchAllDictData()
|
||||
this.dictMap = (res as any)?.data || res || {}
|
||||
this.loaded = true
|
||||
} catch (e) {
|
||||
console.error('加载字典数据失败', e)
|
||||
}
|
||||
},
|
||||
clearDict() {
|
||||
this.dictMap = {}
|
||||
this.loaded = false
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user