useMenuSplit.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. import LiveChatService from '@/utils/liveChat.js'
  10. export interface MenuItem {
  11. path: string
  12. label: string
  13. icon: string
  14. children?: MenuItem[]
  15. isOpenMenu?: boolean
  16. submenuHeight?: number
  17. isExternal?: boolean
  18. type?: string
  19. lang?: string
  20. }
  21. function cloneMenu(menus: MenuItem[]): MenuItem[] {
  22. return menus.map(item => ({
  23. ...item,
  24. children: item.children ? cloneMenu(item.children) : [],
  25. isOpenMenu: item.isOpenMenu ?? false,
  26. // 🔥 直接按数量 × 40px
  27. submenuHeight: (item.children?.length || 0) * ((40 + 8)) + 8,
  28. }))
  29. }
  30. export function useMenuSplit(handleClick1: (item: MenuItem) => void) {
  31. const { locale } = useI18n()
  32. const globalStore = useGlobalStore()
  33. const mode = computed(() => globalStore.mode)
  34. const windowWidth = useWindowWidth(300)
  35. const shouldShowLanguageMenu = computed(() => windowWidth.value <= 991)
  36. const { host } = Config
  37. const router = useRouter()
  38. const route = useRoute()
  39. const submenuRefs = ref<any[]>([])
  40. function setSubmenuRef(index: number, el: HTMLElement | null) { }
  41. // 设置全局模式
  42. function setMode(code: string) {
  43. globalStore.setMode(code);
  44. switch (code) {
  45. case 'follow':
  46. router.reLaunch('/pages/follow/index')
  47. break
  48. case 'ib':
  49. router.reLaunch('/pages/ib/index')
  50. break
  51. case 'customer':
  52. router.reLaunch('/pages/customer/index')
  53. break
  54. default:
  55. break
  56. }
  57. if (code === 'ib') {
  58. uni.$emit('open-ib')
  59. }
  60. }
  61. // 🔥 已删除:measureHeight
  62. // 🔥 已删除:updateSubmenuHeight
  63. let clickTimer: ReturnType<typeof setTimeout> | null = null
  64. function handleClick(index: number) {
  65. if (clickTimer) return
  66. clickTimer = setTimeout(() => {
  67. clickTimer = null
  68. }, 300)
  69. const item = menus.value[index]
  70. if (!item.children || item.children.length === 0) {
  71. // #ifdef H5
  72. if (item.type === 'chat' && !shouldShowLanguageMenu.value) {
  73. if (LiveChatService) {
  74. LiveChatService.showChat();
  75. }
  76. return
  77. } else {
  78. shouldShowLanguageMenu.value && handleClick1(item)
  79. router.push(item.path)
  80. return
  81. }
  82. // #endif
  83. shouldShowLanguageMenu.value && handleClick1(item)
  84. router.push(item.path)
  85. return
  86. }
  87. item.isOpenMenu = !item.isOpenMenu
  88. }
  89. function handleSubmenuClick(subItem: any) {
  90. if (subItem.type === 'lang') {
  91. locale.value = subItem.lang
  92. return
  93. }
  94. if (subItem.isExternal) {
  95. // #ifdef H5
  96. window.open(subItem.path, '_blank')
  97. // #endif
  98. // #ifdef APP-PLUS
  99. plus.runtime.openURL(subItem.path)
  100. // #endif
  101. return
  102. }
  103. shouldShowLanguageMenu.value && handleClick1(subItem)
  104. router.push(subItem.path)
  105. }
  106. const handleResize = () => { }
  107. const customMenuList = computed(() =>
  108. localesList.map((code) => ({
  109. label: `language.${code}`,
  110. lang: code,
  111. type: "lang",
  112. path: '/'
  113. }))
  114. )
  115. const languageMenuItem = computed<MenuItem>(() => ({
  116. path: '/',
  117. isOpenMenu: false,
  118. label: 'language.index',
  119. icon: 'cwg-lang',
  120. children: customMenuList.value,
  121. submenuHeight: customMenuList.value.length * ((40 + 8)) + 8,
  122. }))
  123. const customerBaseMenus = computed<MenuItem[]>(() => [
  124. {
  125. isOpenMenu: false,
  126. submenuHeight: 4 * ((40 + 8)) + 8,
  127. path: '/',
  128. label: 'Shop.Index.Transaction',
  129. icon: 'crm-trade',
  130. children: [
  131. { path: '/pages/customer/index', label: 'Custom.Index.AccountList', icon: 'icon-client' },
  132. { path: '/pages/customer/trade-history', label: 'Ib.Report.Tit1', icon: 'icon-transfer' },
  133. { path: '/pages/customer/trade-position', label: 'Ib.Report.Tit4', icon: 'icon-transfer' },
  134. { path: '/pages/customer/recording-history', label: 'Home.page_customer.item7', icon: 'icon-application' },
  135. ],
  136. },
  137. {
  138. isOpenMenu: false,
  139. submenuHeight: 6 * ((40 + 8)) + 8,
  140. path: '/',
  141. label: 'vu.item6',
  142. icon: 'crm-payment',
  143. children: [
  144. { path: '/pages/customer/deposit-select', label: 'Home.page_customer.item2', icon: 'icon-deposit' },
  145. { path: '/pages/customer/withdrawal-select', label: 'Home.page_customer.item3', icon: 'icon-withdrawal' },
  146. { path: '/pages/customer/payment-history', label: 'Home.page_customer.item4', icon: 'icon-payment' },
  147. { path: '/pages/customer/transfer', label: 'Custom.Index.Transfer', icon: 'icon-transfer' },
  148. { path: '/pages/customer/wallet-transfer', label: 'wallet.item62', icon: 'icon-transfer' },
  149. { path: '/pages/customer/wallet-history', label: 'wallet.item7', icon: 'icon-transfer' },
  150. ],
  151. },
  152. {
  153. path: '/pages/activities/index',
  154. isOpenMenu: false,
  155. label: 'Home.page_customer.item6',
  156. icon: 'crm-hd',
  157. children: [],
  158. submenuHeight: 0,
  159. },
  160. {
  161. path: '/',
  162. isOpenMenu: false,
  163. submenuHeight: 2 * ((40 + 8)) + 8,
  164. label: 'vu.item5',
  165. icon: 'crm-chart-area',
  166. children: [
  167. { path: '/pages/analytics/analystViews', label: 'News.Announcement', icon: 'icon-application' },
  168. { path: '/pages/analytics/news', label: 'News.NewsInformation', icon: 'icon-application' },
  169. ],
  170. },
  171. {
  172. path: '/pages/common/download',
  173. isOpenMenu: false,
  174. label: 'Downloadpage.item1',
  175. icon: 'crm-download',
  176. children: [],
  177. submenuHeight: 0,
  178. },
  179. {
  180. path: '/pages/common/chat',
  181. isOpenMenu: false,
  182. label: 'Downloadpage.item16',
  183. icon: 'crm-headset',
  184. children: [],
  185. type: 'chat',
  186. submenuHeight: 0,
  187. },
  188. {
  189. path: '/',
  190. isOpenMenu: false,
  191. submenuHeight: 5 * ((40 + 8)) + 8,
  192. label: 'Custom.Index.Settings',
  193. icon: 'crm-sz',
  194. children: [
  195. { path: '/pages/mine/info?type=1', label: 'PersonalManagement.Title.PersonalInformation', icon: 'crm-headset' },
  196. { path: '/pages/mine/info?type=2', label: 'PersonalManagement.Title.BankInformation', icon: 'crm-headset' },
  197. { path: '/pages/mine/info?type=3', label: 'PersonalManagement.Title.FileManagement', icon: 'crm-headset' },
  198. { path: '/pages/mine/info?type=4', label: 'PersonalManagement.Title.SecurityCenter', icon: 'crm-headset' },
  199. { path: '/pages/common/notice', label: 'News.Notice', icon: 'crm-headset' },
  200. ],
  201. },
  202. ])
  203. const ibBaseMenus = computed<MenuItem[]>(() => [
  204. {
  205. isOpenMenu: false,
  206. path: '/pages/ib/index',
  207. label: 'Documentary.console.item1',
  208. icon: 'crm-mb',
  209. submenuHeight: 0,
  210. },
  211. {
  212. path: '/',
  213. label: 'Ib.Custom.Manage3',
  214. icon: 'crm-bg',
  215. submenuHeight: 4 * ((40 + 8)) + 8,
  216. children: [
  217. { path: '/pages/ib/customer', label: 'Ib.Custom.Manage3', icon: 'icon-deposit' },
  218. { path: '/pages/ib/subsList', label: 'Ib.Custom.Manage2', icon: 'icon-deposit' },
  219. // { path: '/pages/ib/agentList', label: 'Documentary.console.item23', icon: 'icon-deposit' },
  220. { path: '/pages/ib/accountList', label: 'Ib.Custom.Manage1', icon: 'icon-deposit' }
  221. ],
  222. },
  223. {
  224. isOpenMenu: false,
  225. submenuHeight: 4 * ((40 + 8)) + 8,
  226. path: '/',
  227. label: 'vu.item6',
  228. icon: 'crm-payment',
  229. children: [
  230. { path: '/pages/ib/transfer', label: 'Home.page_ib.item4', icon: 'icon-payment' },
  231. { path: '/pages/ib/withdraw-select', label: 'Home.page_ib.item5', icon: 'icon-transfer' },
  232. { path: '/pages/ib/agent-transfer', label: 'Home.page_ib.item9', icon: 'icon-transfer' },
  233. { path: '/pages/ib/recording', label: 'Home.page_ib.item7', icon: 'icon-application' },
  234. ],
  235. },
  236. {
  237. isOpenMenu: false,
  238. submenuHeight: 2 * ((40 + 8)) + 8,
  239. path: '/',
  240. label: 'Home.page_ib.item3',
  241. icon: 'crm-newspaper',
  242. children: [
  243. { path: '/pages/ib/report', label: 'Home.page_ib.item3', icon: 'icon-withdrawal' },
  244. { path: '/pages/ib/complexReport', label: 'Home.page_ib.item11', icon: 'icon-withdrawal' },
  245. ],
  246. },
  247. // 推广
  248. // {
  249. // isOpenMenu: false,
  250. // path: '/',
  251. // label: 'Home.page_ib.item12',
  252. // icon: 'crm-bulletin',
  253. // submenuHeight: 1 * ((40 + 8)) + 8,
  254. // children: [
  255. // { path: '/pages/ib/promotion', label: 'Home.page_ib.item13', icon: 'icon-withdrawal' },
  256. // ],
  257. // },
  258. ])
  259. const followBaseMenus = computed<MenuItem[]>(() => [
  260. {
  261. isOpenMenu: false,
  262. path: '/pages/follow/index',
  263. label: 'Documentary.console.item1',
  264. icon: 'crm-mb',
  265. submenuHeight: 0,
  266. },
  267. {
  268. isOpenMenu: false,
  269. path: '/pages/follow/trading-center',
  270. label: 'Documentary.page_doc.item2',
  271. icon: 'crm-gd',
  272. submenuHeight: 0,
  273. },
  274. {
  275. isOpenMenu: false,
  276. submenuHeight: 1 * ((40 + 8)) + 8,
  277. path: '/',
  278. label: 'Documentary.page_doc.item3',
  279. icon: 'crm-newspaper',
  280. children: [
  281. { path: '/pages/follow/report', label: 'Documentary.page_doc.item3', icon: 'icon-client' },
  282. ],
  283. },
  284. {
  285. isOpenMenu: false,
  286. submenuHeight: 2 * ((40 + 8)) + 8,
  287. path: '/',
  288. label: 'Documentary.page_doc.item4',
  289. icon: 'crm-payment',
  290. children: [
  291. { path: '/pages/follow/transfer', label: 'Documentary.TundManagement.item2', icon: 'icon-client' },
  292. { path: '/pages/follow/transfer-history', label: 'Documentary.TundManagement.item3', icon: 'icon-transfer' }
  293. ],
  294. },
  295. {
  296. isOpenMenu: false,
  297. submenuHeight: 5 * ((40 + 8)) + 8,
  298. path: '/',
  299. label: 'Documentary.page_doc.item5',
  300. icon: 'crm-trade',
  301. children: [
  302. { path: '/pages/follow/trading-management', label: 'Documentary.TundManagement.item11', icon: 'icon-client' },
  303. { path: '/pages/follow/follow-list', label: 'Documentary.TundManagement.item17', icon: 'icon-client' },
  304. { path: '/pages/follow/account-management', label: 'Documentary.TundManagement.item9', icon: 'icon-transfer' },
  305. { path: '/pages/follow/subscribe-list', label: 'Documentary.TundManagement.item45', icon: 'icon-transfer' },
  306. { path: '/pages/follow/record', label: 'Documentary.TundManagement.item10', icon: 'icon-transfer' }
  307. ],
  308. },
  309. ])
  310. const menus = ref<MenuItem[]>([])
  311. watch(mode, (newMode, oldMode) => {
  312. if (newMode !== oldMode) {
  313. let base: MenuItem[] = []
  314. switch (newMode) {
  315. case 'follow': base = [...followBaseMenus.value]; break
  316. case 'ib': base = [...ibBaseMenus.value]; break
  317. case 'customer': base = [...customerBaseMenus.value]; break
  318. default: break
  319. }
  320. if (shouldShowLanguageMenu.value) base.push(languageMenuItem.value)
  321. menus.value = cloneMenu(base)
  322. }
  323. }, { immediate: true })
  324. watch(route, () => {
  325. const currentPath = route.path
  326. menus.value.forEach((item, idx) => {
  327. if (item.children?.length) {
  328. const isActive = item.children.some(child => {
  329. if (child.isExternal || child.type === 'lang') return false
  330. return currentPath === child.path || currentPath.startsWith(child.path + '?')
  331. })
  332. if (isActive) item.isOpenMenu = true
  333. }
  334. })
  335. }, { immediate: true })
  336. onMounted(() => { })
  337. return {
  338. menus,
  339. mode,
  340. shouldShowLanguageMenu,
  341. windowWidth,
  342. setMode,
  343. setSubmenuRef,
  344. handleClick,
  345. handleSubmenuClick,
  346. }
  347. }