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>

效果图

相关推荐
PedroQue997 小时前
Vue Router 4.x风格导航守卫全面升级
前端·uni-app
Haibakeji10 小时前
APP小程序定制开发项目频繁延期?前期规避方法与落地避坑指南
小程序·uni-app·软件需求
anyup11 小时前
迁移uni-app x,我是如何让 AI 把我一步步搞崩溃的...
前端·uni-app·trae
2501_9160074712 小时前
申请 iOS 推送证书并配置 APNs 群发推送教程
android·ios·小程序·https·uni-app·iphone·webview
博客zhu虎康13 小时前
uni-app云打包显示编译成功,但是一直没有进入打包队列,后面也一直没反应
uni-app
JavaDog程序狗1 天前
【指南】uni-app微信小程序多环境CI-CD完全指南
ci/cd·uni-app·jenkins
PedroQue991 天前
uni-router v2.1.0 升级:导航守卫全面支持返回值模式
前端·uni-app
2501_915909062 天前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者83 天前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者84 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview