h5打包的app进行版本升级

// 获取版本号
    getNativeVersion () {
      let that = this;
      document.addEventListener("plusready", function () {
        plus.runtime.getProperty(plus.runtime.appid, function (inf) {
          that.nativeVersion = inf.version;
          localStorage.setItem("nativeVersion", inf.version);
          that.checkupdate()
        });
      });
    },
  // 对比版本号
    checkupdate () {
      this.axios.post(API_URL + 'Home/Index/getAppVersion', qs.stringify({
      })).then((res) => {
        if (res.data.status == 1) {
          this.version = res.data.data.version
          this.downurl = res.data.data.downurl
          // alert(res.data.data.version + '接口版本')
          // alert(localStorage.getItem('nativeVersion') + 'hbuilder版本')
          // alert(res.data.data.version != localStorage.getItem('nativeVersion'))

          if (res.data.data.version != localStorage.getItem('nativeVersion')) {
            MessageBox.confirm('', {
              title: '提示',
              message: '发现新版本',
              confirmButtonText: '升级',
              cancelButtonText: '取消'
            }).then(action => {
              if (action == 'confirm') {
                // alert(this.downurl)
                // this.downloadApk(this.downurl)
                plus.nativeUI.toast("正在准备环境,请稍后!");
                var dtask = plus.downloader.createDownload(this.downurl, {}, function (d, status) {
                  if (status == 200) {
                    var path = d.filename;//下载apk
                    plus.runtime.install(path); // 自动安装apk文件
                  } else {
                    plus.nativeUI.alert('版本更新失败:' + status);
                  }
                });
                dtask.start();
              }
            }).catch(error => {
              if (error == 'cancel') {
                alert('取消升级');
              }
            });
          } else {
            console.log('版本一致')
          }
        } else {
          alert(res.data.data.version)
        }
      }).catch((err) => {
        console.log(err);
      });
    },
posted @ 2021-04-02 17:29  小小小~  阅读(459)  评论(0编辑  收藏  举报