downLoad.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="content">
  3. <view class="main">
  4. <view class="main-bg"><image src="../../static/images/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. this.dtask = plus.downloader.createDownload(version);
  32. this.dtask.addEventListener('statechanged', this.onStateChanged, false);
  33. this.dtask.start();
  34. },
  35. cancelDown() {
  36. this.dtask.abort();
  37. uni.navigateTo({
  38. url: '/pages/index/index'
  39. });
  40. },
  41. onStateChanged(download, status) {
  42. let prg = 0;
  43. console.log(this.percent);
  44. switch (download.state) {
  45. case 1:
  46. // showLoading.setTitle("正在下载");
  47. break;
  48. case 2:
  49. // showLoading.setTitle("已连接到服务器");
  50. break;
  51. case 3:
  52. prg = parseInt(
  53. //下载的进度
  54. (parseFloat(download.downloadedSize) / parseFloat(download.totalSize)) * 100
  55. );
  56. // showLoading.setTitle("版本更新,正在下载" + prg + "% ");
  57. this.percent = prg;
  58. break;
  59. case 4:
  60. this.installApp(download.filename);
  61. // plus.nativeUI.closeWaiting(); //关闭系统提示框
  62. this.showProcess = false;
  63. //下载完成
  64. break;
  65. }
  66. },
  67. installApp(path) {
  68. plus.nativeUI.showWaiting('安装升级包...');
  69. plus.runtime.install(
  70. path,
  71. {},
  72. function() {
  73. plus.nativeUI.closeWaiting();
  74. uni.showToast({
  75. icon: 'none',
  76. title: '升级完成,准备重新载入'
  77. });
  78. setTimeout(_ => {
  79. uni.hideToast();
  80. plus.runtime.restart();
  81. }, 1000);
  82. },
  83. function(e) {
  84. plus.nativeUI.closeWaiting();
  85. plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message);
  86. }
  87. );
  88. }
  89. }
  90. };
  91. </script>
  92. <style lang="scss">
  93. page,
  94. .content {
  95. min-height: 100%;
  96. height: auto;
  97. }
  98. .main {
  99. margin-top: 100rpx;
  100. .main-bg {
  101. margin: 0 auto;
  102. width: 550rpx;
  103. height: 340rpx;
  104. image {
  105. width: 100%;
  106. height: 100%;
  107. }
  108. }
  109. .title {
  110. text-align: center;
  111. color: #000000;
  112. font-size: 40rpx;
  113. margin-top: 20rpx;
  114. font-family: PingFang SC;
  115. font-weight: bolder;
  116. }
  117. .jdt {
  118. width: 550rpx;
  119. margin: 20rpx auto;
  120. }
  121. .num {
  122. text-align: center;
  123. color: #ff7575;
  124. font-size: 30rpx;
  125. font-family: PingFang SC;
  126. font-weight: 500;
  127. }
  128. .tipSpan {
  129. margin-top: 20rpx;
  130. text-align: center;
  131. color: #ababab;
  132. font-size: 30rpx;
  133. font-family: PingFang SC;
  134. font-weight: 500;
  135. }
  136. .btn {
  137. display: flex;
  138. justify-content: center;
  139. align-items: center;
  140. color: #ffffff;
  141. width: 400rpx;
  142. height: 80rpx;
  143. background: #ff4c4c;
  144. margin: 20rpx auto;
  145. border-radius: 20rpx;
  146. }
  147. }
  148. </style>