From fe09be116e0ffaef7ebd110bdf6375d817f72473 Mon Sep 17 00:00:00 2001 From: zhengyiming <540361168@qq.com> Date: 星期五, 08 八月 2025 10:43:46 +0800 Subject: [PATCH] fix: 修改 --- src/hooks/dic.ts | 165 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 146 insertions(+), 19 deletions(-) diff --git a/src/hooks/dic.ts b/src/hooks/dic.ts index 8543fa8..2eb60c5 100644 --- a/src/hooks/dic.ts +++ b/src/hooks/dic.ts @@ -1,5 +1,7 @@ import { useQuery, useQueryClient } from '@tanstack/vue-query'; import * as dictionaryServices from '@/services/api/dictionary'; +import { formatAreaListToTree } from '@/utils'; +import { CascaderProps } from 'element-plus'; export function useGetDictionaryCategorySelect() { const { data: dictionaryCategoryList, refetch } = useQuery({ @@ -11,15 +13,14 @@ fieldNamesMap: x.data.fieldNames ? JSON.parse(x.data.fieldNames) : {}, })); }, - placeholderData: () => - [] as API.SelectQueryResultOptionGuidGetDictionaryCategorySelectQueryOption[], + placeholderData: () => [] as API.SelectOptionGuidGetDictionaryCategorySelectQueryOption[], }); const queryClient = useQueryClient(); function ensureQueryData() { return queryClient.ensureQueryData< - API.SelectQueryResultOptionGuidGetDictionaryCategorySelectQueryOption[] + API.SelectOptionGuidGetDictionaryCategorySelectQueryOption[] >({ queryKey: ['dictionaryServices/getDictionaryCategorySelect'], }); @@ -32,11 +33,11 @@ } function getDictionaryCategoryById(id: string) { - return dictionaryCategoryList.value.find((x) => x.value === id); + return dictionaryCategoryList.value.find((x) => x.data?.id === id); } function getDictionaryCategoryByCode(code: string) { - return dictionaryCategoryList.value.find((x) => x.code === code); + return dictionaryCategoryList.value.find((x) => x.data?.code === code); } function getDictionaryCategoryNameByCode(code: string) { @@ -56,37 +57,53 @@ type UseDictionaryDataSelectOptions = { categoryId?: MaybeRef<string>; categoryCode?: MaybeRef<CategoryCode>; + parentId?: MaybeRef<string>; + staleTime?: number; + /** 鍏抽敭瀛� */ + keywords?: MaybeRef<string>; + /** 鏌ヨ鎵�鏈� */ + all?: MaybeRef<boolean>; + maxDeep?: number; }; export function useDictionaryDataSelect({ categoryId, categoryCode, + parentId, + staleTime, + keywords, + all, + maxDeep, }: UseDictionaryDataSelectOptions) { + const params = computed(() => ({ + categoryId: unref(categoryId), + categoryCode: unref(categoryCode), + parentId: unref(parentId), + keywords: unref(keywords), + all: unref(all), + maxDeep: maxDeep, + })); const { data: dictionaryDataList, refetch } = useQuery({ - queryKey: ['dictionaryServices/getDictionaryDataSelect', categoryId, categoryCode], + queryKey: ['dictionaryServices/getDictionaryDataSelect', params], queryFn: async () => { - let res = await dictionaryServices.getDictionaryDataSelect( - { - categoryId: unref(categoryId), - categoryCode: unref(categoryCode), - }, - { showLoading: false } - ); + let res = await dictionaryServices.getDictionaryDataSelect(params.value, { + showLoading: false, + }); return res.map((x) => ({ ...x, code: x.data?.code ?? '', })); }, - placeholderData: () => - [] as API.SelectQueryResultOptionGuidGetDictionaryDataSelectQueryResultOption[], + placeholderData: () => [] as API.SelectOptionStringGetDictionaryDataSelectQueryResultOption[], + staleTime, }); function getDictionaryDataNameById(id: string) { - return dictionaryDataList.value?.find((x) => x.value === id)?.label; + return dictionaryDataList.value?.find((x) => x.data?.id === id)?.label; } function getDictionaryDataByCode(code: string) { - return dictionaryDataList.value?.find((x) => x.code === code); + return dictionaryDataList.value?.find((x) => x.data?.code === code); } function getDictionaryDataNameByCode(code: string) { @@ -97,13 +114,13 @@ function ensureQueryData() { return queryClient.ensureQueryData< - API.SelectQueryResultOptionGuidGetDictionaryDataSelectQueryResultOption[] + API.SelectOptionStringGetDictionaryDataSelectQueryResultOption[] >({ queryKey: ['dictionaryServices/getDictionaryDataSelect'], }); } - function updateDictionaryDataSelect() { + function updateDictionaryDataSelect(categoryId?: string) { queryClient.invalidateQueries({ queryKey: ['dictionaryServices/getDictionaryDataSelect'], }); @@ -119,3 +136,113 @@ updateDictionaryDataSelect, }; } + +export function useArea() { + const queryClient = useQueryClient(); + + function getAreaList(parentId = '') { + const params = { + categoryCode: CategoryCode.Area, + parentId: parentId, + }; + return queryClient.fetchQuery({ + queryKey: ['dictionaryServices/getDictionaryDataSelect', params], + queryFn: async () => { + let res = await dictionaryServices.getDictionaryDataSelect(params, { showLoading: false }); + return res.map(convertDictionaryToAreaTreeNode); + }, + staleTime: Infinity, + }); + } + + return { getAreaList }; +} + +function convertDictionaryToAreaTreeNode( + item: API.SelectOptionStringGetDictionaryDataSelectQueryResultOption +) { + return { + children: [], + areaCode: item.data?.code, + parentCode: '', + areaName: item.label, + layer: Number(item.data?.field4), + sort: item.data?.sort, + id: item.data?.id, + } as API.AreaTreeNode; +} + +export function useAllAreaList() { + const { dictionaryDataList } = useDictionaryDataSelect({ + categoryCode: CategoryCode.Area, + staleTime: Infinity, + all: true, + maxDeep: AreaType.Area, + }); + + const areaList = computed(() => dictionaryDataList.value.map(convertDictionaryToAreaTreeNode)); + + const areaTreeProps = { + children: 'children', + label: 'areaName', + value: 'areaCode', + }; + + const areaTree = computed(() => formatAreaListToTree(areaList.value)); + + const findAreaCodeFromName = (areaName: string) => { + const areaItem = areaList.value.find((x) => x.areaName === areaName); + return areaItem?.areaCode ?? ''; + }; + + const findAreaNameFromCode = (areaCode: string) => { + const areaItem = areaList.value.find((x) => x.areaCode === areaCode); + return areaItem?.areaName ?? ''; + }; + + const findAreaItemFromCode = (areaCode: string) => { + const areaItem = areaList.value.find((x) => x.areaCode === areaCode); + return areaItem; + }; + + return { + areaList, + areaTree, + areaTreeProps, + findAreaCodeFromName, + findAreaNameFromCode, + findAreaItemFromCode, + }; +} + +type UseAreaByCascaderOptions = { + layer?: AreaType; +}; + +/** + * @description 鑱旂骇閫夋嫨涓娇鐢� + */ +export function useAreaByCascader(options: UseAreaByCascaderOptions = {}) { + const { layer = AreaType.Area } = options; + + const { getAreaList } = useArea(); + + return computed(() => ({ + props: { + label: 'areaName', + value: 'areaCode', + lazy: true, + async lazyLoad(node, resolve) { + const { level, data } = node; + const areas = (await getAreaList((data as API.AreaTreeNode).id)) as any[]; + + return resolve( + areas.map((x) => ({ + ...x, + leaf: x.layer >= layer, + })) + ); + }, + } as CascaderProps, + })); +} -- Gitblit v1.9.1