前期介绍
-
获取 uniapp 的包信息
可参考 H5产业联盟 https://www.html5plus.org/doc/zh_cn/nativeui.html -
制作热更新wgt包
对配置的js/nvue文件进行原生混淆,这个类似于代码加密 -
uniapp的wgt包好像不支持差异化热更新,每次都会拉取全部的代码,在云端制作并下发wgt包
-
将制作的wgt包,放到我们的服务器上,然后请求接口时,传递 app版本 和 手机系统 两个参数,以此来获取 热更新包,并安装
下面是详细代码
<script>
export default {
globalData: {
baseUrl: 'http://www.aliba.com', //用于热更新的地址
hotVersion: 1, //热更新版本
},
onLaunch() {
this.hotUpdate();
},
methods: {
hotUpdate() {
// #ifdef APP-PLUS
const _this = this;
//获取App的包信息
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
//调取接口获取当前系统(Android/ios)的这个App版本是否有热更新
_this.getHotUpdateInfo({
appversion: widgetInfo.version, //app版本号
phoneSystem: uni.getSystemInfoSync().platform //app系统
}).then(res => {
console.log(res);
if (_this.globalData.hotVersion > res.hotVersion) {
_this.downloadHotUpdate(res.url);
}
})
})
// #endif
},
getHotUpdateInfo(params) {
return new Promise((resolve, reject) => {
uni.request({
url: this.globalData.baseUrl + '/api/getAppHot',
data: params,
method: 'GET',
success: (res) => {
resolve(res.data);
},
fail: (err) => {
console.log(err);
reject(err);
},
complete: () => {
}
})
})
},
downloadHotUpdate(hotUrl) {
uni.downloadFile({
url: hotUrl,
success: (res) => {
console.log(res);
plus.runtime.install(
res.tempFilePath, {
force: true //是否强制安装
},
function() {
console.log('install success...');
},
function() {
console.error('install fail...');
},
)
},
fail: (err) => {
console.log(err)
uni.showToast({
title: '下载失败',
icon: 'none'
})
}
})
},
}
}
</script>