Index.vue 5.5 KB

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