vUpgrade.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view class="upgrade-mask d-flex align-center justify-center" v-if="show">
  3. <view class="upgrade-box w-8/12 bg-panel-4 p-t-md rounded">
  4. <!-- 进度条 -->
  5. <view class="progress d-flex justify-between p-x-md" :class="{isDwonload:isDwonload}">
  6. <view
  7. class="item w-5 h-20"
  8. v-for="item in 20"
  9. :class="{active:progress/20>=item}"
  10. :key="item"
  11. :style="{'animation-delay':item*.1+'s'}"
  12. ></view>
  13. </view>
  14. <view class="title fn-center p-y-xs color-warning-dark">V{{version}}</view>
  15. <view class="overflow-scroll bg-panel-1 m-x-md content" v-html="serverData.update_log">
  16. </view>
  17. <view class="d-flex fn-center btn-group" v-if="!isDwonload">
  18. <view class="btn flex-fill p-md" @click="show=false">取消</view>
  19. <view class="btn flex-fill p-md color-warning-dark" @click="downloadApp">升级</view>
  20. </view>
  21. <view class="d-flex justify-center p-y-xs" v-else>
  22. <van-loading />
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import Member from "@/api/member";
  29. export default {
  30. name: "v-upgrade",
  31. data() {
  32. return {
  33. currentVersion: "1.0.0",
  34. version: "1.0.0",
  35. progress: 0,
  36. isDwonload: false,
  37. show: false,
  38. serverData: {},
  39. };
  40. },
  41. methods: {
  42. // 获取app版本
  43. getAppVersion() {
  44. plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
  45. this.currentVersion = widgetInfo.version;
  46. this.getServeVersion();
  47. });
  48. },
  49. // 获取服务器app版本
  50. getServeVersion() {
  51. Member.getNewestVersion().then((res) => {
  52. let key = "android";
  53. if (plus.os.name == "Android") {
  54. key = "android";
  55. } else if (plus.os.name == "iOS") {
  56. key = "ios";
  57. }
  58. this.serverData = res.data[key];
  59. this.version = this.serverData.version;
  60. this.upgradeType();
  61. });
  62. },
  63. // 判断是否下载和更新方式
  64. upgradeType() {
  65. if (this.version != this.currentVersion) {
  66. this.show = true;
  67. }
  68. },
  69. // 下载app或wgt
  70. downloadApp() {
  71. this.isDwonload = true;
  72. const downloadTask = uni.downloadFile({
  73. url: this.serverData.url, //仅为示例,并非真实的资源
  74. success: (downloadResult) => {
  75. if (downloadResult.statusCode === 200) {
  76. this.progress = 100;
  77. this.appInstall(downloadResult);
  78. }
  79. },
  80. });
  81. downloadTask.onProgressUpdate((res) => {
  82. if (this.progress >= 100) return;
  83. this.progress = res.progress;
  84. });
  85. },
  86. // 热更新
  87. wgtInstall() {},
  88. // 全包更新
  89. appInstall(downloadResult) {
  90. plus.runtime.install(
  91. downloadResult.tempFilePath,
  92. {
  93. force: false,
  94. },
  95. () => {
  96. console.log("install success...");
  97. plus.runtime.restart();
  98. },
  99. (e) => {
  100. console.error("install fail...");
  101. }
  102. );
  103. },
  104. },
  105. created() {
  106. // #ifdef APP-PLUS
  107. this.getAppVersion();
  108. // #endif
  109. },
  110. };
  111. </script>
  112. <style lang="scss" scoped>
  113. .upgrade-mask {
  114. position: fixed;
  115. top: 0;
  116. left: 0;
  117. bottom: 0;
  118. right: 0;
  119. display: flex;
  120. z-index: 999;
  121. background: rgba(0, 0, 0, 0.5);
  122. animation: pageShow 0.5s forwards;
  123. .progress {
  124. transform: skewX(-15deg);
  125. .item {
  126. background-color: transparent;
  127. border: 1px solid $orange-dark;
  128. &.active {
  129. background-color: $orange-dark;
  130. }
  131. }
  132. &.isDwonload {
  133. .item {
  134. animation: myPing 1s ease-in-out 1s infinite alternate;
  135. }
  136. }
  137. }
  138. .content {
  139. height: 150px;
  140. }
  141. .btn-group {
  142. .btn:active {
  143. background: rgba($gray-4, 0.1);
  144. }
  145. }
  146. }
  147. @keyframes myPing {
  148. 0% {
  149. transform: scale(1);
  150. }
  151. 50% {
  152. transform: scale(1);
  153. }
  154. 70% {
  155. transform: scale(1);
  156. }
  157. 100% {
  158. transform: scale(1.1);
  159. }
  160. }
  161. @keyframes pageShow {
  162. 0% {
  163. transform: translateY(100%);
  164. opacity: 0;
  165. }
  166. 100% {
  167. transform: translateY(0);
  168. opacity: 1;
  169. }
  170. }
  171. </style>