hbuilder 热更新

记录下Hbuilder做热更新的功能。

首先是获取本地的版本与服务器的版本对比。服务器的版本大于本地的版本才进行更新。获取本地版本的方法

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

然后下载服务器的wgt更新文件方法并安装

 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文件 放在服务器就可以了

原文地址:https://www.cnblogs.com/huzhuhua/p/10832675.html