热更新是一种App软件开发者常用的更新方式。简单来说,就是在用户通过下载安装APP之后,打开App时遇到的即时更新。
实现的主要思路:在页面初始化的时候(onLaunch或者首页的onLoad),先获取到当前的版本号,走接口,获取到目前线上最新的软件版本号,判断哪个版本为最新版本,如果线上为最新版本,就使用uni.downloadFile
下载这个文件,下载完成后安装这个软件即可
let _that = this
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
console.log(widgetInfo.version, 'widgetInfo.version');
oForm.checkAppUpload().then( lastAppInfo => {
console.log(lastAppInfo,'lastAppInfo');
this.sCurrentVersion = widgetInfo.version;
// 判断版本
if (this.compareVersion(this.sCurrentVersion, lastAppInfo.version) == 2) {
uni.showModal({
title: "系统提示",
content: `当前版本号为(${this.sCurrentVersion}),邀您更新版本(${lastAppInfo.version})`,
success: (res) => {
//用户确认更新
if (res.confirm) {
//使用下载api(uni.downloadFile)
let uploadTask = uni.downloadFile({
url: "这里写后端给的apk在线下载地址",
success: (downloadResult) => {
console.log(downloadResult,'downloadResult');
//使用安装wgtapi(plus.runtime.install)
plus.runtime.install(
downloadResult.tempFilePath, {
force: true
},
() => {
uni.showModal({
title: '系统提示',
content: '新版本已经更新完成,需要重启应用',
showCancel: false,
//用户点击确认重启
success: function(res) {
if (res.confirm) {
plus.runtime.restart();
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
)
},
fail:(err) => {
console.log(err,'err');
}
});
//在这里监听用户下载进度
uploadTask.onProgressUpdate((res) => {
if (res.progress > 0) {
_that.bShowLoading = true
}
_that.nLoadingNum = res.progress
if (_that.nProgress == 100) {
_that.bShowLoading = false
}
})
}
}
});
}
})
});
比较版本号的方法
/**
* 版本比较
* @param {Object} version1 当前版本
* @param {Object} version2 线上最新版本
*/
compareVersion(version1, version2) {
let arr1 = version1.split(".");
let arr2 = version2.split(".");
let length = Math.max(arr1.length, arr2.length);
for (let i = 0; i < length; i++) {
const n1 = Number(arr1[i] || 0)
const n2 = Number(arr2[i] || 0)
// 第一个版本号大
if (n1 > n2) return 1
//第二个版本号大
if (n1 < n2) return 2
}
return false;
}
需要注意的是:有些链接下载的时候他是不会触发 onProgressUpdate
这个方法,这个时候只能在 uni.downloadFile
前启动 loading ,在下载完成后关闭 loading
uni.downloadFile传送门
手动计算进度条
// 弹窗触发
update(){
let _that = this
let downloadURL = `https://www.xxxxxxx.com/xxxx`
var dtask = plus.downloader.createDownload(downloadURL, {
method: 'GET'
}, (d, status) => {
if (status == 200) {
_that.nLoadingNum = 100
_that.bShowLoading = false
console.log('下载成功安装: ' + d.filename)
plus.runtime.install(d.filename)
} else {
_that.bShowLoading = false
plus.nativeUI.alert('安装失败,请稍候重试:' + status)
}
})
_that.bShowLoading = true
dtask.addEventListener('statechanged', (task) => {
if (task.state == 3) {
_that.nNowDownLoad = dtask.downloadedSize
if (dtask.downloadedSize > this.objNewInfo.fileSize) {
_that.nLoadingNum = 99
} else {
_that.nLoadingNum = Math.floor(dtask.downloadedSize / this.objNewInfo.fileSize * 100)
}
}
})
dtask.start()
_that.tTimer = setInterval(() => {
let nDownByte = _that.nNowDownLoad - _that.nBeforeDownLoad
if (nDownByte > 1024 * 1024) {
let speed = (nDownByte / 1024 / 1024).toFixed(1)
_that.sDownSpeed = speed + 'mb/s'
} else {
let speed = (nDownByte / 1024).toFixed(1)
_that.sDownSpeed = speed + 'kb/s'
}
// sDownSpeed 这个就是网速
_that.nBeforeDownLoad = _that.nNowDownLoad
}, 1000)
}