uni-app 版本检测升级

发布于:2025-02-10 ⋅ 阅读:(51) ⋅ 点赞:(0)

uni-app 版本检测升级 (安卓系统)

  1. pc端管理页面

版本号:必须和app打包 manifest.json 中的版本号一致
在这里插入图片描述

  1. app端

这里直接上代码

// App.vue
<script>
	export default {
		data(){
			return {
				version:'1.0.0',
				platform:'ios',
			}
		},
		onLaunch() {
			console.log('App Launch')
			// #ifdef APP-PLUS
				this.apppanduangengxin()
			// #endif
		},
		methods:{
			apppanduangengxin(){
				this.platform = uni.getSystemInfoSync().platform;
				plus.runtime.getProperty( plus.runtime.appid, ( wgtinfo ) => {
					this.version = wgtinfo.version
					this.guanyuwomen()
				});
			},
			guanyuwomen(){
				uni.$u.http.get(`/***`,{}).then(res => {
					// console.log(this.version,res.data)
					if(this.version != res.data.version){
						if (this.platform == 'android') {
							uni.showModal({
								title:'版本更新提示',
								content:res.data.contents,
								// showCancel:false,
								success: (res02) => { 
									if (res02.confirm) {
										console.log("用户点击了确定增量更新");
										// this.UpdateOfWgt(res)
										this.UpdateOfApk(res)
									} else {
										// uni.$emit("noUpdate", {})
										console.log("用户点击了取消增量更新");
									}  
								}  
							})
							
						}else {
							// plus.runtime.launchApplication(
							// 	{
							// 		action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
							// 	},
							// 	function(e) {
							// 		console.log(e.message);
							// 	}
							// );
						}
					}
				})
			},
			UpdateOfApk(result) {
				let prg = 0;
				let showLoading = plus.nativeUI.showWaiting("正在下载");  //创建一个showWaiting对象 
				const downloadTask = uni.downloadFile({
					url: result.data.downloadLink,  
					success: (downloadResult) => { 
						plus.nativeUI.closeWaiting();
						if (downloadResult.statusCode === 200) { 
							console.log('安装包下载成功,即将安装:' + JSON.stringify(downloadResult,null,4));
							plus.runtime.openFile( downloadResult.tempFilePath )
						}  
					}
				});
				downloadTask.onProgressUpdate((res) => {
					// uni.$emit("progress", res)
					prg = parseInt(
						(parseFloat(res.totalBytesWritten) /
						 parseFloat(res.totalBytesExpectedToWrite)) *
						  100
					);
					showLoading.setTitle("  正在下载" + prg + "%  ");
					// console.log('下载进度' + res);
					// console.log('已经下载的数据长度' + res.totalBytesWritten);
					// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				});
				uni.$on("cancelUpdate", ()=> {
					console.log("用户点击了取消下载");
					downloadTask.abort();
				})
			},
		}
	}
</script>



看懂了吧 为我点点赞


网站公告

今日签到

点亮在社区的每一天
去签到