From 03ffdd67fc4c40f3e9443931a0aa55e508182873 Mon Sep 17 00:00:00 2001 From: wupengfei <834520024@qq.com> Date: 星期三, 06 八月 2025 17:00:06 +0800 Subject: [PATCH] feat: 字典 --- src/hooks/dic.ts | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 106 insertions(+), 5 deletions(-) diff --git a/src/hooks/dic.ts b/src/hooks/dic.ts index 25dd1c8..8935320 100644 --- a/src/hooks/dic.ts +++ b/src/hooks/dic.ts @@ -1,8 +1,109 @@ import { useQuery, useQueryClient } from '@tanstack/vue-query'; -import { SearchType } from '@/constants'; +import * as dictionaryServices from '@/services/api/dictionary'; -type UseSearchSettingTypeOptions = { - searchType: number; - belongType?: number; - onSuccess?: (data: any[]) => any; +export function useGetDictionaryCategorySelect() { + const { data: dictionaryCategoryList, refetch } = useQuery({ + queryKey: ['dictionaryServices/getDictionaryCategorySelect'], + queryFn: async () => { + let res = await dictionaryServices.getDictionaryCategorySelect({}, { showLoading: false }); + return res.map((x) => ({ + ...x, + fieldNamesMap: x.data.fieldNames ? JSON.parse(x.data.fieldNames) : {}, + })); + }, + placeholderData: () => + [] as API.SelectQueryResultOptionGuidGetDictionaryCategorySelectQueryOption[], + }); + + const queryClient = useQueryClient(); + + function ensureQueryData() { + return queryClient.ensureQueryData< + API.SelectQueryResultOptionGuidGetDictionaryCategorySelectQueryOption[] + >({ + queryKey: ['dictionaryServices/getDictionaryCategorySelect'], + }); + } + + function getDictionaryCategoryById(id: string) { + return dictionaryCategoryList.value.find((x) => x.value === id); + } + + function getDictionaryCategoryByCode(code: string) { + return dictionaryCategoryList.value.find((x) => x.code === code); + } + + function getDictionaryCategoryNameByCode(code: string) { + return getDictionaryCategoryByCode(code)?.label ?? ''; + } + + return { + dictionaryCategoryList, + ensureQueryData, + getDictionaryCategoryById, + getDictionaryCategoryNameByCode, + getDictionaryCategoryByCode, + }; +} + +type UseDictionaryDataSelectOptions = { + categoryId?: MaybeRef<string>; + categoryCode?: MaybeRef<string>; }; + +export function useDictionaryDataSelect({ + categoryId, + categoryCode, +}: UseDictionaryDataSelectOptions) { + const { data: dictionaryDataList, refetch } = useQuery({ + queryKey: ['dictionaryServices/getDictionaryDataSelect'], + queryFn: async () => { + let res = await dictionaryServices.getDictionaryDataSelect( + { + categoryId: unref(categoryId), + categoryCode: unref(categoryCode), + }, + { showLoading: false } + ); + return res.map((x) => ({ + ...x, + code: x.data?.code ?? '', + })); + }, + placeholderData: () => + [] as API.SelectQueryResultOptionGuidGetDictionaryDataSelectQueryResultOption[], + + enabled: !!unref(categoryId) || !!unref(categoryCode), + }); + + function getDictionaryDataNameById(id: string) { + return dictionaryDataList.value?.find((x) => x.value === id)?.label; + } + + function getDictionaryDataByCode(code: string) { + return dictionaryDataList.value?.find((x) => x.code === code); + } + + function getDictionaryDataNameByCode(code: string) { + return getDictionaryDataByCode(code)?.label ?? ''; + } + + const queryClient = useQueryClient(); + + function ensureQueryData() { + return queryClient.ensureQueryData< + API.SelectQueryResultOptionGuidGetDictionaryDataSelectQueryResultOption[] + >({ + queryKey: ['dictionaryServices/getDictionaryDataSelect'], + }); + } + + return { + dictionaryDataList, + ensureQueryData, + refetch, + getDictionaryDataNameById, + getDictionaryDataNameByCode, + getDictionaryDataByCode, + }; +} -- Gitblit v1.9.1