left-window.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, onBeforeUnmount } from 'vue';
  23. import useRouter from "@/hooks/useRouter";
  24. const router = useRouter();
  25. import useRoute from '@/hooks/useRoute'
  26. const route = useRoute()
  27. import { useI18n } from "vue-i18n";
  28. const { t } = useI18n();
  29. interface MenuItem {
  30. key: string;
  31. label: string;
  32. icon?: string;
  33. children?: MenuItem[];
  34. }
  35. const emit = defineEmits(['menu-click']);
  36. // 菜单数据
  37. const menu = ref<MenuItem[]>([
  38. {
  39. isOpenMenu: false, submenuHeight: 0,
  40. path: '/pages/customer/index', label: 'Home.msg.Custom', icon: 'crm-house',
  41. children: [
  42. { path: '/pages/customer/index', label: 'Client Zone', icon: 'icon-client' },
  43. { path: '/pages/customer/promotion', label: 'Promotion Center', icon: 'icon-promotion' },
  44. { path: '/pages/customer/deposit', label: 'Deposit', icon: 'icon-deposit' },
  45. { path: '/pages/customer/withdrawal', label: 'Withdrawal', icon: 'icon-withdrawal' },
  46. { path: '/pages/customer/payment-history', label: 'Payment History', icon: 'icon-payment' },
  47. { path: '/pages/customer/transfer', label: 'Internal Transfer', icon: 'icon-transfer' },
  48. { path: '/pages/customer/recording-history', label: 'Application History', icon: 'icon-application' }
  49. ]
  50. },
  51. {
  52. isOpenMenu: false,
  53. path: '/pages/customer/promotion', label: 'Home.msg.Ib', icon: 'crm-users',
  54. children: []
  55. },
  56. {
  57. path: '/pages/customer/deposit', isOpenMenu: false, label: 'Documentary.title', icon: 'crm-chart-area',
  58. children: []
  59. },
  60. {
  61. path: '/pages/customer/withdrawal', isOpenMenu: false, label: 'Downloadpage.item1', icon: 'crm-download',
  62. children: []
  63. },
  64. {
  65. path: '/pages/customer/support', isOpenMenu: false, label: 'Downloadpage.item16', icon: 'crm-headset',
  66. children: []
  67. },
  68. {
  69. path: '/pages/customer/support', isOpenMenu: false, label: '设置', icon: 'crm-headset',
  70. children: [
  71. { path: '/pages/mine/info?type=1', isOpenMenu: false, label: 'PersonalManagement.Title.PersonalInformation', icon: 'crm-headset' },
  72. { path: '/pages/mine/info?type=2', isOpenMenu: false, label: 'PersonalManagement.Title.BankInformation', icon: 'crm-headset' },
  73. { path: '/pages/mine/info?type=3', isOpenMenu: false, label: 'PersonalManagement.Title.FileManagement', icon: 'crm-headset' },
  74. { path: '/pages/mine/info?type=4', isOpenMenu: false, label: 'PersonalManagement.Title.SecurityCenter', icon: 'crm-headset' },
  75. ]
  76. },
  77. ]);
  78. const submenuRefs = ref<any[]>([]);
  79. const measureHeight = (element: HTMLElement): number => {
  80. const originalDisplay = element.style.display;
  81. const originalPosition = element.style.position;
  82. const originalVisibility = element.style.visibility;
  83. const originalWidth = element.style.width;
  84. element.style.display = 'block';
  85. element.style.position = 'absolute';
  86. element.style.visibility = 'hidden';
  87. element.style.width = '100%';
  88. const height = element.scrollHeight || element.offsetHeight;
  89. element.style.display = originalDisplay;
  90. element.style.position = originalPosition;
  91. element.style.visibility = originalVisibility;
  92. element.style.width = originalWidth;
  93. return height;
  94. };
  95. const updateSubmenuHeight = (index: number) => {
  96. const refs = submenuRefs.value;
  97. if (refs && refs[index]) {
  98. const el = refs[index].$el || refs[index];
  99. const height = measureHeight(el);
  100. if (height > 0) {
  101. menu.value[index].submenuHeight = height;
  102. }
  103. }
  104. };
  105. function handleClick(index: number) {
  106. if (!menu.value[index].children || menu.value[index].children.length == 0) {
  107. router.push(menu.value[index].path);
  108. return;
  109. }
  110. menu.value[index].isOpenMenu = !menu.value[index].isOpenMenu;
  111. }
  112. watch(route, () => {
  113. const currentPath = route.path;
  114. menu.value.forEach((item, index) => {
  115. if (item.children) {
  116. const isActive = item.children.some(child => child.path.includes(currentPath));
  117. menu.value[index].isOpenMenu = isActive;
  118. if (isActive) {
  119. nextTick(() => {
  120. updateSubmenuHeight(index);
  121. });
  122. }
  123. }
  124. });
  125. }, { immediate: true })
  126. // 添加窗口大小变化监听
  127. const handleResize = () => {
  128. menu.value.forEach((item, index) => {
  129. if (item.children) {
  130. updateSubmenuHeight(index);
  131. }
  132. });
  133. };
  134. onMounted(() => {
  135. nextTick(() => {
  136. menu.value.forEach((item, index) => {
  137. if (item.children) {
  138. updateSubmenuHeight(index);
  139. }
  140. });
  141. });
  142. // 添加窗口resize监听
  143. window.addEventListener('resize', handleResize);
  144. });
  145. // 组件卸载时移除监听
  146. onBeforeUnmount(() => {
  147. window.removeEventListener('resize', handleResize);
  148. });
  149. </script>
  150. <style scoped lang="scss">
  151. @import "@/uni.scss";
  152. .cwg-sidebar {
  153. width: 100%;
  154. color: #6c8595;
  155. height: calc(100vh - 56px);
  156. overflow: auto;
  157. display: flex;
  158. flex-direction: column;
  159. align-items: center;
  160. padding: px2rpx(8);
  161. padding-top: px2rpx(20);
  162. box-sizing: border-box;
  163. gap: px2rpx(8);
  164. border-right: 1px solid rgba(108, 133, 149, 0.12);
  165. .logo {
  166. width: px2rpx(54);
  167. }
  168. .submenu-box {
  169. width: 100%;
  170. height: 0;
  171. overflow: hidden;
  172. }
  173. .menu {
  174. width: 100%;
  175. position: relative;
  176. display: flex;
  177. flex-direction: column;
  178. align-items: center;
  179. box-sizing: border-box;
  180. }
  181. .menu-item {
  182. width: 100%;
  183. height: px2rpx(40);
  184. cursor: pointer;
  185. display: flex;
  186. align-items: center;
  187. justify-content: space-between;
  188. gap: px2rpx(8);
  189. padding: px2rpx(10);
  190. box-sizing: border-box;
  191. font-size: 14px;
  192. .menu-label {
  193. flex: 1;
  194. }
  195. &:hover {
  196. background: rgba(108, 133, 149, 0.12) !important;
  197. border: 1px solid rgb(145, 163, 176) !important;
  198. border-radius: px2rpx(4);
  199. }
  200. .expanded .icon {
  201. transform: rotate(180deg);
  202. }
  203. }
  204. }
  205. @media screen and (max-width: 1100px) {
  206. .cwg-sidebar:not(:hover) .menu-label,
  207. .cwg-sidebar:not(:hover) .submenu-box,
  208. .cwg-sidebar:not(:hover) .chevron-icon {
  209. display: none;
  210. }
  211. .cwg-sidebar:hover .menu-label,
  212. .cwg-sidebar:hover .submenu-box,
  213. .cwg-sidebar:hover .chevron-icon {
  214. display: block;
  215. }
  216. }
  217. </style>