left-window.vue 8.4 KB

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