downLoad.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="content">
  3. <view class="main">
  4. <view class="main-bg"><image src="../../static/img/download.png" mode=""></image></view>
  5. <view class="title">软件更新</view>
  6. <view class="jdt">
  7. <u-line-progress :show-percent="false" :percent="percent" :striped-active="true" :striped="true" inactive-color="#f5957d" active-color="#fd6f3f"></u-line-progress>
  8. </view>
  9. <view class="num">{{ percent }}%</view>
  10. <view class="tipSpan">软件更新中,请勿断开相框电源</view>
  11. <!-- <view class="btn" @click="cancelDown()">取消更新</view> -->
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. dtask: '',
  20. percent: 0
  21. };
  22. },
  23. onLoad(opt) {
  24. this.downApp(opt.apk);
  25. },
  26. onShow() {},
  27. onReachBottom() {},
  28. onReady() {},
  29. methods: {
  30. downApp(version) {
  31. if(!(version.indexOf("http")>-1)){
  32. version = this.$store.state.baseURL+version
  33. }
  34. this.dtask = plus.downloader.createDownload(version);
  35. this.dtask.addEventListener('statechanged', this.onStateChanged, false);
  36. this.dtask.start();
  37. },
  38. cancelDown() {
  39. this.dtask.abort();
  40. uni.navigateTo({
  41. url: '/pages/index/index'
  42. });
  43. },
  44. onStateChanged(download, status) {
  45. let prg = 0;
  46. console.log(this.percent);
  47. switch (download.state) {
  48. case 1:
  49. // showLoading.setTitle("正在下载");
  50. break;
  51. case 2:
  52. // showLoading.setTitle("已连接到服务器");
  53. break;
  54. case 3:
  55. prg = parseInt(
  56. //下载的进度
  57. (parseFloat(download.downloadedSize) / parseFloat(download.totalSize)) * 100
  58. );
  59. // showLoading.setTitle("版本更新,正在下载" + prg + "% ");
  60. this.percent = prg;
  61. break;
  62. case 4:
  63. this.installApp(download.filename);
  64. // plus.nativeUI.closeWaiting(); //关闭系统提示框
  65. this.showProcess = false;
  66. //下载完成
  67. break;
  68. }
  69. },
  70. installApp(path) {
  71. plus.nativeUI.showWaiting('安装升级包...');
  72. plus.runtime.install(
  73. path,
  74. {},
  75. function() {
  76. plus.nativeUI.closeWaiting();
  77. uni.showToast({
  78. icon: 'none',
  79. title: '升级完成,准备重新载入'
  80. });
  81. setTimeout(_ => {
  82. uni.hideToast();
  83. plus.runtime.restart();
  84. }, 1000);
  85. },
  86. function(e) {
  87. plus.nativeUI.closeWaiting();
  88. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  89. }
  90. );
  91. }
  92. }
  93. };
  94. </script>
  95. <style lang="scss">
  96. page,
  97. .content {
  98. min-height: 100%;
  99. height: auto;
  100. }
  101. .main {
  102. margin-top: 100rpx;
  103. .main-bg {
  104. margin: 0 auto;
  105. width: 550rpx;
  106. height: 340rpx;
  107. image {
  108. width: 100%;
  109. height: 100%;
  110. }
  111. }
  112. .title {
  113. text-align: center;
  114. color: #000000;
  115. font-size: 40rpx;
  116. margin-top: 20rpx;
  117. font-family: PingFang SC;
  118. font-weight: bolder;
  119. }
  120. .jdt {
  121. width: 550rpx;
  122. margin: 20rpx auto;
  123. }
  124. .num {
  125. text-align: center;
  126. color: #ff7575;
  127. font-size: 30rpx;
  128. font-family: PingFang SC;
  129. font-weight: 500;
  130. }
  131. .tipSpan {
  132. margin-top: 20rpx;
  133. text-align: center;
  134. color: #ababab;
  135. font-size: 30rpx;
  136. font-family: PingFang SC;
  137. font-weight: 500;
  138. }
  139. .btn {
  140. display: flex;
  141. justify-content: center;
  142. align-items: center;
  143. color: #ffffff;
  144. width: 400rpx;
  145. height: 80rpx;
  146. background: #ff4c4c;
  147. margin: 20rpx auto;
  148. border-radius: 20rpx;
  149. }
  150. }
  151. </style>