index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <Dialog teleport="body" class="app-updater" v-model:show="show" :title="message ? '提示' : '下载中'"
  3. :show-confirm-button="showConfirmButton" :confirm-button-text="confirmButtonText" show-cancel-button
  4. :before-close="onbeforeClose" @confirm="onConfirm" @cancel="onCancel">
  5. <div class="app-updater-message" v-if="message">
  6. <span>{{ message }}</span>
  7. </div>
  8. <div class="app-updater-progress" v-else>
  9. <span class="app-updater-progress__bar" :style="styles"></span>
  10. <span class="app-updater-progress__text">{{ downloadProgress.toFixed(0) }}%</span>
  11. </div>
  12. </Dialog>
  13. </template>
  14. <script lang="ts" setup>
  15. import { shallowRef, computed, onMounted, onUnmounted } from 'vue'
  16. import { Dialog } from 'vant'
  17. import { versionToNumber } from '@/filters'
  18. import { GetAppUpdateInfo } from '@/services/api/common'
  19. import plus from '@/utils/h5plus'
  20. const props = defineProps({
  21. iosUpdateUrl: String
  22. })
  23. const show = shallowRef(false)
  24. const pending = shallowRef(true) // 是否进行中状态
  25. const showConfirmButton = shallowRef(true)
  26. const message = shallowRef('')
  27. const confirmButtonText = shallowRef('')
  28. const downloadUrl = shallowRef('') // 下载地址
  29. const downloadProgress = shallowRef(0) // 下载进度
  30. const fileUrl = shallowRef('') // 文件安装地址
  31. const styles = computed(() => ({
  32. width: downloadProgress.value + '%'
  33. }))
  34. // 监听下载进度
  35. const ondownload = plus.onDownloadProgress((progress) => {
  36. downloadProgress.value = progress
  37. })
  38. const onbeforeClose = () => {
  39. return false
  40. }
  41. const onConfirm = () => {
  42. const os = plus.getSystemInfo('os')
  43. if (os === 'iOS') {
  44. show.value = false
  45. plus.openURL(downloadUrl.value)
  46. } else if (downloadProgress.value === 100) {
  47. show.value = false
  48. plus.installApp(fileUrl.value)
  49. } else if (confirmButtonText.value === '隐藏') {
  50. show.value = false
  51. } else {
  52. message.value = ''
  53. confirmButtonText.value = '隐藏'
  54. plus.createDownload(downloadUrl.value).then((res) => {
  55. fileUrl.value = res.filename
  56. message.value = '下载完成,是否立即安装?'
  57. confirmButtonText.value = '安装'
  58. }).catch((err) => {
  59. message.value = err
  60. showConfirmButton.value = false
  61. }).finally(() => {
  62. show.value = pending.value
  63. })
  64. }
  65. }
  66. const onCancel = () => {
  67. if (fileUrl.value) {
  68. plus.deleteFile(fileUrl.value)
  69. }
  70. ondownload.cancel()
  71. pending.value = false
  72. show.value = false
  73. }
  74. onMounted(() => {
  75. const os = plus.getSystemInfo('os')
  76. const currentVersion = plus.getSystemInfo('version')
  77. const currentVersionCode = plus.getSystemInfo('versionCode')
  78. if (os === 'Android') {
  79. // 获取应用更新信息
  80. GetAppUpdateInfo().then((res) => {
  81. const data = JSON.parse(res)
  82. if (data) {
  83. const { LastVersionCode, ApkUrl } = data[0] as Model.AppUpdateInfo
  84. if (Number(LastVersionCode) > Number(currentVersionCode)) {
  85. downloadUrl.value = ApkUrl
  86. message.value = '发现新版本,是否更新?'
  87. confirmButtonText.value = '下载'
  88. show.value = true
  89. }
  90. }
  91. })
  92. }
  93. if (os === 'iOS' && props.iosUpdateUrl) {
  94. plus.httpRequest({
  95. url: props.iosUpdateUrl
  96. }).then((res) => {
  97. const results = res.data.results
  98. if (results?.length) {
  99. const { version, trackViewUrl } = results[0]
  100. if (versionToNumber(version) > versionToNumber(currentVersion)) {
  101. downloadUrl.value = trackViewUrl
  102. message.value = '发现新版本,是否更新?'
  103. confirmButtonText.value = '更新'
  104. show.value = true
  105. }
  106. }
  107. })
  108. }
  109. })
  110. onUnmounted(() => ondownload.cancel())
  111. </script>
  112. <style lang="less">
  113. @import './index.less';
  114. </style>