Index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 } 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 tabList: Tabbar[] = [
  32. {
  33. name: 'home-index',
  34. label: '首页',
  35. icon: 'g-icon-home--line',
  36. activeIcon: 'g-icon-home--fill',
  37. },
  38. {
  39. name: 'home-50102',
  40. label: '订单挂牌',
  41. icon: 'g-icon-listing--line',
  42. activeIcon: 'g-icon-listing--fill',
  43. },
  44. {
  45. name: 'home-spot',
  46. label: '现货挂牌',
  47. icon: 'g-icon-spot--line',
  48. activeIcon: 'g-icon-spot--fill',
  49. },
  50. {
  51. name: 'home-50101',
  52. label: '协议转让',
  53. icon: 'g-icon-transfer--line',
  54. activeIcon: 'g-icon-transfer--fill',
  55. },
  56. {
  57. name: 'home-mine',
  58. label: '我的',
  59. icon: 'g-icon-mine--line',
  60. activeIcon: 'g-icon-mine--fill',
  61. }
  62. ]
  63. const onTabClick = (index: number) => {
  64. const { name } = tabList[index]
  65. cssTransition.value = false
  66. if (name === 'home-index' || loginStore.token) {
  67. currentTab.value = index
  68. routerTo(name, true)
  69. } else {
  70. fullloading((hideLoading) => {
  71. userLogin(true).then(() => {
  72. currentTab.value = index
  73. routerTo(name, true)
  74. }).catch(() => {
  75. routerTo('user-login')
  76. }).finally(() => {
  77. hideLoading()
  78. })
  79. }, '加载中...')
  80. }
  81. }
  82. onMounted(() => {
  83. const { tabIndex } = getGlobalUrlParams()
  84. currentTab.value = tabIndex > -1 ? tabIndex : tabList.findIndex((e) => e.name === route.name)
  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=6459789608'
  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 { tabIndex } = getGlobalUrlParams()
  144. if (tabIndex > -1) {
  145. onTabClick(tabIndex)
  146. } else {
  147. nextTick(() => {
  148. cssTransition.value = true
  149. })
  150. }
  151. })
  152. </script>
  153. <style lang="less">
  154. @import './index.less';
  155. </style>