index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="app-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" />
  8. </keep-alive>
  9. </RouterTransition>
  10. </router-view>
  11. <app-tabbar :data-list="tabs" :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, onUnmounted, computed, PropType } from 'vue'
  17. import { closeToast } from 'vant'
  18. import { showLoading, fullloading } from '@/utils/vant'
  19. import { Tabbar } from '@mobile/components/base/tabbar/types'
  20. import { useNavigation } from '@mobile/router/navigation'
  21. import { useLogin } from '@/business/login'
  22. import { useLoginStore } from '@/stores'
  23. import eventBus from '@/services/bus'
  24. import AppTabbar from '@mobile/components/base/tabbar/index.vue'
  25. import AppUpdater from '@mobile/components/base/updater/index.vue'
  26. import RouterTransition from '@mobile/components/base/router-transition/index.vue'
  27. const props = defineProps({
  28. tabs: {
  29. type: Array as PropType<Tabbar[]>,
  30. required: true
  31. },
  32. iosUpdateUrl: String
  33. })
  34. const { route, routerTo, getGlobalUrlParams, setGlobalUrlParams } = useNavigation()
  35. const { userLogin } = useLogin()
  36. const loginStore = useLoginStore()
  37. const cssTransition = shallowRef(true) // 是否使用css动画
  38. const currentTab = shallowRef(0)
  39. const tabIndex = computed(() => props.tabs.findIndex((e) => e.name === route.name))
  40. const onTabClick = (index: number) => {
  41. const { name } = props.tabs[index]
  42. cssTransition.value = false
  43. if (name === 'home-index' || loginStore.token) {
  44. currentTab.value = index
  45. routerTo(name, true)
  46. } else {
  47. fullloading((hideLoading) => {
  48. userLogin(true).then(() => {
  49. currentTab.value = index
  50. routerTo(name, true)
  51. }).catch(() => {
  52. cssTransition.value = true
  53. routerTo('user-login')
  54. }).finally(() => {
  55. hideLoading()
  56. })
  57. }, '加载中...')
  58. }
  59. }
  60. // 接收交易服务重连通知
  61. const tradeServerReconnectNotify = eventBus.$on('TradeServerReconnectNotify', () => {
  62. showLoading('服务连接中...')
  63. })
  64. // 接收交易服务重连成功通知
  65. const tradeServerReconnectSuccessNotify = eventBus.$on('TradeServerReconnectSuccessNotify', () => {
  66. closeToast()
  67. })
  68. watch(() => route.name, () => {
  69. const params = getGlobalUrlParams()
  70. if (params.tabIndex > -1) {
  71. onTabClick(params.tabIndex)
  72. } else {
  73. // 如果参数不是 tabIndex ,需要保留到下一个路由
  74. setGlobalUrlParams(params)
  75. }
  76. nextTick(() => {
  77. cssTransition.value = true
  78. })
  79. })
  80. onMounted(() => {
  81. // 页面初始化时判断底部导航位置
  82. const params = getGlobalUrlParams()
  83. if (params.tabIndex > -1) {
  84. onTabClick(params.tabIndex)
  85. } else {
  86. currentTab.value = tabIndex.value
  87. }
  88. })
  89. onUnmounted(() => {
  90. tradeServerReconnectNotify.cancel()
  91. tradeServerReconnectSuccessNotify.cancel()
  92. })
  93. </script>
  94. <style lang="less">
  95. @import './index.less';
  96. </style>