注意:在使用plus.runtime.install 时,如果无法更新成功,请将 force设置为true
App.vue 文件
onLaunch() {
this.HotUpdate();
},
methods: {
HotUpdate() {
// #ifdef APP
let data ={
version:"1.1.0",//线上版本
Url:url,//下载更新文件地址
};
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
let linevison = Number(data.version.replace(/\./g, ""));
let localvsion = Number(widgetInfo.version.replace(/\./g, ""));
if (linevison > localvsion) {
uni.reLaunch({
url: `/pages/UpdatePage/UpdatePage?Url=${data.Url}`,
success: (e) => {
plus.navigator.closeSplashscreen();
},
fail(e) {
plus.navigator.closeSplashscreen();
}
})
} else {
plus.nativeUI
.closeWaiting();
plus.navigator.closeSplashscreen();
}
});
// #endif
},
}
pages/UpdatePage /UpdatePage.vue 文件
<template>
<view class="update">
<text>正在更新请稍后...</text>
<view class="progress-box">
<progress :percent="percent" show-info stroke-width="6" activeColor="#409eff" backgroundColor="#EBEBEB" />
</view>
</view>
</template>
<script>
export default {
data() {
return {
percent: 0,
Url: ''
}
},
onLoad(option) {
if (option.Url) {
this.Url= option.Url;
this.download()
}
},
methods: {
download() {
let dtask = uni.downloadFile({
url: this.Url,
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
plus.runtime.install(
downloadResult
.tempFilePath, {
force: true
},
() => {
plus.nativeUI.showWaiting("更新成功");
setTimeout(() => {
plus.nativeUI
.closeWaiting();
plus.runtime.restart();
}, 500);
},
function(e) {
plus.nativeUI.showWaiting("安装失败");
setTimeout(() => {
plus.nativeUI
.closeWaiting();
uni.switchTab({
url: "/pages/index/index"
})
}, 1000);
console.error('install fail...');
});
}
},
fail: (e) => {
plus.nativeUI.closeWaiting();
plus.nativeUI.showWaiting("下载失败");
setTimeout(() => {
plus.nativeUI
.closeWaiting();
this.navTo("/pages/index/index");
}, 1000);
}
})
dtask.onProgressUpdate((res) => {
this.percent = res.progress;
})
}
}
}
</script>
<style lang="scss">
/deep/.uni-progress-bar {
border-radius: 200rpx;
.uni-progress-inner-bar {
border-radius: 200rpx;
}
.uni-progress-inf {
color: #606266;
}
}
</style>
<style lang="scss" scoped>
.update {
width: 100vw;
height: 100vh;
background-size: 100% 98%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.progress-box {
margin-top: 10rpx;
width: 75%;
}
}
</style>