import { request } from "@cqsjjb/jjb-common-lib/http.js"; import { useEffect, useState } from "react"; import { DICTIONARY_APP_KEY_ENUM } from "../../../enum/dictionary"; import BasicSelect from "../Basic"; // 全局缓存 const cacheMap = new Map(); /** * 数据字典下拉组件 */ function DictionarySelect(props) { const { appKey = DICTIONARY_APP_KEY_ENUM.DEFAULT, dictValue = "", nameKey = "dictLabel", idKey = "dictValue", ...restProps } = props; const [data, setData] = useState([]); const getData = async () => { setData([]); if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) { console.error(`【DictionarySelect】 传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中,当前传入的 appKey 是 ${appKey}`); return; } if (!dictValue) { console.error(`【DictionarySelect】 缺少 dictValue 参数`); return; } // 生成缓存键 const cacheKey = JSON.stringify({ appKey, dictValue }); // 检查缓存,如果存在直接返回缓存结果 if (cacheMap.has(cacheKey)) { setData(cacheMap.get(cacheKey)); return; } const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); // 存入缓存 cacheMap.set(cacheKey, data); setData(data); }; useEffect(() => { getData(); }, [dictValue]); return ( ); } DictionarySelect.displayName = "DictionarySelect"; export default DictionarySelect;