From 8a20db630014c6fd536f9a1f7e22b8005d6f5eac Mon Sep 17 00:00:00 2001
From: zhengyiming <540361168@qq.com>
Date: 星期三, 15 十月 2025 13:35:18 +0800
Subject: [PATCH] fix: bug
---
src/hooks/dic.ts | 160 ++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 145 insertions(+), 15 deletions(-)
diff --git a/src/hooks/dic.ts b/src/hooks/dic.ts
index 5d9baff..06171e5 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({
@@ -31,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) {
@@ -55,36 +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.SelectOptionGuidGetDictionaryDataSelectQueryResultOption[],
+ 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) {
@@ -95,7 +114,7 @@
function ensureQueryData() {
return queryClient.ensureQueryData<
- API.SelectOptionGuidGetDictionaryDataSelectQueryResultOption[]
+ API.SelectOptionStringGetDictionaryDataSelectQueryResultOption[]
>({
queryKey: ['dictionaryServices/getDictionaryDataSelect'],
});
@@ -103,7 +122,7 @@
function updateDictionaryDataSelect(categoryId?: string) {
queryClient.invalidateQueries({
- queryKey: ['dictionaryServices/getDictionaryDataSelect', categoryId],
+ queryKey: ['dictionaryServices/getDictionaryDataSelect'],
});
}
@@ -117,3 +136,114 @@
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: item.data?.parentCode,
+ areaName: item.label,
+ layer: Number(item.data?.field4),
+ quickQuery: item.data?.field2,
+ 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