Index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, setGlobalUrlParams } = 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-50101',
  41. label: '订单挂牌',
  42. icon: 'g-icon-listing--line',
  43. activeIcon: 'g-icon-listing--fill',
  44. },
  45. {
  46. name: 'home-mine',
  47. label: '我的',
  48. icon: 'g-icon-mine--line',
  49. activeIcon: 'g-icon-mine--fill',
  50. }
  51. ]
  52. const onTabClick = (index: number) => {
  53. const { name } = tabList[index]
  54. cssTransition.value = false
  55. if (name === 'home-index' || loginStore.token) {
  56. currentTab.value = index
  57. routerTo(name, true)
  58. } else {
  59. fullloading((hideLoading) => {
  60. userLogin(true).then(() => {
  61. currentTab.value = index
  62. routerTo(name, true)
  63. }).catch(() => {
  64. routerTo('user-login')
  65. }).finally(() => {
  66. hideLoading()
  67. })
  68. }, '加载中...')
  69. }
  70. }
  71. onMounted(() => {
  72. currentTab.value = tabIndex.value
  73. const os = plus.getSystemInfo('os')
  74. const currentVersion = plus.getSystemInfo('version')
  75. const currentVersionCode = plus.getSystemInfo('versionCode')
  76. if (os === 'Android') {
  77. // 监听下载进度
  78. const ondownload = plus.onDownload((filename, progress) => {
  79. if (progress === 100) {
  80. dialog({
  81. message: '新版本下载完成,是否安装?',
  82. showCancelButton: true,
  83. confirmButtonText: '安装'
  84. }).then(() => {
  85. plus.installApp(filename)
  86. }).catch(() => {
  87. plus.deleteFile(filename)
  88. })
  89. }
  90. })
  91. // 获取应用更新信息
  92. getAppUpdateInfo().then((res) => {
  93. const data = JSON.parse(res)
  94. if (data) {
  95. const { LastVersionCode, ApkUrl } = data[0] as Model.AppUpdateInfo
  96. if (Number(LastVersionCode) > Number(currentVersionCode)) {
  97. dialog({
  98. message: '发现新版本,是否下载?',
  99. showCancelButton: true,
  100. confirmButtonText: '下载'
  101. }).then(() => {
  102. plus.createDownload(ApkUrl)
  103. }).catch(() => {
  104. ondownload.cancel()
  105. })
  106. }
  107. }
  108. })
  109. }
  110. if (os === 'iOS') {
  111. // plus.httpRequest({
  112. // url: 'https://itunes.apple.com/lookup?id=1661067556'
  113. // }).then((res) => {
  114. // const results = res.data.results
  115. // if (results?.length) {
  116. // const { version, trackViewUrl } = results[0]
  117. // if (versionToNumber(version) > versionToNumber(currentVersion)) {
  118. // dialog({
  119. // message: '发现新版本,是否更新?',
  120. // showCancelButton: true,
  121. // confirmButtonText: '更新'
  122. // }).then(() => {
  123. // plus.openURL(trackViewUrl)
  124. // })
  125. // }
  126. // }
  127. // })
  128. }
  129. })
  130. watch(() => route.name, () => {
  131. const params = getGlobalUrlParams()
  132. if (params.tabIndex > -1) {
  133. onTabClick(params.tabIndex)
  134. } else if (tabIndex.value > -1) {
  135. onTabClick(tabIndex.value)
  136. } else {
  137. // 如果参数不是 tabIndex,需要保留到下一个路由
  138. setGlobalUrlParams(params)
  139. }
  140. nextTick(() => {
  141. cssTransition.value = true
  142. })
  143. })
  144. </script>
  145. <style lang="less">
  146. @import './index.less';
  147. </style>