refactor: 获取字典的方法 提取公共函数 减少冗余代码
This commit is contained in:
parent
ccbc385bc8
commit
7ad45c897b
@ -1,15 +1,22 @@
|
|||||||
import type { DictData } from '#/api/system/dict/dict-data-model';
|
|
||||||
|
|
||||||
import { dictDataInfo } from '#/api/system/dict/dict-data';
|
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<T>(
|
||||||
|
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(
|
dictRequestCache.set(
|
||||||
dictName,
|
dictName,
|
||||||
dictDataInfo(dictName)
|
dictDataInfo(dictName)
|
||||||
@ -25,40 +32,31 @@ export function getDict(dictName: string): DictData[] {
|
|||||||
* 会导致if一直进入逻辑导致接口无限刷新
|
* 会导致if一直进入逻辑导致接口无限刷新
|
||||||
* 在这里dictList为空时 不删除缓存
|
* 在这里dictList为空时 不删除缓存
|
||||||
*/
|
*/
|
||||||
if (dictList.length > 0) {
|
if (dataList.length > 0) {
|
||||||
dictRequestCache.delete(dictName);
|
dictRequestCache.delete(dictName);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return dictList;
|
return dataList;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDictOptions(dictName: string): Option[] {
|
/**
|
||||||
const { dictRequestCache, getDictOptions, setDictInfo } = useDictStore();
|
* 这里是提供给渲染标签使用的方法
|
||||||
const dictOptionList = getDictOptions(dictName);
|
* @param dictName 字典名称
|
||||||
// 检查请求状态缓存
|
* @returns 字典信息
|
||||||
if (dictOptionList.length === 0 && !dictRequestCache.has(dictName)) {
|
*/
|
||||||
dictRequestCache.set(
|
export function getDict(dictName: string) {
|
||||||
dictName,
|
const { getDict } = useDictStore();
|
||||||
dictDataInfo(dictName)
|
return fetchAndCacheDictData(dictName, () => getDict(dictName));
|
||||||
.then((resp) => {
|
}
|
||||||
// 缓存到store 这样就不用重复获取了
|
|
||||||
// 内部处理了push的逻辑 这里不用push
|
/**
|
||||||
setDictInfo(dictName, resp);
|
* 一般是Select, Radio, Checkbox等组件使用
|
||||||
})
|
* @param dictName 字典名称
|
||||||
.finally(() => {
|
* @returns Options数组
|
||||||
// 移除请求状态缓存
|
*/
|
||||||
/**
|
export function getDictOptions(dictName: string) {
|
||||||
* 这里主要判断字典item为空的情况(无奈兼容 不给字典item本来就是错误用法)
|
const { getDictOptions } = useDictStore();
|
||||||
* 会导致if一直进入逻辑导致接口五线刷新
|
return fetchAndCacheDictData(dictName, () => getDictOptions(dictName));
|
||||||
* 在这里dictList为空时 不删除缓存
|
|
||||||
*/
|
|
||||||
if (dictOptionList.length > 0) {
|
|
||||||
dictRequestCache.delete(dictName);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return dictOptionList;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user