cwg-sidebar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="cwg-sidebar">
  3. <view class="menu" v-for="(item, index) in menu" :key="item.path">
  4. <view class="menu-item" @click="handleClick(index)">
  5. <cwg-icon :name="item.icon" :size="20" color="#6c8595" />
  6. <view class="menu-label" v-t="item.label" />
  7. <view class="chevron-icon" :class="{ 'expanded': item.isOpenMenu }">
  8. <cwg-icon v-if="item.children && item.children.length" name="crm-chevron-down" :size="20"
  9. color="#6c8595" />
  10. </view>
  11. </view>
  12. <view ref="submenuRefs" class="submenu-box" :style="{
  13. height: item.isOpenMenu ? item.submenuHeight + 'px' : '0px',
  14. transition: 'height 281ms cubic-bezier(0.4, 0, 0.2, 1)'
  15. }" :class="{ 'active': item.isOpenMenu }">
  16. <cwg-submenu v-if="item.children" :submenu-items="item.children" @submenu-click="handleSubmenuClick" />
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script lang="ts" setup>
  22. import { ref, onMounted, nextTick, watch } from 'vue';
  23. import useRouter from "@/hooks/useRouter";
  24. const router = useRouter();
  25. import useRoute from '@/hooks/useRoute'
  26. const route = useRoute()
  27. interface MenuItem {
  28. key: string;
  29. label: string;
  30. icon?: string;
  31. children?: MenuItem[];
  32. }
  33. const emit = defineEmits(['menu-click']);
  34. // 菜单数据
  35. const menu = ref<MenuItem[]>([
  36. {
  37. isOpenMenu: false, submenuHeight: 0,
  38. path: '/pages/customer/index', label: 'Home.msg.Custom', icon: 'crm-house',
  39. children: [
  40. { path: '/pages/customer/index', label: 'Client Zone', icon: 'icon-client' },
  41. { path: '/pages/customer/promotion', label: 'Promotion Center', icon: 'icon-promotion' },
  42. { path: '/pages/customer/deposit', label: 'Deposit', icon: 'icon-deposit' },
  43. { path: '/pages/customer/withdrawal', label: 'Withdrawal', icon: 'icon-withdrawal' },
  44. { path: '/pages/customer/payment-history', label: 'Payment History', icon: 'icon-payment' },
  45. { path: '/pages/customer/transfer', label: 'Internal Transfer', icon: 'icon-transfer' },
  46. { path: '/pages/customer/recording-history', label: 'Application History', icon: 'icon-application' }
  47. ]
  48. },
  49. {
  50. isOpenMenu: false, submenuHeight: 0,
  51. path: '/pages/customer/index', label: 'Home.msg.Custom', icon: 'crm-house',
  52. children: []
  53. },
  54. {
  55. isOpenMenu: false,
  56. path: '/pages/customer/promotion', label: 'Home.msg.Ib', icon: 'crm-users',
  57. children: []
  58. },
  59. {
  60. path: '/pages/customer/deposit', isOpenMenu: false, label: 'Documentary.title', icon: 'crm-chart-area',
  61. children: []
  62. },
  63. {
  64. path: '/pages/customer/withdrawal', isOpenMenu: false, label: 'Downloadpage.item1', icon: 'crm-download',
  65. children: []
  66. },
  67. {
  68. path: '/pages/customer/support', isOpenMenu: false, label: 'Downloadpage.item16', icon: 'crm-headset',
  69. children: []
  70. },
  71. ]);
  72. const submenuRefs = ref<any[]>([]);
  73. const measureHeight = (element: HTMLElement): number => {
  74. const originalDisplay = element.style.display;
  75. const originalPosition = element.style.position;
  76. const originalVisibility = element.style.visibility;
  77. const originalWidth = element.style.width;
  78. element.style.display = 'block';
  79. element.style.position = 'absolute';
  80. element.style.visibility = 'hidden';
  81. element.style.width = '100%';
  82. const height = element.scrollHeight || element.offsetHeight;
  83. element.style.display = originalDisplay;
  84. element.style.position = originalPosition;
  85. element.style.visibility = originalVisibility;
  86. element.style.width = originalWidth;
  87. return height;
  88. };
  89. const updateSubmenuHeight = (index: number) => {
  90. const refs = submenuRefs.value;
  91. if (refs && refs[index]) {
  92. const el = refs[index].$el || refs[index];
  93. const height = measureHeight(el);
  94. if (height > 0) {
  95. menu.value[index].submenuHeight = height;
  96. }
  97. }
  98. };
  99. function handleClick(index: number) {
  100. menu.value[index].isOpenMenu = !menu.value[index].isOpenMenu;
  101. }
  102. watch(route, () => {
  103. const currentPath = route.path;
  104. menu.value.forEach((item, index) => {
  105. if (item.children) {
  106. const isActive = item.children.some(child => child.path === currentPath);
  107. menu.value[index].isOpenMenu = isActive;
  108. if (isActive) {
  109. nextTick(() => {
  110. updateSubmenuHeight(index);
  111. });
  112. }
  113. }
  114. });
  115. }, { immediate: true })
  116. onMounted(() => {
  117. nextTick(() => {
  118. menu.value.forEach((item, index) => {
  119. if (item.children) {
  120. updateSubmenuHeight(index);
  121. }
  122. });
  123. });
  124. });
  125. </script>
  126. <style scoped lang="scss">
  127. @import "@/uni.scss";
  128. .cwg-sidebar {
  129. width: px2rpx(280);
  130. color: #6c8595;
  131. height: calc(100vh - 56px);
  132. overflow: auto;
  133. display: flex;
  134. flex-direction: column;
  135. align-items: center;
  136. padding: px2rpx(8);
  137. padding-top: px2rpx(20);
  138. box-sizing: border-box;
  139. gap: px2rpx(8);
  140. border-right: 1px solid rgba(108, 133, 149, 0.12);
  141. .logo {
  142. width: px2rpx(54);
  143. }
  144. .submenu-box {
  145. width: 100%;
  146. height: 0;
  147. overflow: hidden;
  148. }
  149. .menu {
  150. width: 100%;
  151. position: relative;
  152. display: flex;
  153. flex-direction: column;
  154. align-items: center;
  155. box-sizing: border-box;
  156. }
  157. .menu-item {
  158. width: 100%;
  159. height: px2rpx(40);
  160. cursor: pointer;
  161. display: flex;
  162. align-items: center;
  163. justify-content: space-between;
  164. gap: px2rpx(8);
  165. padding: px2rpx(10);
  166. box-sizing: border-box;
  167. font-size: 14px;
  168. .menu-label {
  169. flex: 1;
  170. }
  171. &:hover {
  172. background: rgba(108, 133, 149, 0.12) !important;
  173. border: 1px solid rgb(145, 163, 176) !important;
  174. border-radius: px2rpx(4);
  175. }
  176. .expanded .icon {
  177. transform: rotate(180deg);
  178. }
  179. }
  180. }
  181. </style>