uniapp 热更新带进度条

注意:在使用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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值