index.vue 6.7 KB

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