export function autoUpgrate (data, callback) {
var currentInfo = data
newVersion = data
if (!window.plus) return
var appid = window.plus.runtime.appid
window.plus.runtime.getProperty(appid, function (wgtinfo) {
var currentVer = wgtinfo.version
if ((+currentInfo.force === 1 && currentVer < currentInfo.ver) || +currentInfo.force === 2 && currentVer !== currentInfo.ver) {
// 有更新,
const isdownload = window.localStorage.getItem('isdownload') || 0
if (isdownload === '1') {
installRestatrt()
} else {
upgrade(currentInfo)
}
}
})
}
export function upgrade (data) {
var rooturl = data.address + data.filename + data.ver
var suffix = +data.type === 1 ? window.plus.os.name === 'iOS' ? '.ipa' : '.apk' : +data.type === 2 ? '.wgt' : '.wgtu'
if (suffix === '.ipa') {
suffix = '.wgt'
}
var downloadurl = rooturl + suffix
var download = window.plus.downloader.createDownload(downloadurl, {method: 'GET'}, function (f, status) {
if (status === 200) {
localStorage.removeItem('guide-lauchFlag')
// 下载完毕
window.localStorage.setItem('filename', f.filename)
if (data.force === '2') {
// 强制安装
Vue.$vux.alert.show({
title: '提示',
content: '版本(' + data.ver + ')已就绪,将立即启用',
onHide () {
window.plus.runtime.install(f.filename, {force: true}, function () {
// 安装成功
window.localStorage.removeItem('filename')
window.localStorage.removeItem('isdownload')
window.plus.runtime.restart()
}, function () {
// 安装失败
installCount++
if (installCount < 3) {
upgrade(data)
} else {
return
}
})
}
})
} else {
window.localStorage.setItem('isdownload', '1') // 记录已经下载完毕状态
}
}
})
download.addEventListener('statechanged', function (task, status) {
if (status === 200) {
Vue.$vux.loading.hide()
localStorage.removeItem('guide-lauchFlag')
}
}, false)
download.start()
}
// 安装并重启
export function installRestatrt () {
const filename = window.localStorage.getItem('filename') || ''
if (filename !== '') {
window.plus.runtime.install(filename, {force: true}, function () {
// 安装成功
window.localStorage.removeItem('filename')
window.localStorage.removeItem('isdownload')
window.plus.runtime.restart()
}, function () {
// 安装失败
installCount++
if (installCount < 3) {
upgrade(newVersion)
} else {
return
}
})
}
}
// 是否重新启动
export function upgradeRestart () {
if (newVersion && newVersion.ver) {
Vue.$vux.confirm.show({
title: '新版本',
content: '版本(' + newVersion.ver + ')已就绪,将立即应用.',
onConfirm () {
installRestatrt()
}
})
}
}
代码如上,但是会一直在走 安装, 重启, 安装, 重启.... 安卓正常,求指教
0 个回复