hbuilder 熱更新

記錄下Hbuilder作熱更新的功能。npm

首先是獲取本地的版本與服務器的版本對比。服務器的版本大於本地的版本才進行更新。獲取本地版本的方法服務器

    plus.runtime.getProperty(plus.runtime.appid, data => {
        let version = data.version ///獲取本地的版本。
    });

而後下載服務器的wgt更新文件方法並安裝app

 methods: {
    downloadWgt() {
      // 更新文件 wgt 文件地址
      var wgtUrl = "http://10.10.10.76:8081/H5347043D.wgt";
      // plus.nativeUI.showWaiting("更新ing...");
      let t = this.$toast.loading({
        mask: true,
        message: "安裝包下載中!",
        type: "loading",
        duration: 0
      });
      plus.downloader
        .createDownload(wgtUrl, {}, (d, status) => {
          t.clear();

          if (status == 200) {
            this.installWgt(d.filename); // 安裝wgt方法
          } else {
            this.$toast("下載升級包失敗!");
          }
          // plus.nativeUI.closeWaiting();
        })
        .start();
    },

    installWgt(path) {
      // plus.nativeUI.showWaiting("安裝wgt文件...");
      let t = this.$toast.loading({
        mask: true,
        message: "安裝中...",
        type: "loading",
        duration: 0
      });
      plus.runtime.install(
        path,
        {},
        () => {
          t.clear();// 更新完成後刪除更新包
          plus.io.resolveLocalFileSystemURL(
            path,
            entry => {
              entry.remove(
                () => {
                 console.log("文件刪除成功==" + path);
                  plus.runtime.restart();
                },
                () => {
                  console.log("文件刪除失敗==" + path);
                  plus.runtime.restart();
                }
              );
            },
            err => {
              //plus.nativeUI.alert("路徑不存在");
              plus.runtime.restart();
            }
          );
        },
        err => {
          t.clear();
          this.$toast.fail("安裝失敗,請重試!");
        }
      );
    }
  }

 

npm run build

打包後在Hubilder裏面選擇發行--->製做移動App資源升級包。打包出來 的WGT文件 放在服務器就能夠了ui

相關文章
相關標籤/搜索