uniapp打包安卓应用实现自动更新

一般写在App.vue里,根据实际情况写在onShow或者onLoad

plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
			this.version = widgetInfo.version
			console.log('this.version==',this.version)
			uni.request({
				url: '自己url',
				success: (resa) => {
					let res = resa.data.result
					console.log('res===',res)
					function compareVersion(version1, version2) {
						const newVersion1 = `${version1}`.split('.').length < 3 ? `${version1}`.concat('.0') : `${version1}`;
						const newVersion2 = `${version2}`.split('.').length < 3 ? `${version2}`.concat('.0') : `${version2}`;
						  //计算版本号大小,转化大小
						 function toNum(a){
						 	const c = a.toString().split('.');
						 	const num_place = ["", "0", "00", "000", "0000"],
						 		r = num_place.reverse();
						 	for (let i = 0; i < c.length; i++){
						 		const len=c[i].length;
						 		c[i]=r[len]+c[i];
						 	}
						 		return c.join('');
						 	}
						 		
						 	// 检测版本号是否需要更新
						 	function checkPlugin(a, b) {
						 		const numA = toNum(a);
						 		const numB = toNum(b);
						 		return numA > numB ? 1 : numA < numB ? -1 : 0;
						 	}
						 		return checkPlugin(newVersion1 ,newVersion2);
						 	}
						 		if (res.code == 1) {
									// 1代表app新包版本号大于本地版本号
						 			if (compareVersion(res.appVersion, this.version) === 1) {
						 				uni.showModal({
						 					title: '提示',
											showCancel:false,
						 					content: '发现新的应用安装包,点击确定立即更新',
						 					success: function (resModal) {
						 					   if (resModal.confirm) {
													uni.showLoading({
														title: '更新中……'
													})
						 							uni.downloadFile({
						 								// 存放最新安装包的地址
						 								url: res.url,
						 								success: (downloadResult) => {  
															uni.hideLoading();
															console.log('downloadResult===',downloadResult)
						 								    if (downloadResult.statusCode === 200) { 
																uni.hideLoading();
						 								        plus.runtime.install(downloadResult.tempFilePath,{ 
						 								        	force: false 
						 								        }, function() {
						 								        	console.log('install success...');  
						 								            plus.runtime.restart();  
						 								        }, function(e) {  
																	uni.hideLoading();
						 								            console.error('install fail...');  
						 								        });  
						 								    }
						 								}
						 							}); 
						 					   } 
						 					}
						 				});
						 			} else {
						 		}
						 }
					}
				});
			});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值