cwg-sidebar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. console.log(height, 12121);
  95. if (height > 0) {
  96. menu.value[index].submenuHeight = height;
  97. }
  98. }
  99. };
  100. function handleClick(index: number) {
  101. menu.value[index].isOpenMenu = !menu.value[index].isOpenMenu;
  102. }
  103. watch(route, () => {
  104. const currentPath = route.path;
  105. menu.value.forEach((item, index) => {
  106. if (item.children) {
  107. const isActive = item.children.some(child => child.path === currentPath);
  108. menu.value[index].isOpenMenu = isActive;
  109. if (isActive) {
  110. nextTick(() => {
  111. updateSubmenuHeight(index);
  112. });
  113. }
  114. }
  115. });
  116. }, { immediate: true })
  117. onMounted(() => {
  118. nextTick(() => {
  119. menu.value.forEach((item, index) => {
  120. if (item.children) {
  121. updateSubmenuHeight(index);
  122. }
  123. });
  124. });
  125. });
  126. </script>
  127. <style scoped lang="scss">
  128. @import "@/uni.scss";
  129. .cwg-sidebar {
  130. width: px2rpx(280);
  131. color: #6c8595;
  132. height: calc(100vh - 56px);
  133. overflow: auto;
  134. display: flex;
  135. flex-direction: column;
  136. align-items: center;
  137. padding: px2rpx(8);
  138. padding-top: px2rpx(20);
  139. box-sizing: border-box;
  140. gap: px2rpx(8);
  141. border-right: 1px solid rgba(108, 133, 149, 0.12);
  142. .logo {
  143. width: px2rpx(54);
  144. }
  145. .submenu-box {
  146. width: 100%;
  147. height: 0;
  148. overflow: hidden;
  149. }
  150. .menu {
  151. width: 100%;
  152. position: relative;
  153. display: flex;
  154. flex-direction: column;
  155. align-items: center;
  156. box-sizing: border-box;
  157. }
  158. .menu-item {
  159. width: 100%;
  160. height: px2rpx(40);
  161. cursor: pointer;
  162. display: flex;
  163. align-items: center;
  164. justify-content: space-between;
  165. gap: px2rpx(8);
  166. padding: px2rpx(10);
  167. box-sizing: border-box;
  168. font-size: 14px;
  169. .menu-label {
  170. flex: 1;
  171. }
  172. &:hover {
  173. background: rgba(108, 133, 149, 0.12) !important;
  174. border: 1px solid rgb(145, 163, 176) !important;
  175. border-radius: px2rpx(4);
  176. }
  177. .expanded .icon {
  178. transform: rotate(180deg);
  179. }
  180. }
  181. }
  182. </style>