Index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. </div>
  13. </template>
  14. <script lang="ts" setup>
  15. import { shallowRef, nextTick, watch, onMounted ,computed} from 'vue'
  16. import { fullloading, dialog } from '@/utils/vant'
  17. import { versionToNumber } from '@/filters'
  18. import { Tabbar } from '@mobile/components/base/tabbar/types'
  19. import { getAppUpdateInfo } from '@/services/api/common'
  20. import { useNavigation } from '@mobile/router/navigation'
  21. import { useLogin } from '@/business/login'
  22. import { useLoginStore } from '@/stores'
  23. import plus from '@/utils/h5plus'
  24. import AppTabbar from '@mobile/components/base/tabbar/index.vue'
  25. import RouterTransition from '@mobile/components/base/router-transition/index.vue'
  26. const { route, routerTo, getGlobalUrlParams } = useNavigation()
  27. const { userLogin } = useLogin()
  28. const loginStore = useLoginStore()
  29. const cssTransition = shallowRef(true) // 是否使用css动画
  30. const currentTab = shallowRef(0)
  31. const tabIndex = computed(() => tabList.findIndex((e) => e.name === route.name))
  32. const tabList: Tabbar[] = [
  33. {
  34. name: 'home-index',
  35. label: '首页',
  36. icon: 'g-icon-home--line',
  37. activeIcon: 'g-icon-home--fill',
  38. },
  39. {
  40. name: 'home-ballot',
  41. label: '新品发布',
  42. icon: 'g-icon-presale--line',
  43. activeIcon: 'g-icon-presale--fill',
  44. },
  45. {
  46. name: 'home-transfer',
  47. label: '定金转让',
  48. icon: 'g-icon-transfer--line',
  49. activeIcon: 'g-icon-transfer--fill',
  50. },
  51. {
  52. name: 'home-50101',
  53. label: '日清挂牌',
  54. icon: 'g-icon-listing--line',
  55. activeIcon: 'g-icon-listing--fill',
  56. },
  57. {
  58. name: 'home-mine',
  59. label: '我的',
  60. icon: 'g-icon-mine--line',
  61. activeIcon: 'g-icon-mine--fill',
  62. }
  63. ]
  64. const onTabClick = (index: number) => {
  65. const { name } = tabList[index]
  66. cssTransition.value = false
  67. if (name === 'home-index' || loginStore.token) {
  68. currentTab.value = index
  69. routerTo(name, true)
  70. } else {
  71. fullloading((hideLoading) => {
  72. userLogin(true).then(() => {
  73. currentTab.value = index
  74. routerTo(name, true)
  75. }).catch(() => {
  76. routerTo('user-login')
  77. }).finally(() => {
  78. hideLoading()
  79. })
  80. }, '加载中...')
  81. }
  82. }
  83. onMounted(() => {
  84. currentTab.value = tabIndex.value
  85. const os = plus.getSystemInfo('os')
  86. const currentVersion = plus.getSystemInfo('version')
  87. const currentVersionCode = plus.getSystemInfo('versionCode')
  88. if (os === 'Android') {
  89. // 监听下载进度
  90. const ondownload = plus.onDownload((filename, progress) => {
  91. if (progress === 100) {
  92. dialog({
  93. message: '新版本下载完成,是否安装?',
  94. showCancelButton: true,
  95. confirmButtonText: '安装'
  96. }).then(() => {
  97. plus.installApp(filename)
  98. }).catch(() => {
  99. plus.deleteFile(filename)
  100. })
  101. }
  102. })
  103. // 获取应用更新信息
  104. getAppUpdateInfo().then((res) => {
  105. const data = JSON.parse(res)
  106. if (data) {
  107. const { LastVersionCode, ApkUrl } = data[0] as Model.AppUpdateInfo
  108. if (Number(LastVersionCode) > Number(currentVersionCode)) {
  109. dialog({
  110. message: '发现新版本,是否下载?',
  111. showCancelButton: true,
  112. confirmButtonText: '下载'
  113. }).then(() => {
  114. plus.createDownload(ApkUrl)
  115. }).catch(() => {
  116. ondownload.cancel()
  117. })
  118. }
  119. }
  120. })
  121. }
  122. if (os === 'iOS') {
  123. // plus.httpRequest({
  124. // url: 'https://itunes.apple.com/lookup?id=1661067556'
  125. // }).then((res) => {
  126. // const results = res.data.results
  127. // if (results?.length) {
  128. // const { version, trackViewUrl } = results[0]
  129. // if (versionToNumber(version) > versionToNumber(currentVersion)) {
  130. // dialog({
  131. // message: '发现新版本,是否更新?',
  132. // showCancelButton: true,
  133. // confirmButtonText: '更新'
  134. // }).then(() => {
  135. // plus.openURL(trackViewUrl)
  136. // })
  137. // }
  138. // }
  139. // })
  140. }
  141. })
  142. watch(() => route.name, () => {
  143. const params = getGlobalUrlParams()
  144. if (params.tabIndex > -1) {
  145. onTabClick(params.tabIndex)
  146. } else if (tabIndex.value > -1) {
  147. onTabClick(tabIndex.value)
  148. } else {
  149. nextTick(() => {
  150. cssTransition.value = true
  151. })
  152. }
  153. })
  154. </script>
  155. <style lang="less">
  156. @import './index.less';
  157. </style>