提交
This commit is contained in:
1 parent
6dc8cfb2c1
commit
d26fb23fcf
181 files changed
+25727
-24557
No files matched your search
@@ -1,5 +1,10 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { fetchAllDictData } from '@/api/admin'
|
||||
import {
|
||||
fetchAllDictData,
|
||||
fetchEnsureDictData,
|
||||
fetchEnsureRemoveDictData
|
||||
} from '@/api/admin'
|
||||
import { isDictEnsureType } from '@/constants/dict-ensure-whitelist'
|
||||
|
||||
interface DictItem {
|
||||
label: string
|
||||
@@ -11,6 +16,8 @@ export const useDictStore = defineStore('dictStore', {
|
||||
state: () => ({
|
||||
dictMap: {} as Record<string, DictItem[]>,
|
||||
loaded: false,
|
||||
/** 本会话 ensure 新插入的 value,用于下拉显示删除按钮 */
|
||||
sessionCreated: {} as Record<string, string[]>
|
||||
}),
|
||||
|
||||
getters: {
|
||||
@@ -29,6 +36,11 @@ export const useDictStore = defineStore('dictStore', {
|
||||
getOptions: (state) => (dictType: string): DictItem[] => {
|
||||
return state.dictMap[dictType] || []
|
||||
},
|
||||
isSessionCreated:
|
||||
(state) =>
|
||||
(dictType: string, value: string): boolean => {
|
||||
return (state.sessionCreated[dictType] || []).includes(value)
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
@@ -45,6 +57,51 @@ export const useDictStore = defineStore('dictStore', {
|
||||
clearDict() {
|
||||
this.dictMap = {}
|
||||
this.loaded = false
|
||||
this.sessionCreated = {}
|
||||
},
|
||||
},
|
||||
upsertLocal(dictType: string, item: { label: string; value: string; tagType?: string }) {
|
||||
const list = this.dictMap[dictType] ? [...this.dictMap[dictType]] : []
|
||||
const idx = list.findIndex((i) => i.value === item.value)
|
||||
const row: DictItem = {
|
||||
label: item.label,
|
||||
value: item.value,
|
||||
tagType: item.tagType || 'info'
|
||||
}
|
||||
if (idx >= 0) list[idx] = row
|
||||
else list.push(row)
|
||||
this.dictMap[dictType] = list
|
||||
},
|
||||
removeLocal(dictType: string, value: string) {
|
||||
const list = this.dictMap[dictType] || []
|
||||
this.dictMap[dictType] = list.filter((i) => i.value !== value)
|
||||
const created = this.sessionCreated[dictType] || []
|
||||
this.sessionCreated[dictType] = created.filter((v) => v !== value)
|
||||
},
|
||||
markSessionCreated(dictType: string, value: string) {
|
||||
const created = this.sessionCreated[dictType] ? [...this.sessionCreated[dictType]] : []
|
||||
if (!created.includes(value)) created.push(value)
|
||||
this.sessionCreated[dictType] = created
|
||||
},
|
||||
async ensure(dictType: string, label: string, preferredValue?: string) {
|
||||
const text = (label || '').trim()
|
||||
if (!text) throw new Error('字典标签不能为空')
|
||||
if (!isDictEnsureType(dictType)) throw new Error('不支持的字典类型')
|
||||
const res = await fetchEnsureDictData({
|
||||
dictType,
|
||||
label: text,
|
||||
preferredValue
|
||||
})
|
||||
const data = (res as any)?.data || res
|
||||
this.upsertLocal(dictType, { label: data.label, value: data.value })
|
||||
if (data.created) this.markSessionCreated(dictType, data.value)
|
||||
return data as { label: string; value: string; created: boolean }
|
||||
},
|
||||
async ensureRemove(dictType: string, value: string) {
|
||||
const v = (value || '').trim()
|
||||
if (!v) throw new Error('字典值不能为空')
|
||||
if (!isDictEnsureType(dictType)) throw new Error('不支持的字典类型')
|
||||
await fetchEnsureRemoveDictData({ dictType, value: v })
|
||||
this.removeLocal(dictType, v)
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user