From 7ad45c897be118f4b3fdfeacb74373f1b147cf79 Mon Sep 17 00:00:00 2001 From: dap <15891557205@163.com> Date: Fri, 13 Dec 2024 15:41:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=8E=B7=E5=8F=96=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E7=9A=84=E6=96=B9=E6=B3=95=20=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=87=BD=E6=95=B0=20=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/utils/dict.ts | 74 ++++++++++++++++----------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/apps/web-antd/src/utils/dict.ts b/apps/web-antd/src/utils/dict.ts index ce108d1f..792e90b2 100644 --- a/apps/web-antd/src/utils/dict.ts +++ b/apps/web-antd/src/utils/dict.ts @@ -1,15 +1,22 @@ -import type { DictData } from '#/api/system/dict/dict-data-model'; - import { dictDataInfo } from '#/api/system/dict/dict-data'; -import { type Option, useDictStore } from '#/store/dict'; +import { useDictStore } from '#/store/dict'; + +/** + * 抽取公共逻辑的基础方法 + * @param dictName 字典名称 + * @param dataGetter 获取字典数据的函数 + * @returns 数据 + */ +function fetchAndCacheDictData( + dictName: string, + dataGetter: () => T[], +): T[] { + const { dictRequestCache, setDictInfo } = useDictStore(); + // 有调用方决定如何获取数据 + const dataList = dataGetter(); -// todo 重复代码的封装 -export function getDict(dictName: string): DictData[] { - const { dictRequestCache, getDict, setDictInfo } = useDictStore(); - // 这里拿到 - const dictList = getDict(dictName); // 检查请求状态缓存 - if (dictList.length === 0 && !dictRequestCache.has(dictName)) { + if (dataList.length === 0 && !dictRequestCache.has(dictName)) { dictRequestCache.set( dictName, dictDataInfo(dictName) @@ -25,40 +32,31 @@ export function getDict(dictName: string): DictData[] { * 会导致if一直进入逻辑导致接口无限刷新 * 在这里dictList为空时 不删除缓存 */ - if (dictList.length > 0) { + if (dataList.length > 0) { dictRequestCache.delete(dictName); } }), ); } - return dictList; + return dataList; } -export function getDictOptions(dictName: string): Option[] { - const { dictRequestCache, getDictOptions, setDictInfo } = useDictStore(); - const dictOptionList = getDictOptions(dictName); - // 检查请求状态缓存 - if (dictOptionList.length === 0 && !dictRequestCache.has(dictName)) { - dictRequestCache.set( - dictName, - dictDataInfo(dictName) - .then((resp) => { - // 缓存到store 这样就不用重复获取了 - // 内部处理了push的逻辑 这里不用push - setDictInfo(dictName, resp); - }) - .finally(() => { - // 移除请求状态缓存 - /** - * 这里主要判断字典item为空的情况(无奈兼容 不给字典item本来就是错误用法) - * 会导致if一直进入逻辑导致接口五线刷新 - * 在这里dictList为空时 不删除缓存 - */ - if (dictOptionList.length > 0) { - dictRequestCache.delete(dictName); - } - }), - ); - } - return dictOptionList; +/** + * 这里是提供给渲染标签使用的方法 + * @param dictName 字典名称 + * @returns 字典信息 + */ +export function getDict(dictName: string) { + const { getDict } = useDictStore(); + return fetchAndCacheDictData(dictName, () => getDict(dictName)); +} + +/** + * 一般是Select, Radio, Checkbox等组件使用 + * @param dictName 字典名称 + * @returns Options数组 + */ +export function getDictOptions(dictName: string) { + const { getDictOptions } = useDictStore(); + return fetchAndCacheDictData(dictName, () => getDictOptions(dictName)); }