feat: 字典支持number类型存储
This commit is contained in:
parent
8df6e3860f
commit
74198a0edc
@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
import type { DictData } from '#/api/system/dict/dict-data-model';
|
||||
|
||||
import { reactive } from 'vue';
|
||||
@ -10,13 +11,22 @@ import { defineStore } from 'pinia';
|
||||
export interface Option {
|
||||
disabled?: boolean;
|
||||
label: string;
|
||||
value: string;
|
||||
value: number | string;
|
||||
}
|
||||
|
||||
export function dictToOptions(data: DictData[]): Option[] {
|
||||
/**
|
||||
* 将字典数据转为Options
|
||||
* @param data 字典数据
|
||||
* @param formatNumber 是否需要将value格式化为number类型
|
||||
* @returns options
|
||||
*/
|
||||
export function dictToOptions(
|
||||
data: DictData[],
|
||||
formatNumber = false,
|
||||
): Option[] {
|
||||
return data.map((item) => ({
|
||||
label: item.dictLabel,
|
||||
value: item.dictValue,
|
||||
value: formatNumber ? Number(item.dictValue) : item.dictValue,
|
||||
}));
|
||||
}
|
||||
|
||||
@ -47,7 +57,6 @@ export const useDictStore = defineStore('app-dict', () => {
|
||||
dictMap.set(dictName, reactive([]));
|
||||
}
|
||||
// 这里拿到的就不可能为空了
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
return dictMap.get(dictName)!;
|
||||
}
|
||||
|
||||
@ -58,7 +67,6 @@ export const useDictStore = defineStore('app-dict', () => {
|
||||
dictOptionsMap.set(dictName, []);
|
||||
}
|
||||
// 这里拿到的就不可能为空了
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
return dictOptionsMap.get(dictName)!;
|
||||
}
|
||||
|
||||
@ -76,7 +84,11 @@ export const useDictStore = defineStore('app-dict', () => {
|
||||
* 否则 直接set
|
||||
*
|
||||
*/
|
||||
function setDictInfo(dictName: string, dictValue: DictData[]) {
|
||||
function setDictInfo(
|
||||
dictName: string,
|
||||
dictValue: DictData[],
|
||||
formatNumber = false,
|
||||
) {
|
||||
if (dictMap.has(dictName) && dictMap.get(dictName)?.length === 0) {
|
||||
dictMap.get(dictName)?.push(...dictValue);
|
||||
} else {
|
||||
@ -86,9 +98,11 @@ export const useDictStore = defineStore('app-dict', () => {
|
||||
dictOptionsMap.has(dictName) &&
|
||||
dictOptionsMap.get(dictName)?.length === 0
|
||||
) {
|
||||
dictOptionsMap.get(dictName)?.push(...dictToOptions(dictValue));
|
||||
dictOptionsMap
|
||||
.get(dictName)
|
||||
?.push(...dictToOptions(dictValue, formatNumber));
|
||||
} else {
|
||||
dictOptionsMap.set(dictName, dictToOptions(dictValue));
|
||||
dictOptionsMap.set(dictName, dictToOptions(dictValue, formatNumber));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,13 @@ import { useDictStore } from '#/store/dict';
|
||||
* 抽取公共逻辑的基础方法
|
||||
* @param dictName 字典名称
|
||||
* @param dataGetter 获取字典数据的函数
|
||||
* @param formatNumber 是否格式化字典value为number类型
|
||||
* @returns 数据
|
||||
*/
|
||||
function fetchAndCacheDictData<T>(
|
||||
dictName: string,
|
||||
dataGetter: () => T[],
|
||||
formatNumber = false,
|
||||
): T[] {
|
||||
const { dictRequestCache, setDictInfo } = useDictStore();
|
||||
// 有调用方决定如何获取数据
|
||||
@ -23,7 +25,7 @@ function fetchAndCacheDictData<T>(
|
||||
.then((resp) => {
|
||||
// 缓存到store 这样就不用重复获取了
|
||||
// 内部处理了push的逻辑 这里不用push
|
||||
setDictInfo(dictName, resp);
|
||||
setDictInfo(dictName, resp, formatNumber);
|
||||
})
|
||||
.finally(() => {
|
||||
// 移除请求状态缓存
|
||||
@ -54,9 +56,14 @@ export function getDict(dictName: string) {
|
||||
/**
|
||||
* 一般是Select, Radio, Checkbox等组件使用
|
||||
* @param dictName 字典名称
|
||||
* @param formatNumber 是否格式化字典value为number类型
|
||||
* @returns Options数组
|
||||
*/
|
||||
export function getDictOptions(dictName: string) {
|
||||
export function getDictOptions(dictName: string, formatNumber = false) {
|
||||
const { getDictOptions } = useDictStore();
|
||||
return fetchAndCacheDictData(dictName, () => getDictOptions(dictName));
|
||||
return fetchAndCacheDictData(
|
||||
dictName,
|
||||
() => getDictOptions(dictName),
|
||||
formatNumber,
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user