feat: 字典支持number类型存储

This commit is contained in:
dap 2025-01-08 12:51:04 +08:00
parent 8df6e3860f
commit 74198a0edc
2 changed files with 32 additions and 11 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { DictData } from '#/api/system/dict/dict-data-model'; import type { DictData } from '#/api/system/dict/dict-data-model';
import { reactive } from 'vue'; import { reactive } from 'vue';
@ -10,13 +11,22 @@ import { defineStore } from 'pinia';
export interface Option { export interface Option {
disabled?: boolean; disabled?: boolean;
label: string; 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) => ({ return data.map((item) => ({
label: item.dictLabel, 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([])); dictMap.set(dictName, reactive([]));
} }
// 这里拿到的就不可能为空了 // 这里拿到的就不可能为空了
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return dictMap.get(dictName)!; return dictMap.get(dictName)!;
} }
@ -58,7 +67,6 @@ export const useDictStore = defineStore('app-dict', () => {
dictOptionsMap.set(dictName, []); dictOptionsMap.set(dictName, []);
} }
// 这里拿到的就不可能为空了 // 这里拿到的就不可能为空了
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return dictOptionsMap.get(dictName)!; return dictOptionsMap.get(dictName)!;
} }
@ -76,7 +84,11 @@ export const useDictStore = defineStore('app-dict', () => {
* set * set
* *
*/ */
function setDictInfo(dictName: string, dictValue: DictData[]) { function setDictInfo(
dictName: string,
dictValue: DictData[],
formatNumber = false,
) {
if (dictMap.has(dictName) && dictMap.get(dictName)?.length === 0) { if (dictMap.has(dictName) && dictMap.get(dictName)?.length === 0) {
dictMap.get(dictName)?.push(...dictValue); dictMap.get(dictName)?.push(...dictValue);
} else { } else {
@ -86,9 +98,11 @@ export const useDictStore = defineStore('app-dict', () => {
dictOptionsMap.has(dictName) && dictOptionsMap.has(dictName) &&
dictOptionsMap.get(dictName)?.length === 0 dictOptionsMap.get(dictName)?.length === 0
) { ) {
dictOptionsMap.get(dictName)?.push(...dictToOptions(dictValue)); dictOptionsMap
.get(dictName)
?.push(...dictToOptions(dictValue, formatNumber));
} else { } else {
dictOptionsMap.set(dictName, dictToOptions(dictValue)); dictOptionsMap.set(dictName, dictToOptions(dictValue, formatNumber));
} }
} }

View File

@ -5,11 +5,13 @@ import { useDictStore } from '#/store/dict';
* *
* @param dictName * @param dictName
* @param dataGetter * @param dataGetter
* @param formatNumber value为number类型
* @returns * @returns
*/ */
function fetchAndCacheDictData<T>( function fetchAndCacheDictData<T>(
dictName: string, dictName: string,
dataGetter: () => T[], dataGetter: () => T[],
formatNumber = false,
): T[] { ): T[] {
const { dictRequestCache, setDictInfo } = useDictStore(); const { dictRequestCache, setDictInfo } = useDictStore();
// 有调用方决定如何获取数据 // 有调用方决定如何获取数据
@ -23,7 +25,7 @@ function fetchAndCacheDictData<T>(
.then((resp) => { .then((resp) => {
// 缓存到store 这样就不用重复获取了 // 缓存到store 这样就不用重复获取了
// 内部处理了push的逻辑 这里不用push // 内部处理了push的逻辑 这里不用push
setDictInfo(dictName, resp); setDictInfo(dictName, resp, formatNumber);
}) })
.finally(() => { .finally(() => {
// 移除请求状态缓存 // 移除请求状态缓存
@ -54,9 +56,14 @@ export function getDict(dictName: string) {
/** /**
* Select, Radio, Checkbox等组件使用 * Select, Radio, Checkbox等组件使用
* @param dictName * @param dictName
* @param formatNumber value为number类型
* @returns Options数组 * @returns Options数组
*/ */
export function getDictOptions(dictName: string) { export function getDictOptions(dictName: string, formatNumber = false) {
const { getDictOptions } = useDictStore(); const { getDictOptions } = useDictStore();
return fetchAndCacheDictData(dictName, () => getDictOptions(dictName)); return fetchAndCacheDictData(
dictName,
() => getDictOptions(dictName),
formatNumber,
);
} }