Index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class="home g-flex">
  3. <router-view v-slot="{ Component }">
  4. <RouterTransition :css="cssTransition">
  5. <!-- 缓存所有组件 -->
  6. <keep-alive>
  7. <component class="g-flex__body" :is="Component" v-bind="{ marketSection }" />
  8. </keep-alive>
  9. </RouterTransition>
  10. </router-view>
  11. <app-tabbar :data-list="tabList" :data-index="currentTab" @click="onTabClick" />
  12. <app-updater :ios-update-url="iosUpdateUrl" />
  13. </div>
  14. </template>
  15. <script lang="ts" setup>
  16. import { shallowRef, nextTick, watch, onMounted, onActivated, computed } from 'vue'
  17. import { Tabbar } from '@mobile/components/base/tabbar/types'
  18. import { useNavigation } from '@mobile/router/navigation'
  19. import { useMarketSection } from '@/business/market'
  20. import { useLoginStore, useFuturesStore } from '@/stores'
  21. import AppTabbar from '@mobile/components/base/tabbar/index.vue'
  22. import AppUpdater from '@mobile/components/base/updater/index.vue'
  23. import RouterTransition from '@mobile/components/base/router-transition/index.vue'
  24. import { i18n } from '@/stores'
  25. const { global: { t } } = i18n
  26. defineProps({
  27. iosUpdateUrl: String
  28. })
  29. const { route, routerTo } = useNavigation()
  30. const loginStore = useLoginStore()
  31. const futuresStore = useFuturesStore()
  32. const cssTransition = shallowRef(true) // 是否使用css动画
  33. const currentTab = shallowRef(0)
  34. const marketSection = shallowRef() // 当前选中的板块
  35. const tabIndex = computed(() => {
  36. const value = window.sessionStorage.getItem('currentTab')
  37. if (value) {
  38. const parsedValue = JSON.parse(value)
  39. if (parsedValue.name === route.name) {
  40. return parsedValue.index
  41. }
  42. }
  43. return tabList.value.findIndex((e) => e.name === route.name)
  44. })
  45. // 获取市场板块
  46. const { allMarket } = useMarketSection(() => {
  47. const tab = tabList.value[tabIndex.value]
  48. marketSection.value = tab?.params
  49. currentTab.value = tabIndex.value
  50. })
  51. // 动态添加导航标签页
  52. const addTab = (item: Model.Marketsectionconfignew) => {
  53. const tab: Tabbar = {
  54. name: 'home-404',
  55. label: item.displayname,
  56. icon: 'g-icon-listing--line',
  57. activeIcon: 'g-icon-listing--fill',
  58. params: item
  59. }
  60. switch (item.trademode) {
  61. case 10:
  62. tab.name = 'home-pricing'
  63. tab.icon = 'g-icon-pricing--line'
  64. tab.activeIcon = 'g-icon-pricing--fill'
  65. break
  66. case 17:
  67. tab.name = 'home-spot'
  68. tab.icon = 'g-icon-spot--line'
  69. tab.activeIcon = 'g-icon-spot--fill'
  70. break
  71. case 46:
  72. tab.name = 'home-swap'
  73. tab.icon = 'g-icon-swap--line'
  74. tab.activeIcon = 'g-icon-swap--fill'
  75. break
  76. case 48:
  77. tab.name = 'home-presale'
  78. tab.icon = 'g-icon-presale--line'
  79. tab.activeIcon = 'g-icon-presale--fill'
  80. break
  81. case 49:
  82. tab.name = 'home-transfer'
  83. tab.icon = 'g-icon-transfer--line'
  84. tab.activeIcon = 'g-icon-transfer--fill'
  85. break
  86. case 16:
  87. case 50:
  88. tab.name = 'home-goods'
  89. tab.icon = 'g-icon-listing--line'
  90. tab.activeIcon = 'g-icon-listing--fill'
  91. break
  92. case 51:
  93. tab.name = 'home-ballot'
  94. tab.icon = 'g-icon-presale--line'
  95. tab.activeIcon = 'g-icon-presale--fill'
  96. break
  97. case 99:
  98. tab.name = 'home-market'
  99. tab.icon = 'g-icon-quote--line'
  100. tab.activeIcon = 'g-icon-quote--fill'
  101. break
  102. }
  103. return tab
  104. }
  105. // 导航标签列表
  106. const tabList = computed(() => {
  107. const result: Tabbar[] = [
  108. {
  109. name: 'home-index',
  110. label: t('tabbar.home'),
  111. icon: 'g-icon-home--line',
  112. activeIcon: 'g-icon-home--fill',
  113. },
  114. {
  115. name: 'home-mine',
  116. label: t('tabbar.mine'),
  117. icon: 'g-icon-mine--line',
  118. activeIcon: 'g-icon-mine--fill',
  119. }
  120. ]
  121. if (loginStore.token) {
  122. allMarket.value.forEach((item, index) => {
  123. const startIndex = index + 1
  124. if (startIndex < 4) {
  125. result.splice(startIndex, 0, addTab(item))
  126. }
  127. })
  128. } else {
  129. result.splice(1, 0, {
  130. name: '#',
  131. label: t('tabbar.trade'),
  132. icon: 'g-icon-quote--line',
  133. activeIcon: 'g-icon-quote--fill',
  134. })
  135. }
  136. return result
  137. })
  138. const onTabClick = (index: number) => {
  139. const { name, params } = tabList.value[index]
  140. marketSection.value = params
  141. if (index === 0 || loginStore.token) {
  142. // 缓存当前选中的标签位置,防止 F5 刷新页面后无法定位到当前标签
  143. window.sessionStorage.setItem('currentTab', JSON.stringify({
  144. index,
  145. name
  146. }))
  147. currentTab.value = index
  148. routerTo(name, true)
  149. } else {
  150. window.sessionStorage.removeItem('currentTab')
  151. routerTo('user-login')
  152. }
  153. }
  154. watch(() => route.name, () => {
  155. if (tabIndex.value > -1) {
  156. cssTransition.value = false
  157. currentTab.value = tabIndex.value
  158. }
  159. nextTick(() => {
  160. cssTransition.value = true
  161. })
  162. })
  163. onMounted(() => {
  164. currentTab.value = tabIndex.value
  165. })
  166. onActivated(() => {
  167. // 页面显示时刷新盘面(待优化)
  168. if (loginStore.token && futuresStore.goodsList.length) {
  169. futuresStore.getQuoteDay()
  170. }
  171. })
  172. </script>
  173. <style lang="less">
  174. @import './index.less';
  175. </style>