menu.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <a-menu theme="dark"
  3. mode="inline"
  4. class="left-menu"
  5. v-model:selectedKeys="selectedKeys"
  6. v-model:openKeys="openKeys"
  7. @click="menuClick">
  8. <a-sub-menu v-for="item in menuList"
  9. :key="item.code">
  10. <template #title>
  11. <span>
  12. <svg class="icon svg-icon"
  13. aria-hidden="true">
  14. <use :xlink:href="fontIcon(item.code)"></use>
  15. </svg>
  16. <span class="menu-item_title"
  17. v-show="!collapsed">{{ item.title }}</span>
  18. </span>
  19. </template>
  20. <a-menu-item :key="subItem.code"
  21. v-for="subItem in item.children">
  22. <span>{{ subItem.title }}</span>
  23. </a-menu-item>
  24. </a-sub-menu>
  25. </a-menu>
  26. </template>
  27. <script lang="ts">
  28. import { defineComponent, ref, SetupContext, unref, watchEffect } from 'vue';
  29. import APP from '@/services';
  30. import { initData } from '@/common/methods';
  31. import { OperationTabMenu } from '@/services/go/commonService/interface';
  32. function handleMenu(context: SetupContext) {
  33. const selectedKeys = ref<string[]>(['1-1']);
  34. const openKeys = ref<string[]>(['1']);
  35. const preOpenKeys = ref<string[]>(['1']);
  36. const menuList = APP.getRef('menus');
  37. function menuClick(value: any) {
  38. const arr = value.keyPath.reverse();
  39. const first = menuList.value.find((e: OperationTabMenu) => e.code === arr[0]);
  40. if (first) {
  41. const seconde = first.children.find((e: OperationTabMenu) => e.code === arr[1]);
  42. seconde && context.emit('chooseMenu', seconde);
  43. }
  44. }
  45. // 一级菜单图标
  46. function fontIcon(code: string): string {
  47. let result = 'icon-jinrongfenxi';
  48. switch (code) {
  49. case 'market':
  50. result = 'icon-jinrongfenxi';
  51. break;
  52. case 'info':
  53. result = 'icon-zhangdan';
  54. break;
  55. case 'business':
  56. result = 'icon-renwu';
  57. break;
  58. case 'manage':
  59. result = 'icon-quanxian';
  60. break;
  61. case 'search':
  62. result = 'icon-chazhaojilu';
  63. break;
  64. case 'report':
  65. result = 'icon-bingtu';
  66. break;
  67. }
  68. return '#' + result;
  69. }
  70. return { selectedKeys, menuList, openKeys, preOpenKeys, menuClick, fontIcon };
  71. }
  72. export default defineComponent({
  73. props: {
  74. collapsed: {
  75. default: true,
  76. type: Boolean,
  77. },
  78. },
  79. setup(props, context) {
  80. const { selectedKeys, openKeys, menuList, menuClick, preOpenKeys, fontIcon } = handleMenu(context);
  81. watchEffect(() => {
  82. if (props.collapsed) {
  83. preOpenKeys.value = openKeys.value;
  84. openKeys.value = [];
  85. } else {
  86. openKeys.value = preOpenKeys.value;
  87. }
  88. });
  89. initData(() => {
  90. // 处理路由跳转到菜单栏里第一个对应的页面
  91. const list = unref(menuList);
  92. if (list.length && list[0].children && list[0].children.length) {
  93. // 处理修改具体某个菜单栏,主要处理页面刷新
  94. let firstIndex = 0,
  95. secondeIndex = 0;
  96. for (let i = 0; i < list.length; i++) {
  97. for (let j = 0; j < list[i].children.length; j++) {
  98. if (window.location.hash.includes(list[i].children[j].code)) {
  99. firstIndex = i;
  100. secondeIndex = j;
  101. break;
  102. }
  103. }
  104. }
  105. const temp = list[firstIndex].children[secondeIndex];
  106. const code = temp.code;
  107. selectedKeys.value = [code];
  108. openKeys.value = [list[firstIndex].code];
  109. context.emit('chooseMenu', temp);
  110. }
  111. });
  112. return { selectedKeys, openKeys, menuList, menuClick, fontIcon };
  113. },
  114. });
  115. </script>