hbuilder 热更新

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

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

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

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

    download() {
      // 更新文件 wgt 文件地址
      var wgtUrl = "http://10.10.10.76:8081/H5347043D.wgt";
      plus.nativeUI.showWaiting("更新ing...");
      plus.downloader
        .createDownload(wgtUrl, { filename: "update" }, (d, status) => {
          if (status == 200) {
            this.install(d.filename); // 下载成功安装
          } else {
            plus.nativeUI.alert("下载wgt失败!");
          }
          plus.nativeUI.closeWaiting();
        })
        .start();
    },
    install(path) {
      // 安装下载 的文件
      plus.nativeUI.showWaiting("安装wgt文件...");
      plus.runtime.install(
        path,
        {},
        function() {
          plus.nativeUI.closeWaiting();
          console.log("安装wgt文件成功!");
          plus.nativeUI.alert("应用资源更新完成!", function() {
            plus.runtime.restart();
          });
        },
        function(e) {
          plus.nativeUI.closeWaiting();
          console.log("安装wgt文件失败[" + e.code + "]:" + e.message);
          plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]:" + e.message);
        }
      );
    }
npm run build

打包后在Hubilder里面选择发行--->制作移动App资源升级包。打包出来 的WGT文件 放在服务器就可以了

猜你喜欢

转载自www.cnblogs.com/huzhuhua/p/10832675.html