downLoad.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 :percentage="percent" inactiveColor="#f5957d" activeColor="#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. background-color: #FFF;
  101. }
  102. .main {
  103. margin-top: 100rpx;
  104. .main-bg {
  105. margin: 0 auto;
  106. width: 550rpx;
  107. height: 340rpx;
  108. image {
  109. width: 100%;
  110. height: 100%;
  111. }
  112. }
  113. .title {
  114. text-align: center;
  115. color: #000000;
  116. font-size: 40rpx;
  117. margin-top: 20rpx;
  118. font-family: PingFang SC;
  119. font-weight: bolder;
  120. }
  121. .jdt {
  122. width: 550rpx;
  123. margin: 20rpx auto;
  124. }
  125. .num {
  126. text-align: center;
  127. color: #ff7575;
  128. font-size: 30rpx;
  129. font-family: PingFang SC;
  130. font-weight: 500;
  131. }
  132. .tipSpan {
  133. margin-top: 20rpx;
  134. text-align: center;
  135. color: #ababab;
  136. font-size: 30rpx;
  137. font-family: PingFang SC;
  138. font-weight: 500;
  139. }
  140. .btn {
  141. display: flex;
  142. justify-content: center;
  143. align-items: center;
  144. color: #ffffff;
  145. width: 400rpx;
  146. height: 80rpx;
  147. background: #ff4c4c;
  148. margin: 20rpx auto;
  149. border-radius: 20rpx;
  150. }
  151. }
  152. </style>