在app页面中初始化后检测版本
checkedVersion() {
if (plus) {
const _this = this
plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
//调用接口进行版本比较,可以是后端比较也可以是前端比较
uni.request({
url: `http://${ip}:${port}/app/appVersion/verifyVersion`, //请求接口
method: 'get',
data: {
version: wgtinfo.version,
},
success: result => {
console.log(result, '检查版本结果')
if (result.data.code === 200 && result.data.data) {
// 如果无需强制更新,设置showCancel为true
uni.showModal({
title: '发现新版本',
content: '下载更新后,方可继续使用系统',
showCancel: false,
confirmText: '下载新版本',
complete: res => {
//完成后下载app
_this.changeAppVersion(result.data.data)
}
})
}
}
})
})
}
}
changeAppVersion(path) {
let _this = this
let ip = uni.getStorageSync('ip')
let port = uni.getStorageSync('port')
let downloadResult = `http://${ip}:${port}/app/appVersion/getVersion`
console.log(downloadResult, 'downloadResult')
var dtask = plus.downloader.createDownload(
downloadResult, {},
function(detail, status) {
uni.showToast({
title: '下载完成',
mask: false,
duration: 1000
})
// 下载完成
if (status === 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(detail
.filename), {}, e => e, function(error) {
uni.showToast({
title: '安装成功',
mask: false,
duration: 1500
})
})
} else {
uni.showToast({
title: '更新失败-02',
mask: false,
duration: 1500
})
}
})
try {
dtask.start() // 开启下载的任务
var prg = 0
var showLoading = plus.nativeUI.showWaiting('正在下载') //创建一个showWaiting对象
dtask.addEventListener('statechanged', function(
task,
status
) {
// 给下载任务设置一个监听 并根据状态 做操作
switch (task.state) {
case 1:
showLoading.setTitle('正在下载')
break
case 2:
showLoading.setTitle('已连接到服务器')
break
case 3:
prg = parseInt(
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
)
showLoading.setTitle(' 正在下载 ' + prg + '% ')
break
case 4:
plus.nativeUI.closeWaiting()
plus.runtime.quit()
//下载完成
break
}
})
} catch (err) {
plus.nativeUI.closeWaiting()
uni.showToast({
title: '更新失败-03',
mask: false,
duration: 1500
})
}
},