index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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} 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. getVersionDataApi()
  83. }
  84. })
  85. //获取新版本信息
  86. const versionData = ref({})
  87. const getVersionDataApi = async () => {
  88. const { osName, version } = appInfo.value
  89. const type = osName === 'ios' ? 2 : 1
  90. const { data } = await getVersionData({
  91. softwareType: type,
  92. platform: website.platform
  93. })
  94. const res = getObjValue(data)
  95. versionData.value = res
  96. appUpdate.value.version = res.versionNumber
  97. const toUpdate = appUpdate.value.toUpdate
  98. if (res.versionNumber > version) {
  99. appUpdate.value.isUpdate = true
  100. isShow.value = toUpdate
  101. } else {
  102. appUpdate.value.isUpdate = false
  103. appUpdate.value.toUpdate = false
  104. }
  105. store.setAppUpdate(appUpdate.value)
  106. }
  107. //立即强制更新
  108. const isDownload = ref(false)
  109. const updateClick = async () => {
  110. const {osName} = appInfo.value
  111. const {fileType, fileUrl} = versionData.value
  112. //ios的完整安装包
  113. if (osName === 'ios' && fileType === 0) {
  114. plus.runtime.openURL(fileUrl)
  115. return false
  116. }
  117. //其他类型,安卓、ios的热更新包
  118. downloadFile(fileUrl)
  119. }
  120. //下载相关文件资料
  121. const downloadNum = ref(0)
  122. const totalFileSize = ref('')
  123. const downloaded = ref('')
  124. const downloadFile = (fileUrl) => {
  125. isDownload.value = true
  126. const downloadTask = uni.downloadFile({
  127. url: fileUrl,
  128. success: ({statusCode, tempFilePath}) => {
  129. if (statusCode === 200) {
  130. installAppFile(tempFilePath)
  131. } else {
  132. errorToast('下载失败,请联系管理员')
  133. nextTap()
  134. }
  135. },
  136. fail: () => {
  137. errorToast('下载失败,请联系管理员')
  138. nextTap()
  139. }
  140. });
  141. downloadTask.onProgressUpdate((res) => {
  142. const {totalBytesExpectedToWrite, totalBytesWritten, progress} = getObjValue(res)
  143. totalFileSize.value = filterSize(totalBytesExpectedToWrite)
  144. downloadNum.value = progress
  145. downloaded.value = filterSize(totalBytesWritten)
  146. });
  147. }
  148. //安装相关资源文件
  149. const installAppFile = (file) => {
  150. plus.runtime.install(file, {
  151. force: false
  152. }, function() {
  153. plus.runtime.restart();
  154. }, function(e) {
  155. console.log(e)
  156. errorToast('本次升级失败,请联系管理员')
  157. nextTap()
  158. });
  159. }
  160. //下次再说
  161. const nextTap = () => {
  162. appUpdate.value.toUpdate = false
  163. store.setAppUpdate(appUpdate.value)
  164. isShow.value = false
  165. isDownload.value = false
  166. }
  167. </script>
  168. <style scoped lang="scss">
  169. @import "./style.scss";
  170. </style>