VueApp 自动更新解决plus is not defined问题

一,今天用VueApp 做自动更新调用按照网上列子直接 Plus 打包编译后出现了plus is not defined 

 发现需要引用document.addEventListener("plusready",function(){}) 

代码如下

 

<script>


export default {
  name: "looklive",
  components: {
    // eslint-disable-next-line vue/no-unused-components
    headcontrol,
  },
  data() {
    return {
      path: "",
      installFlag: false,
    };
  },
  mounted() {
    //自动更新
    this.getNativeVersion();
  },
  methods: {
    // 获取当前版本号
    getNativeVersion() {
      let that = this;
      document.addEventListener("plusready", function () {
        plus.runtime.getProperty(plus.runtime.appid, function (inf) {
          that.nativeVersion = inf.version;
          that.checkUpdate(inf.version);
          localStorage.setItem("nativeVersion", inf.version);
        });
      });
    },
    // 检查更新
    checkUpdate(nativeVersion) {
      let that = this;
      const checkUrl = "http://119.23.211.81:8000/api/ip/GetAppVersion";
      that.axios.get(checkUrl).then((result) => {
        // alert("服务区版本" + result.data.version);
        //alert(nativeVersion);
        if (!that.VersionContrast(nativeVersion, result.data.version))
          that.downloadApk(result.data.url);
      });
    },
    //版本号对比
    VersionContrast(curV, reqV) {
      // 当前版本号:curV;比较版本号:reqV
      if (curV && reqV) {
        let arr1 = curV.split("."),
          arr2 = reqV.split(".");
        let minLength = Math.min(arr1.length, arr2.length),
          position = 0,
          diff = 0;
        //依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍)
        while (
          position < minLength &&
          (diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0
        ) {
          position++;
        }
        diff = diff != 0 ? diff : arr1.length - arr2.length;
        //若curV大于reqV,则返回true

        return diff >= 0;
      }
    },
    // 下载apk文件
    downloadApk(url) {
      let that = this;

      let watiting = plus.nativeUI.showWaiting("安装文件...");
      let dtask = plus.downloader.createDownload(url, {
        method: "GET",
        retry: 0,
      });
      dtask.addEventListener(
        "statechanged",
        function (task, status) {
          if (!dtask) {
            return;
          }
          switch (task.state) {
            case 1:
              break;
            case 2:
              break;
            case 3:
              var nowData = Math.floor(
                (task.downloadedSize * 100) / task.totalSize
              );
              console.log(nowData);
              if (nowData % 10 === 0) {
                watiting.setTitle("已下载:" + nowData + "%");
                if (nowData === 100) {
                  watiting.toast("正在准备环境,请稍后!");
                  watiting.close();
                }
              }
              break;
            case 4:
              // 安装apk资源包
              plus.runtime.install(
                dtask.filename,
                {},
                function () {
                  plus.nativeUI.closeWaiting();
                  plus.nativeUI.alert("更新完成!", function () {
                    //  更新完成后重启应用
                    plus.runtime.restart();
                  });
                },
                function (e) {
                  plus.nativeUI.closeWaiting();
                  plus.nativeUI.toast("安装更新失败!");
                }
              );
              break;
            default:
              break;
          }
        },
        false
      );
      dtask.setRequestHeader("Access-Control-Allow-Origin", "*");
      dtask.start();
    },
  },
};
</script>

 

效果如下

  

posted @ 2020-10-26 17:42  可乐_加冰  阅读(3090)  评论(1编辑  收藏  举报