useLayoutMenu.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { useThrottle } from '/@/hooks/core/useThrottle';
  7. import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
  8. import {
  9. getChildrenMenus,
  10. getCurrentParentPath,
  11. getFlatChildrenMenus,
  12. getFlatMenus,
  13. getMenus,
  14. getShallowMenus,
  15. } from '/@/router/menus';
  16. import { permissionStore } from '/@/store/modules/permission';
  17. // import { useI18n } from '/@/hooks/web/useI18n';
  18. // import { cloneDeep } from 'lodash-es';
  19. // const { t } = useI18n();
  20. export function useSplitMenu(splitType: Ref<MenuSplitTyeEnum>) {
  21. // Menu array
  22. const menusRef = ref<Menu[]>([]);
  23. // flat menu array
  24. const flatMenusRef = ref<Menu[]>([]);
  25. const { currentRoute } = useRouter();
  26. const { setMenuSetting, getIsHorizontal, getSplit } = useMenuSetting();
  27. const [throttleHandleSplitLeftMenu] = useThrottle(handleSplitLeftMenu, 50);
  28. const splitNotLeft = computed(
  29. () => unref(splitType) !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontal)
  30. );
  31. const splitLeft = computed(() => !unref(getSplit) || unref(splitType) !== MenuSplitTyeEnum.LEFT);
  32. const spiltTop = computed(() => unref(splitType) === MenuSplitTyeEnum.TOP);
  33. const normalType = computed(() => {
  34. return unref(splitType) === MenuSplitTyeEnum.NONE || !unref(getSplit);
  35. });
  36. // const getI18nFlatMenus = computed(() => {
  37. // return setI18nName(flatMenusRef.value, true, false);
  38. // });
  39. // const getI18nMenus = computed(() => {
  40. // return setI18nName(menusRef.value, true, true);
  41. // });
  42. watch(
  43. [() => unref(currentRoute).path, () => unref(splitType)],
  44. async ([path]: [string, MenuSplitTyeEnum]) => {
  45. if (unref(splitNotLeft)) return;
  46. const parentPath = await getCurrentParentPath(path);
  47. parentPath && throttleHandleSplitLeftMenu(parentPath);
  48. },
  49. {
  50. immediate: true,
  51. }
  52. );
  53. // Menu changes
  54. watch(
  55. [() => permissionStore.getLastBuildMenuTimeState, () => permissionStore.getBackMenuListState],
  56. () => {
  57. genMenus();
  58. },
  59. {
  60. immediate: true,
  61. }
  62. );
  63. // split Menu changes
  64. watch([() => getSplit.value], () => {
  65. if (unref(splitNotLeft)) return;
  66. genMenus();
  67. });
  68. // function setI18nName(list: Menu[], clone = false, deep = true) {
  69. // const menus = clone ? cloneDeep(list) : list;
  70. // const arr: Menu[] = [];
  71. // menus.forEach((item) => {
  72. // if (!item.name.includes('.')) return;
  73. // item.name = t(item.name);
  74. // if (item.children && deep) {
  75. // setI18nName(item.children, false, deep);
  76. // }
  77. // });
  78. // return menus;
  79. // }
  80. // Handle left menu split
  81. async function handleSplitLeftMenu(parentPath: string) {
  82. if (unref(splitLeft)) return;
  83. // spilt mode left
  84. const children = await getChildrenMenus(parentPath);
  85. if (!children) {
  86. setMenuSetting({ hidden: false });
  87. flatMenusRef.value = [];
  88. menusRef.value = [];
  89. return;
  90. }
  91. const flatChildren = await getFlatChildrenMenus(children);
  92. setMenuSetting({ hidden: true });
  93. flatMenusRef.value = flatChildren;
  94. menusRef.value = children;
  95. }
  96. // get menus
  97. async function genMenus() {
  98. // normal mode
  99. if (unref(normalType)) {
  100. flatMenusRef.value = await getFlatMenus();
  101. menusRef.value = await getMenus();
  102. return;
  103. }
  104. // split-top
  105. if (unref(spiltTop)) {
  106. const shallowMenus = await getShallowMenus();
  107. flatMenusRef.value = shallowMenus;
  108. menusRef.value = shallowMenus;
  109. return;
  110. }
  111. }
  112. return { flatMenusRef, menusRef };
  113. }