import { defineStore } from 'pinia' import { ref } from 'vue' import { getDictOptions, getDictOptionsBatch, getAllDictOptions } from '@/api/dict' export const useDictStore = defineStore('dict', () => { const dictMap = ref({}) let loaded = {} const pending = new Map() let allLoaded = false let allPending = null function setItems(code, list) { dictMap.value = { ...dictMap.value, [code]: Array.isArray(list) ? list : [] } } function itemsOf(code) { return dictMap.value[code] || [] } function normalizeCodes(codes) { return [...new Set((codes || []).map((c) => String(c || '').trim()).filter(Boolean))] } async function load(codes) { const list = normalizeCodes(codes) const missing = list.filter((c) => !loaded[c] && !pending.has(c)) if (missing.length) { const promise = (async () => { if (missing.length === 1) { const code = missing[0] try { const res = await getDictOptions(code) setItems(code, res?.code === 200 ? res.data?.list || [] : []) } catch { setItems(code, []) } } else { try { const res = await getDictOptionsBatch(missing) const dicts = res?.code === 200 ? res.data?.dicts || {} : {} missing.forEach((code) => setItems(code, dicts[code] || [])) } catch { missing.forEach((code) => setItems(code, [])) } } })().finally(() => { missing.forEach((code) => { loaded[code] = true pending.delete(code) }) }) missing.forEach((code) => pending.set(code, promise)) } await Promise.all(list.map((c) => pending.get(c) || Promise.resolve())) return list.flatMap((c) => itemsOf(c)) } function loadAll() { if (allLoaded) return Promise.resolve() if (allPending) return allPending allPending = (async () => { try { const res = await getAllDictOptions() const dicts = res?.code === 200 ? res.data?.dicts || {} : {} const next = { ...dictMap.value } Object.entries(dicts).forEach(([code, list]) => { loaded[code] = true next[code] = Array.isArray(list) ? list : [] }) dictMap.value = next } catch { } finally { allLoaded = true allPending = null } })() return allPending } function ensure(code) { const list = normalizeCodes([code]) if (list.some((c) => !loaded[c] && !pending.has(c))) { load(list) } } function getDictItemsSync(code) { ensure(code) return itemsOf(code) } function getDictItem(code, value) { if (value === undefined || value === null || value === '') return null const key = String(value) return itemsOf(code).find((it) => String(it.item_value) === key) || null } function getDictLabel(code, value, fallback) { const hit = getDictItem(code, value) if (hit) return hit.item_label if (fallback !== undefined) return fallback return value === undefined || value === null ? '' : String(value) } function getDictTagType(code, value, fallback = 'info') { const hit = getDictItem(code, value) return hit && hit.tag_type ? hit.tag_type : fallback } function getSelectOptions(code) { return getDictItemsSync(code).map((it) => ({ label: it.item_label, value: it.item_value, tag_type: it.tag_type })) } function preloadDicts(codes) { return load(codes) } function clearCache(code) { if (code) { delete loaded[code] const next = { ...dictMap.value } delete next[code] dictMap.value = next return } loaded = {} dictMap.value = {} allLoaded = false } return { dictMap, ensure, loadAll, getDictItems: load, getDictItemsSync, getDictItem, getDictLabel, getDictTagType, getSelectOptions, preloadDicts, clearCache } })