uniapp实现APP版本升级

App.vue 直接上代码

复制代码
<script>
	export default {
		methods: {
			//APP 版本升级
			Urlupload() {
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					// 版本号变量持久化存储
					getApp().globalData.version = info.version;
					this.ToLoadUpdate(info.versionCode, );
				})
				//#endif
			},

			//下载更新
			ToLoadUpdate(version, ) {
				uni.showModal({
					title: `版本更新 ${version}--->${171}`,
					content: "有更新版本啦!!!",
					confirmText: '立即更新',
					cancelText: '稍后更新',
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							if (plus.networkinfo.getCurrentType() != 3) { //获取手机设备的相关信息,判断是否在Wifi状态。
								uni.showToast({
									title: '检测到您目前非Wifi连接,为节约您的流量,建议您连接WIFI更新!',
									icon: 'none',
									duration: 5000
								});
							} else {
								uni.showToast({
									title: '程序已启动更新,请耐心等待!',
									icon: 'none',
									duration: 3000
								});
							}

							//设置 最新版本apk的下载链接
							const downloadApkUrl =
								'https://wfgtest-1631.oss.wefanbot.com/DEV/qw/106658768610001/1727229160089/wczd_v1.0.0.apk'
							console.log("downloadApkUrl===", downloadApkUrl);

							var dtask = plus.downloader.createDownload(downloadApkUrl, {}, (
								d, status) => { //新建下载任务
								if (status == 200) { //当下载完成
									uni.showModal({
										title: '下载成功',
										content: '确定现在安装吗?',
										confirmText: '立即安装',
										confirmColor: '#EE8F57',
										success: (res2) => {
											if (res2.confirm == true) {
												plus.runtime.install(
													plus.io
													.convertLocalFileSystemURL(
														d.filename
													), {}, {},
													(error) => { //安装应用
														uni.showToast({
															title: '安装失败',
															icon: 'none'
														});
													})
											}
										}
									})
								} else {
									uni.showToast({
										title: '更新失败',
										icon: 'none'
									});
								}
							})
							dtask.start();
							var prg = 0;
							var showLoading = plus.nativeUI.showWaiting("正在下载");

							dtask.addEventListener('statechanged', (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 +
											"% ");
										if (prg === 100) {
											plus.nativeUI.closeWaiting(); //关闭系统提示框
										}
										break;
									case 4:
										plus.nativeUI.closeWaiting(); //关闭系统提示框
										//下载完成
										break;
								}
							});
						} else if (res.cancel) {}
					}
				});
			},

		},
		onLaunch() {
			this.Urlupload()
		},
	}
</script>

效果图

相关推荐
2301_322414280417 小时前
springboot学生课堂出勤统计系统15835-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·mysql·随机森林·微信小程序·uni-app·微信开发者工具·推荐算法
2501_915921433 天前
iOS应用从开发到上架App Store的完整步骤详解
android·ios·小程序·https·uni-app·iphone·webview
小蒜学长4 天前
基于SpringBoot+uni-app的闪洁家政系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·uni-app·家政服务
anyup_前端梦工厂4 天前
uni-app 模板 Starter 一键清理 Skill 发布
前端·人工智能·uni-app
anyup_前端梦工厂4 天前
将uni-app工程迁移uni-app x,AI 能帮我们做什么?
前端·人工智能·uni-app
蒿豪郝灬号5 天前
UniApp接入鸿蒙版阿里百川
华为·uni-app·harmonyos
2501_915921436 天前
详细教程:使用Perfdog工具测量iOS应用的功耗性能与优化方法
android·macos·ios·小程序·uni-app·cocoa·iphone
梦曦i6 天前
uni-app、uni-app x 与 Flutter 深度对比:2026年跨平台开发框架选型指南
flutter·uni-app·uni-app x
普马萨特6 天前
uni-app x 定位踩坑实录:H5/小程序/App 统一经纬度与逆地理编码
uni-app
Geek_Vison7 天前
保险APP已经用了Flutter,还能接入小程序吗?
flutter·小程序·uni-app·mpaas·小程序容器