zhengyiming
6 天以前 20459fe59f680031bfc7c1012be860e0727cb016
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as menuServices from '@/services/api/menu';
import { flattenTree } from '@/utils';
import { DaPengAccessAdapter } from '@bole-core/components';
import { useQuery, useQueryClient } from '@tanstack/vue-query';
 
type UseMenusOptions = {
  params: MaybeRef<API.APIgetMenusParams>;
  enabled?: MaybeRef<boolean>;
};
 
export function useMenus({ params, enabled = true }: UseMenusOptions) {
  const queryClient = useQueryClient();
 
  const { data, refetch } = useQuery({
    queryKey: ['menuServices/getMenus', params],
    queryFn: async () => {
      return await menuServices.getMenus(unref(params), {
        showLoading: false,
      });
    },
    placeholderData: () => [] as API.GetMenusQueryResultItem[],
    staleTime: Infinity,
    enabled: enabled,
  });
 
  const flattenMenus = computed(() => flattenTree(data.value));
 
  function getMenuById(id: string) {
    return flattenMenus.value.find((item) => item.id === id);
  }
 
  return {
    menusTree: data,
    refetch,
    getMenuById,
  };
}
 
type UseMenuOptions = {
  params: MaybeRef<API.APIgetMenuParams>;
  enabled?: MaybeRef<boolean>;
};
 
export function useMenu({ params, enabled = true }: UseMenuOptions) {
  const queryClient = useQueryClient();
 
  const { data, refetch } = useQuery({
    queryKey: ['menuServices/getMenu', params],
    queryFn: async () => {
      return await menuServices.getMenu(unref(params), {
        showLoading: false,
      });
    },
    placeholderData: () => ({} as API.GetMenuQueryResult),
    enabled: enabled,
  });
 
  const menuDefaultGroup = computed(
    () => DaPengAccessAdapter.getMenuGroupAdapter(data.value?.groups ?? []) ?? {}
  );
 
  const menuFields = computed(() => {
    return menuDefaultGroup.value.fields ?? [];
  });
 
  const menuPageButtons = computed(() => {
    const pageButtonLocation = DaPengAccessAdapter.getButtonLocationAdapter(
      menuDefaultGroup.value,
      SubModuleKey[SubModuleType.PageButton]
    );
    return pageButtonLocation?.buttons ?? [];
  });
 
  const menuDataButtons = computed(() => {
    const pageButtonLocation = DaPengAccessAdapter.getButtonLocationAdapter(
      menuDefaultGroup.value,
      SubModuleKey[SubModuleType.DataButton]
    );
    return pageButtonLocation?.buttons ?? [];
  });
 
  return {
    menu: data,
    refetch,
    menuFields,
    menuPageButtons,
    menuDataButtons,
  };
}