useMenuSplit.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import { ref, computed, watch, nextTick, onMounted } from 'vue'
  2. import { useI18n } from 'vue-i18n'
  3. import Config from '@/config/index'
  4. import { localesList } from '@/locale/index'
  5. import { useWindowWidth } from '@/composables/useWindowWidth'
  6. import useGlobalStore from '@/stores/use-global-store'
  7. import useRouter from '@/hooks/useRouter'
  8. import useRoute from '@/hooks/useRoute'
  9. export interface MenuItem {
  10. path: string
  11. label: string
  12. icon: string
  13. children?: MenuItem[]
  14. isOpenMenu?: boolean
  15. submenuHeight?: number
  16. isExternal?: boolean
  17. type?: string
  18. lang?: string
  19. }
  20. function cloneMenu(menus: MenuItem[]): MenuItem[] {
  21. return menus.map(item => ({
  22. ...item,
  23. children: item.children ? cloneMenu(item.children) : [],
  24. isOpenMenu: item.isOpenMenu ?? false,
  25. }))
  26. }
  27. export function useMenuSplit() {
  28. const { locale } = useI18n()
  29. const globalStore = useGlobalStore()
  30. const mode = computed(() => globalStore.mode)
  31. const windowWidth = useWindowWidth(300)
  32. const shouldShowLanguageMenu = computed(() => windowWidth.value <= 991)
  33. const { host } = Config
  34. const router = useRouter()
  35. const route = useRoute()
  36. // 子菜单 DOM 引用
  37. const submenuRefs = ref<any[]>([])
  38. function setSubmenuRef(index: number, el: HTMElement | null) {
  39. if (el) {
  40. submenuRefs.value[index] = el
  41. }
  42. }
  43. // 设置全局模式
  44. function setMode(code: string) {
  45. globalStore.setMode(code);
  46. const homePath = mode.value === 'customer' ? '/pages/customer/index' : '/pages/ib/index'
  47. router.push(homePath)
  48. nextTick(() => {
  49. submenuRefs.value = [] // 重置,等待重新绑定
  50. menus.value.forEach((item, index) => {
  51. if (item.isOpenMenu && item.children && item.children.length) {
  52. updateSubmenuHeight(index)
  53. }
  54. })
  55. })
  56. }
  57. // 测量元素实际高度(用于过渡动画)
  58. const measureHeight = (element: HTMLElement): number => {
  59. const originalDisplay = element.style.display
  60. const originalPosition = element.style.position
  61. const originalVisibility = element.style.visibility
  62. const originalWidth = element.style.width
  63. element.style.display = 'block'
  64. element.style.position = 'absolute'
  65. element.style.visibility = 'hidden'
  66. element.style.width = '100%'
  67. const height = element.scrollHeight || element.offsetHeight
  68. element.style.display = originalDisplay
  69. element.style.position = originalPosition
  70. element.style.visibility = originalVisibility
  71. element.style.width = originalWidth
  72. return height
  73. }
  74. // 更新指定索引的子菜单高度
  75. const updateSubmenuHeight = (index: number) => {
  76. const refs = submenuRefs.value
  77. nextTick(() => {
  78. if (refs && refs[index]) {
  79. const el = refs[index].$el || refs[index]
  80. const height = measureHeight(el)
  81. if (height > 0) {
  82. menus.value[index].submenuHeight = height
  83. }
  84. }
  85. })
  86. }
  87. let clickTimer: ReturnType<typeof setTimeout> | null = null
  88. // 点击菜单项(切换展开/折叠或跳转)
  89. function handleClick(index: number) {
  90. if (clickTimer) return
  91. clickTimer = setTimeout(() => {
  92. clickTimer = null
  93. }, 300)
  94. const item = menus.value[index]
  95. // 无子菜单:执行跳转或特殊操作
  96. if (!item.children || item.children.length === 0) {
  97. // #ifdef H5
  98. if (item.type === 'chat') {
  99. if (window.LiveChatWidget) {
  100. window.LiveChatWidget.call('maximize')
  101. }
  102. return
  103. }
  104. // #endif
  105. router.push(item.path)
  106. return
  107. }
  108. // 有子菜单:切换展开/折叠状态
  109. item.isOpenMenu = !item.isOpenMenu
  110. if (item.isOpenMenu) {
  111. nextTick(() => updateSubmenuHeight(index))
  112. }
  113. }
  114. // 子菜单项点击事件(由 cwg-submenu 组件发出)
  115. function handleSubmenuClick(subItem: any) {
  116. // 处理语言切换
  117. if (subItem.type === 'lang') {
  118. locale.value = subItem.lang
  119. return
  120. }
  121. // 处理外部链接
  122. if (subItem.isExternal) {
  123. // #ifdef H5
  124. window.open(subItem.path, '_blank')
  125. // #endif
  126. // #ifdef APP-PLUS
  127. plus.runtime.openURL(subItem.path)
  128. // #endif
  129. return
  130. }
  131. // 内部页面跳转
  132. router.push(subItem.path)
  133. }
  134. // 窗口大小变化时重新计算所有已展开子菜单的高度
  135. const handleResize = () => {
  136. menus.value.forEach((item, index) => {
  137. if (item.isOpenMenu && item.children && item.children.length) {
  138. updateSubmenuHeight(index)
  139. }
  140. })
  141. }
  142. const customMenuList = computed(() =>
  143. localesList.map((code) => ({
  144. label: `language.${code}`,
  145. lang: code,
  146. type: "lang",
  147. path: '/'
  148. }))
  149. )
  150. const languageMenuItem = computed<MenuItem>(() => ({
  151. path: '/',
  152. isOpenMenu: false,
  153. label: 'language.index',
  154. icon: 'cwg-lang',
  155. children: customMenuList.value,
  156. submenuHeight: 0,
  157. }))
  158. const customerBaseMenus = computed<MenuItem[]>(() => [
  159. {
  160. isOpenMenu: false,
  161. submenuHeight: 0,
  162. path: '/',
  163. label: 'Shop.Index.Transaction',
  164. icon: 'crm-trade',
  165. children: [
  166. { path: '/pages/customer/index', label: 'Custom.Index.AccountList', icon: 'icon-client' },
  167. { path: '/pages/customer/trade-history', label: 'Ib.Report.Tit1', icon: 'icon-transfer' },
  168. { path: '/pages/customer/trade-position', label: 'Ib.Report.Tit4', icon: 'icon-transfer' },
  169. { path: '/pages/customer/recording-history', label: 'Home.page_customer.item7', icon: 'icon-application' },
  170. ],
  171. },
  172. {
  173. isOpenMenu: false,
  174. submenuHeight: 0,
  175. path: '/',
  176. label: 'Latest.PaymentWallet',
  177. icon: 'crm-payment',
  178. children: [
  179. { path: '/pages/customer/deposit', label: 'Home.page_customer.item2', icon: 'icon-deposit' },
  180. { path: '/pages/customer/withdrawal', label: 'Home.page_customer.item3', icon: 'icon-withdrawal' },
  181. { path: '/pages/customer/payment-history', label: 'Home.page_customer.item4', icon: 'icon-payment' },
  182. { path: '/pages/customer/transfer', label: 'Custom.Index.Transfer', icon: 'icon-transfer' },
  183. ],
  184. },
  185. {
  186. path: '/',
  187. isOpenMenu: false,
  188. label: 'News.News',
  189. icon: 'crm-chart-area',
  190. children: [
  191. { path: '/pages/analytics/analystViews', label: 'News.Announcement', icon: 'icon-application' },
  192. { path: '/pages/analytics/news', label: 'News.NewsInformation', icon: 'icon-application' },
  193. {
  194. path: `https://www.${host}.com/${locale.value}/economic-calendar`,
  195. label: 'News.FinancialCalendar',
  196. icon: 'icon-application',
  197. isExternal: true,
  198. },
  199. ],
  200. },
  201. {
  202. path: '/pages/customer/withdrawal',
  203. isOpenMenu: false,
  204. label: 'Downloadpage.item1',
  205. icon: 'crm-download',
  206. children: [],
  207. },
  208. {
  209. path: '/pages/common/chat',
  210. isOpenMenu: false,
  211. label: 'Downloadpage.item16',
  212. icon: 'crm-headset',
  213. children: [],
  214. type: 'chat',
  215. },
  216. {
  217. path: '/',
  218. isOpenMenu: false,
  219. label: 'Custom.Index.Settings',
  220. icon: 'crm-sz',
  221. children: [
  222. { path: '/pages/mine/info?type=1', label: 'PersonalManagement.Title.PersonalInformation', icon: 'crm-headset' },
  223. { path: '/pages/mine/info?type=2', label: 'PersonalManagement.Title.BankInformation', icon: 'crm-headset' },
  224. { path: '/pages/mine/info?type=3', label: 'PersonalManagement.Title.FileManagement', icon: 'crm-headset' },
  225. { path: '/pages/mine/info?type=4', label: 'PersonalManagement.Title.SecurityCenter', icon: 'crm-headset' },
  226. { path: '/pages/common/notice', label: 'News.Notice', icon: 'crm-headset' },
  227. ],
  228. },
  229. ])
  230. const ibBaseMenus = computed<MenuItem[]>(() => [
  231. {
  232. isOpenMenu: false,
  233. path: '/pages/ib/index',
  234. label: 'Home.page_ib.item1',
  235. icon: 'crm-mb',
  236. },
  237. {
  238. path: '/',
  239. label: 'Home.page_ib.item2',
  240. icon: 'crm-bg',
  241. children: [
  242. { path: '/pages/ib/customer', label: 'Home.page_ib.item2', icon: 'icon-deposit' },
  243. { path: '/pages/ib/subsList', label: 'Home.page_ib.item12', icon: 'icon-deposit' },
  244. { path: '/pages/ib/agentList', label: 'Home.page_ib.item11', icon: 'icon-deposit' },
  245. { path: '/pages/ib/accountList', label: 'Home.page_ib.item10', icon: 'icon-deposit' }
  246. ],
  247. },
  248. {
  249. isOpenMenu: false,
  250. submenuHeight: 0,
  251. path: '/',
  252. label: 'Latest.PaymentWallet',
  253. icon: 'crm-payment',
  254. children: [
  255. { path: '/pages/ib/transfer', label: 'Home.page_ib.item4', icon: 'icon-payment' },
  256. { path: '/pages/ib/withdraw', label: 'Home.page_ib.item5', icon: 'icon-transfer' },
  257. { path: '/pages/ib/agent-transfer', label: 'Home.page_ib.item9', icon: 'icon-transfer' },
  258. { path: '/pages/ib/recording', label: 'Home.page_ib.item7', icon: 'icon-application' },
  259. ],
  260. },
  261. {
  262. isOpenMenu: false,
  263. path: '/',
  264. label: 'Home.page_ib.item3',
  265. icon: 'crm-newspaper',
  266. children: [
  267. { path: '/pages/ib/report', label: 'Home.page_ib.item3', icon: 'icon-withdrawal' },
  268. ],
  269. },
  270. ])
  271. const menus = ref<MenuItem[]>([])
  272. // 监听 mode 变化,自动导航到对应首页
  273. watch(mode, (newMode, oldMode) => {
  274. if (newMode !== oldMode) {
  275. const base = newMode === 'customer' ? [...customerBaseMenus.value] : [...ibBaseMenus.value]
  276. if (shouldShowLanguageMenu.value) {
  277. base.push(languageMenuItem.value)
  278. }
  279. menus.value = cloneMenu(base)
  280. }
  281. }, { immediate: true })
  282. // 监听路由变化:自动展开包含当前路径的父菜单,不自动关闭其他菜单
  283. watch(route, () => {
  284. const currentPath = route.path
  285. const shouldOpenIndices: number[] = []
  286. menus.value.forEach((item, idx) => {
  287. if (item.children && item.children.length) {
  288. const isActive = item.children.some(child => {
  289. if (child.isExternal || child.type === 'lang') return false
  290. return currentPath === child.path || currentPath.startsWith(child.path + '?') || currentPath.startsWith(child.path + '/')
  291. })
  292. if (isActive && !item.isOpenMenu) {
  293. shouldOpenIndices.push(idx)
  294. }
  295. }
  296. })
  297. if (shouldOpenIndices.length) {
  298. shouldOpenIndices.forEach(idx => {
  299. menus.value[idx].isOpenMenu = true
  300. })
  301. nextTick(() => {
  302. shouldOpenIndices.forEach(idx => updateSubmenuHeight(idx))
  303. })
  304. }
  305. }, { immediate: true })
  306. watch(windowWidth, handleResize)
  307. onMounted(() => {
  308. nextTick(() => {
  309. menus.value.forEach((item, index) => {
  310. if (item.isOpenMenu && item.children && item.children.length) {
  311. updateSubmenuHeight(index)
  312. }
  313. })
  314. })
  315. })
  316. return {
  317. menus, // 最终菜单(已克隆,可直接修改 isOpenMenu 等)
  318. mode, // 只读或按需使用
  319. shouldShowLanguageMenu, // 可选,供外部获取状态
  320. windowWidth, // 可选,供外部获取宽度(300)
  321. setMode, // 可选,供外部设置模式
  322. setSubmenuRef, // 可选,供外部设置子菜单引用
  323. updateSubmenuHeight, // 可选,供外部更新子菜单高度
  324. handleClick, // 可选,供外部处理点击事件
  325. handleSubmenuClick, // 可选,供外部处理子菜单点击事件
  326. }
  327. }