useLayoutMenu.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type { Menu } from '/@/router/types';
  2. import type { Ref } from 'vue';
  3. import { watch, unref, ref, computed } from 'vue';
  4. import { useRouter } from 'vue-router';
  5. import { MenuSplitTyeEnum } from '/@/enums/menuEnum';
  6. import { useThrottleFn } from '@vueuse/core';
  7. import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
  8. import { getChildrenMenus, getCurrentParentPath, getMenus, getShallowMenus } from '/@/router/menus';
  9. import { usePermissionStore } from '/@/store/modules/permission';
  10. import { useAppInject } from '/@/hooks/web/useAppInject';
  11. export function useSplitMenu(splitType: Ref<MenuSplitTyeEnum>) {
  12. // Menu array
  13. const menusRef = ref<Menu[]>([]);
  14. const { currentRoute } = useRouter();
  15. const { getIsMobile } = useAppInject();
  16. const permissionStore = usePermissionStore();
  17. const { setMenuSetting, getIsHorizontal, getSplit } = useMenuSetting();
  18. const throttleHandleSplitLeftMenu = useThrottleFn(handleSplitLeftMenu, 50);
  19. const splitNotLeft = computed(
  20. () => unref(splitType) !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontal)
  21. );
  22. const getSplitLeft = computed(
  23. () => !unref(getSplit) || unref(splitType) !== MenuSplitTyeEnum.LEFT
  24. );
  25. const getSpiltTop = computed(() => unref(splitType) === MenuSplitTyeEnum.TOP);
  26. const normalType = computed(() => {
  27. return unref(splitType) === MenuSplitTyeEnum.NONE || !unref(getSplit);
  28. });
  29. watch(
  30. [() => unref(currentRoute).path, () => unref(splitType)],
  31. async ([path]: [string, MenuSplitTyeEnum]) => {
  32. if (unref(splitNotLeft) || unref(getIsMobile)) return;
  33. const { meta } = unref(currentRoute);
  34. const currentActiveMenu = meta.currentActiveMenu as string;
  35. let parentPath = await getCurrentParentPath(path);
  36. if (!parentPath) {
  37. parentPath = await getCurrentParentPath(currentActiveMenu);
  38. }
  39. parentPath && throttleHandleSplitLeftMenu(parentPath);
  40. },
  41. {
  42. immediate: true,
  43. }
  44. );
  45. // Menu changes
  46. watch(
  47. [() => permissionStore.getLastBuildMenuTime, () => permissionStore.getBackMenuList],
  48. () => {
  49. genMenus();
  50. },
  51. {
  52. immediate: true,
  53. }
  54. );
  55. // split Menu changes
  56. watch(
  57. () => getSplit.value,
  58. () => {
  59. if (unref(splitNotLeft)) return;
  60. genMenus();
  61. }
  62. );
  63. // Handle left menu split
  64. async function handleSplitLeftMenu(parentPath: string) {
  65. if (unref(getSplitLeft) || unref(getIsMobile)) return;
  66. // spilt mode left
  67. const children = await getChildrenMenus(parentPath);
  68. if (!children || !children.length) {
  69. setMenuSetting({ hidden: true });
  70. menusRef.value = [];
  71. return;
  72. }
  73. setMenuSetting({ hidden: false });
  74. menusRef.value = children;
  75. }
  76. // get menus
  77. async function genMenus() {
  78. // normal mode
  79. if (unref(normalType) || unref(getIsMobile)) {
  80. menusRef.value = await getMenus();
  81. return;
  82. }
  83. // split-top
  84. if (unref(getSpiltTop)) {
  85. const shallowMenus = await getShallowMenus();
  86. menusRef.value = shallowMenus;
  87. return;
  88. }
  89. }
  90. return { menusRef };
  91. }