uniapp app更新

uniapp app更新
这个版本要随之增加,不然刚更新时直接用app, 新包增加的那些页面跳转会有问题,不能跳新的页面

javascript 复制代码
//app更新检测
updataApp(){
	const that = this;
	uni.showLoading({title:'加载中...'})
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		let currentVersion = widgetInfo.versionCode; //获取当前版本号
		console.log('当前版本',currentVersion)
		that.$api.getLatestVersion()
		.then(res =>{
			console.log('版本',res) 
			if(res.code == 200){
				let newVersion = res.data.version;
				if (currentVersion < newVersion) {
					that.downloadUrl = that.baseUrl + res.data.path;
					console.log('下载路径',that.downloadUrl)
					that.openPop(666)
				}
			}
		})
	})
},
//APP更新
actionApp(){
	const that = this;
	const downloadTask = uni.downloadFile({
		url: that.downloadUrl,
		success: (downloadResult) => {
			console.log('?????',downloadResult)
			plus.runtime.install(
				downloadResult.tempFilePath, {
					force: true
				},
				function() {
					that.closePop()
					uni.showToast({
						title: '安装成功,请重新启动',
						icon: 'none',
						mask:true
					})
					// that.clearCache()
					plus.runtime.restart();
				},
				function(e) {
					console.log(e)
					that.closePop()
					uni.showToast({
						title: '安装失败',
						icon: 'none',
						mask:true
					})
					//关闭应用
					setTimeout(() =>{
						plus.runtime.quit();
					},1000)
				})
		},
		fail: (err) => {
			that.closePop()
			console.log(err);
			uni.showToast({
				title: '下载失败',
				icon: 'none',
				mask:true
			})
			//关闭应用
			setTimeout(() =>{
				plus.runtime.quit();
			},1000)
		}
	})
	
	//下载进度监听
	downloadTask.onProgressUpdate((res) => {
		if (res.progress <= 99) {
			console.log('下载进度'+res.progress+'%');
			that.schedule = res.progress;
		}
	});
},
//清除app更新后的文件缓存
clearCache(type = 2) {
	// 第一种方式:彻底清理存储数据(用户数据),包括用户权限和缓存,但是这种方式清理之后当前的app会闪退出去,只有等待用户重新拉起。
	if (type == 1) {
		plus.android.importClass("android.app.ActivityManager");
		var Context = plus.android.importClass("android.content.Context");
		var am = plus.android.runtimeMainActivity().getSystemService(Context.ACTIVITY_SERVICE);
		am.clearApplicationUserData();
		return
	}
	// 第二种方式:清理内存存储文件(部分用户数据),不会清理用户权限,也不会闪退,一些静态的图片会被清理掉。
	let main = plus.android.runtimeMainActivity();
	let sdRoot1 = main.getFilesDir();
	let filesOne = plus.android.invoke(sdRoot1, "listFiles");
	let lenOne = filesOne.length;
	for (let i = 0; i < lenOne; i++) {
		let filePath = '' + filesOne[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径
		plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
			if (entry.isDirectory) {
				entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
					console.log('清理文件数据完成')
				}, function(e) {
					console.log(e.message)
				});
			} else {
				entry.remove(function(entry) { //递归删除其下的所有文件及子目录
					console.log('清理文件数据完成')
				}, function(e) {
					console.log(e.message)
				});
			}
		}, function(e) {
			console.log('清理文件数据失败')
		});
	};
},
相关推荐
Geek_Vison13 小时前
保险APP已经用了Flutter,还能接入小程序吗?
flutter·小程序·uni-app·mpaas·小程序容器
郑州光合科技余经理1 天前
本地生活平台搭建:跨业态用户标识怎么贯通
java·开发语言·前端·后端·uni-app·php·ai编程
2501_915909062 天前
iOS应用从开发到上架App Store的完整发布流程与步骤指南
android·ios·小程序·https·uni-app·webview
不爱说话郭德纲2 天前
从“点点点”到一键出包:我把 uni-app x Android 离线打包做成了脚本
android·前端·uni-app
snotJam2 天前
关于Uniapp的Android自定义基座使用
android·uni-app
今年下半年3 天前
【微信小程序】uni-app + Vue3 + Vite 的小程序项目实现「进入指定范围才能打卡」的考勤功能
微信小程序·uni-app·定位·考勤
郑州光合科技余经理7 天前
海外版外卖加盟:总站与分站配送规则怎么分开管
java·开发语言·前端·后端·uni-app·php·ai编程
遗憾随她而去.8 天前
uniApp跨端开发: 解决不同平台显示差异问题
uni-app
程序鉴定师11 天前
2026年深圳小程序/App开发公司技术选型指南:从架构设计到交付标准全维度评估
java·小程序·uni-app·php·objective-c
Q264336502311 天前
【有源码】基于uni-app的旅游行程规划小程序-基于微信小程序的智慧文旅综合服务平台
java·微信小程序·小程序·uni-app·毕业设计·springboot·源代码