index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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" />
  8. </keep-alive>
  9. </RouterTransition>
  10. </router-view>
  11. <app-tabbar class="home-tabbar" :data-list="tabList" :data-index="currentTab" @click="onTabClick" />
  12. <app-updater ios-update-url="https://itunes.apple.com/lookup?id=1661067556" />
  13. </div>
  14. </template>
  15. <script lang="ts" setup>
  16. import { shallowRef, nextTick, watch, onMounted, computed } from 'vue'
  17. import { fullloading } from '@/utils/vant'
  18. import { Tabbar } from '@mobile/components/base/tabbar/interface'
  19. import { useNavigation } from '@/hooks/navigation'
  20. import { useLogin } from '@/business/login'
  21. import { useLoginStore } from '@/stores'
  22. import AppTabbar from '@mobile/components/base/tabbar/index.vue'
  23. import AppUpdater from '@mobile/components/base/updater/index.vue'
  24. import RouterTransition from '@mobile/components/base/router-transition/index.vue'
  25. const { route, routerTo } = useNavigation()
  26. const { userLogin } = useLogin()
  27. const loginStore = useLoginStore()
  28. const cssTransition = shallowRef(true) // 是否使用css动画
  29. const currentTab = shallowRef(0)
  30. const tabIndex = computed(() => tabList.findIndex((e) => e.name === route.name))
  31. const tabList: Tabbar[] = [
  32. {
  33. name: 'home-index',
  34. label: '首页',
  35. icon: 'icon-home',
  36. activeIcon: 'icon-home-active',
  37. },
  38. {
  39. name: 'home-purchase',
  40. label: '采购',
  41. icon: 'icon-purchase',
  42. activeIcon: 'icon-purchase-active',
  43. },
  44. {
  45. name: 'home-mine',
  46. label: '我的',
  47. icon: 'icon-mine',
  48. activeIcon: 'icon-mine-active',
  49. }
  50. ]
  51. const onTabClick = (index: number) => {
  52. const { name } = tabList[index]
  53. if (index === 0 || loginStore.token) {
  54. routerTo(name, true)
  55. } else {
  56. fullloading((hideLoading) => {
  57. userLogin(true).then(() => {
  58. routerTo(name, true)
  59. }).catch(() => {
  60. routerTo('UserLogin')
  61. }).finally(() => {
  62. hideLoading()
  63. })
  64. }, '加载中...')
  65. }
  66. }
  67. watch(() => route.name, () => {
  68. if (tabIndex.value > -1) {
  69. cssTransition.value = false
  70. currentTab.value = tabIndex.value
  71. }
  72. nextTick(() => {
  73. cssTransition.value = true
  74. })
  75. })
  76. onMounted(() => {
  77. currentTab.value = tabIndex.value
  78. })
  79. </script>
  80. <style lang="less">
  81. @import './index.less';
  82. </style>