uniapp整包更新和热更新

​async  mounted() {
      let _this = this
      // #ifdef APP-PLUS
        let version = '' //线上版本
        let insVersion = '' //当前APP安装版本
        //请求 获取服务器最新版本号 以及最新文件地址 APK 和wgt文件
        const res = await  this.$api('getVersion')
        version = res.newversion
        //获取本地安装的版本号,app环境才有此方法 否则会报错
        plus.runtime.getProperty(plus.runtime.appid, (idx)=> {
          insVersion = idx.version
          if(version != insVersion){
            // 线上版本与本地安装版本不一致 唤醒版本安装弹窗
            if(res.updateType == 1){
              // 整包更新
              uni.showModal({
                title: '版本更新',
                content: '有新的版本发布,检测到当前最新版本为:' + version +
                  '是否立即进行版本下载?',
                confirmText: '立即更新',
                cancelText: '稍后进行',
                success: (showResult) => {
                  if (showResult.confirm) {
                  //创建下载任务
                    var dtask = plus.downloader.createDownload(res 
                      .downloadurlApk, {
                        method: 'GET'
                      },
                      function(d, status) {
                        if (status == 200) {
                          uni.showToast({
                            title: '版本更新成功',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                          });
                          var path = d.filename;
                          plus.runtime.install(path);
                        } else {
                          plus.nativeUI.alert('版本更新失败:' +
                            status);
                        
                        }
                      });
                    dtask.addEventListener('statechanged', function(task,status){
                        // 监听下载任务 并计算下载任务进度   
​
                      _this.progress = parseInt((parseFloat(task.downloadedSize) /parseFloat(task.totalSize)) * 100);
                      if(_this.progress>=100){
                          //下载完成 做相应操作
                      }
                    });
                    dtask.start();
                  }else{
                    uni.showToast({
                      title: '正在启动,请稍后!',
                      icon: 'none',
                      duration: 2000,
                      mask: true
                    });
                   
                  }
                }
              })
            }else{
              // 热更新
              uni.showLoading({
                title: '正在更新,请稍后!',
                mask:true
              })
              uni.downloadFile({
                url: res.downloadurlWget,
                success: (downloadResult) => {
                  if (downloadResult.statusCode === 200) {
                    uni.hideLoading()
                    uni.showModal({
                      title: '',
                      content: '更新成功,确定现在重启吗?',
                      confirmText: '重启',
                      confirmColor: '#EE8F57',
                      success: function(res) {
                        if (res.confirm == true) {
                          plus.runtime.install(
                            //安装
                            downloadResult.tempFilePath, {
                              force: true
                            },
                            function(res) {
                              utils.showToast('已重启');
                              plus.runtime.restart();
                            
                            }
                          );
                        }
                      }
                    });
                  }else{
                    uni.hideLoading() 
                  }
                }
              });
            }
          }else{
            // 当前是最新版本
           
          }
        })
       // #endif
      },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值