index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <view class="hc-update-app-box" v-if="isShow">
  3. <view class="update-head-bar">
  4. <view class="head-bar">
  5. <image class="head-img" src="/static/update/1.png"/>
  6. <view class="relative flex">
  7. <view class="flex-1 text-white hc-p">
  8. <view class="text-34">发现新版本</view>
  9. <view class="mt-2">v{{versionData.versionNumber}}</view>
  10. </view>
  11. <view class="relative top--60 right-40">
  12. <c-lottie src='/static/update/2.json' width="240rpx" height='240rpx' :loop="true"/>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="update-content-bar hc-p">
  17. <scroll-view scroll-y>
  18. <view class="text-gray-5 text-26">{{versionData.updateDate}}</view>
  19. <view class="mt-1">本次更新升级内容:</view>
  20. <view class="mt-3 text-gray-6" style="white-space: pre;">{{versionData.updateContent}}</view>
  21. </scroll-view>
  22. </view>
  23. <view class="update-action-bar">
  24. <view class="relative hc-p" v-if="isDownload">
  25. <view class="hc-flex justify-between mb-1">
  26. <view class="text-24 text-gray-5">已下载:{{downloaded}}</view>
  27. <view class="text-24 text-gray-5">总大小:{{totalFileSize}}</view>
  28. </view>
  29. <uv-line-progress :percentage="downloadNum"/>
  30. </view>
  31. <template v-else>
  32. <view class="hc-flex hc-p" v-if="versionData.constraintUpdate !== 1">
  33. <button class="cu-btn bg-blue-5 text-white flex-1 mr-2" @click="updateClick">立即更新</button>
  34. <button class="cu-btn bg-gray-4 text-white flex-1 ml-2" @click="nextTap">下次再说</button>
  35. </view>
  36. <view class="hc-flex hc-p" v-else>
  37. <button class="cu-btn bg-blue-5 text-white flex-1" @click="updateClick">立即更新</button>
  38. </view>
  39. </template>
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script setup>
  45. import {onMounted, ref, watch} from "vue";
  46. import {getVersionData} from "~api/other";
  47. import website from '@/config/index';
  48. import {getObjValue} from "js-fast-way";
  49. import {filterSize} from "@/utils/utils";
  50. import {errorToast, successToast} from "@/utils/tools";
  51. import {useAppStore} from "@/store";
  52. //初始变量
  53. const store = useAppStore()
  54. const isShow = ref(false)
  55. const onUpdate = ref(store.onUpdate)
  56. const appUpdate = ref(store.appUpdate)
  57. const appInfo = ref({osName: '', version: ''})
  58. //渲染完成
  59. onMounted(() => {
  60. const {appVersion, appWgtVersion, osName} = uni.getSystemInfoSync();
  61. let version = appVersion
  62. if (appVersion === appWgtVersion) {
  63. version = appVersion
  64. } else if (!appVersion && appWgtVersion) {
  65. version = appWgtVersion
  66. } else if (appVersion && !appWgtVersion) {
  67. version = appVersion
  68. } else if (appVersion > appWgtVersion) {
  69. version = appVersion
  70. } else if (appVersion < appWgtVersion) {
  71. version = appWgtVersion
  72. }
  73. appInfo.value = {osName: osName, version: version}
  74. appUpdate.value.appVersion = version
  75. getVersionDataApi()
  76. })
  77. //监听
  78. watch(() => [
  79. store.onUpdate
  80. ], ([val]) => {
  81. if (val) {
  82. const { version } = appInfo.value
  83. const { versionNumber } = versionData.value
  84. if (versionNumber > version) {
  85. isShow.value = true
  86. }
  87. }
  88. })
  89. //获取新版本信息
  90. const versionData = ref({})
  91. const getVersionDataApi = async () => {
  92. const { osName, version } = appInfo.value
  93. const type = osName === 'ios' ? 2 : 1
  94. const { data } = await getVersionData({
  95. softwareType: type,
  96. platform: website.platform
  97. })
  98. const res = getObjValue(data)
  99. versionData.value = res
  100. appUpdate.value.version = res.versionNumber
  101. const toUpdate = appUpdate.value.toUpdate
  102. if (res.versionNumber > version) {
  103. appUpdate.value.isUpdate = true
  104. isShow.value = toUpdate
  105. } else {
  106. appUpdate.value.isUpdate = false
  107. appUpdate.value.toUpdate = false
  108. }
  109. store.setAppUpdate(appUpdate.value)
  110. }
  111. //立即强制更新
  112. const isDownload = ref(false)
  113. const updateClick = async () => {
  114. const {osName} = appInfo.value
  115. const {fileType, fileUrl} = versionData.value
  116. //ios的完整安装包
  117. if (osName === 'ios' && fileType === 0) {
  118. plus.runtime.openURL(fileUrl)
  119. return false
  120. }
  121. //其他类型,安卓、ios的热更新包
  122. downloadFile(fileUrl)
  123. }
  124. //下载相关文件资料
  125. const downloadNum = ref(0)
  126. const totalFileSize = ref('')
  127. const downloaded = ref('')
  128. const downloadFile = (fileUrl) => {
  129. isDownload.value = true
  130. const downloadTask = uni.downloadFile({
  131. url: fileUrl,
  132. success: ({statusCode, tempFilePath}) => {
  133. if (statusCode === 200) {
  134. installAppFile(tempFilePath)
  135. } else {
  136. errorToast('下载失败,请联系管理员')
  137. nextTap()
  138. }
  139. },
  140. fail: () => {
  141. errorToast('下载失败,请联系管理员')
  142. nextTap()
  143. }
  144. });
  145. downloadTask.onProgressUpdate((res) => {
  146. const {totalBytesExpectedToWrite, totalBytesWritten, progress} = getObjValue(res)
  147. totalFileSize.value = filterSize(totalBytesExpectedToWrite)
  148. downloadNum.value = progress
  149. downloaded.value = filterSize(totalBytesWritten)
  150. });
  151. }
  152. //安装相关资源文件
  153. const installAppFile = (file) => {
  154. //0 完整安装包, 1 wgt热更新包
  155. const {fileType} = versionData.value
  156. plus.runtime.install(file, {
  157. force: false
  158. }, function() {
  159. successToast('本次升级成功')
  160. setTimeout(() => {
  161. plus.runtime.restart();
  162. }, 1500)
  163. }, function(e) {
  164. console.log(e)
  165. errorToast('本次升级失败,请联系管理员')
  166. nextTap()
  167. });
  168. }
  169. //下次再说
  170. const nextTap = () => {
  171. appUpdate.value.toUpdate = false
  172. store.setAppUpdate(appUpdate.value)
  173. isShow.value = false
  174. isDownload.value = false
  175. }
  176. </script>
  177. <style scoped lang="scss">
  178. @import "./style.scss";
  179. </style>