From 301c54de08c6a19b3473a58f756c1c30970f29dd Mon Sep 17 00:00:00 2001
From: wupengfei <834520024@qq.com>
Date: 星期三, 06 八月 2025 17:38:58 +0800
Subject: [PATCH] feat: 字典
---
src/hooks/dic.ts | 126 +++++++++++++++++++++++++++++++++++++++--
1 files changed, 119 insertions(+), 7 deletions(-)
diff --git a/src/hooks/dic.ts b/src/hooks/dic.ts
index f896744..8543fa8 100644
--- a/src/hooks/dic.ts
+++ b/src/hooks/dic.ts
@@ -1,9 +1,121 @@
-import { useQuery } from '@tanstack/vue-query';
-import { useQueryClient } from '@tanstack/vue-query';
-import { SearchType } from '@/constants';
+import { useQuery, useQueryClient } from '@tanstack/vue-query';
+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 updateDictionaryCategorySelect() {
+ queryClient.invalidateQueries({
+ 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,
+ updateDictionaryCategorySelect,
+ };
+}
+
+type UseDictionaryDataSelectOptions = {
+ categoryId?: MaybeRef<string>;
+ categoryCode?: MaybeRef<CategoryCode>;
};
+
+export function useDictionaryDataSelect({
+ categoryId,
+ categoryCode,
+}: UseDictionaryDataSelectOptions) {
+ const { data: dictionaryDataList, refetch } = useQuery({
+ queryKey: ['dictionaryServices/getDictionaryDataSelect', categoryId, categoryCode],
+ 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[],
+ });
+
+ 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'],
+ });
+ }
+
+ function updateDictionaryDataSelect() {
+ queryClient.invalidateQueries({
+ queryKey: ['dictionaryServices/getDictionaryDataSelect'],
+ });
+ }
+
+ return {
+ dictionaryDataList,
+ ensureQueryData,
+ refetch,
+ getDictionaryDataNameById,
+ getDictionaryDataNameByCode,
+ getDictionaryDataByCode,
+ updateDictionaryDataSelect,
+ };
+}
--
Gitblit v1.9.1