uniapp写安卓端app自动更新下载apk安装

需要后端返回一个接口,接口里有最新版本号和下载地址,通过对比当前版本号和最新版本号来控制下载和安装最新版本

复制代码
//获取当前版本
AndroidCheckUpdate() {
	const systemInfo = uni.getSystemInfoSync();
	this.version = Number(systemInfo.appVersion.split('.').join(''))
	home.getVersionNew({}).then(res => {
		console.log(res,'res')
		var versions = Number(res.data.version.split('.').join(''))||Number(version)
		if(versions>this.version){
			uni.showModal({
				title:'版本提示',
				content:'检查到当前版本不是最新版本,是否更新为最新版本?',
				success:(e)=> {
					if(e.confirm){
						console.log(e)
						this.pullapk(res.data.fileUrl)
						//下载更新
					}
				}
			})
		}
	}).catch(e => {
		uni.showToast({
			title:e.msg,
			icon:'none'
		})
		return
	})
	},

pullapk(url){
	console.log(url)
	uni.showLoading({ title: "下载中" });
	uni.downloadFile({
		url: url, //下载地址
		success: (res) => {
			console.log(res, "下载成功");
			if (res.statusCode === 200) {
				console.log("下载成功");
				uni.hideLoading();
				uni.showToast({ title: "下载成功", icon: "success" });
				uni.saveFile({
					tempFilePath: res.tempFilePath,
					success: function (res) {
						uni.openDocument({
							filePath: res.savedFilePath,
							success: function (res) {
								console.log(res, "打开安装包");
							},
						});
					},
					fail: (err) => {
						console.log(err, "打开安装包-失败");
					},
				});
			}
		},fail(e) {
			uni.hideLoading();
			uni.showToast({ title: "下载失败,请检查网络", icon: "none" });
			console.log(e)
		},
	})
},
相关推荐
Deryck_德瑞克1 小时前
【已解决】MySQL连接出错 1045 - Access denied for user ‘root‘@‘::1‘
android·mysql·adb
2501_915918412 小时前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
程序员小寒2 小时前
JavaScript设计模式(八):命令模式实现与应用
前端·javascript·设计模式·ecmascript·命令模式
.豆鲨包2 小时前
【Android】组件化搭建的一般流程
android
心有—林夕3 小时前
MySQL 误操作恢复完全指南
android·数据库·mysql
忙什么果3 小时前
Mamba学习笔记2:Mamba模型
android·笔记·学习
Z_Wonderful4 小时前
在 Next.js 中,使用 [id] 或 public 作为文件夹或文件名是两种完全不同的概念,分别对应 动态路由 和 静态资源托管
javascript·网络·chrome
Wyawsl4 小时前
MySQL故障排查与优化
android·adb
私人珍藏库6 小时前
[Android] 后台视频录制 FadCam v3.0.1
android·app·工具·软件·多功能
Z_Wonderful6 小时前
在 **Next.js** 中使用 `mysql2` 连接 MySQL 数据库并查询 `xxx` 表的数据
android·数据库