uniapp implements automatic app update

Requirements overview: Recently encountered requirements, the app scanned and verified needs to be automatically upgraded and installed online (because the app is simply not available in the application market, the online update method is used for research)

Step 1: First you need an interface (backend) that can update and get data
  • For example, the version name when packaging for the first time is 1.0.1, then after manually installing this version 1.0.1 on the mobile phone for the first time, if you want to realize automatic update later, the version name to be packaged again must be greater than 1.0.1 (Because the logic of this implementation is based on the version name)
    Version number location: manifest.json >> basic configuration >> application version name

  • When the app needs to be updated, change the version name (must be greater than the previous version), and start packaging

  • At the same time, update your packaged version name synchronously in the back-end interface

Step 2: The packaged apk file needs to be placed on the server, and then get the address of the file on the server (useful later)
Step 3: Start writing the front-end logic in App.vue (paste the code directly)
  • onLoad() : Triggered when the page is loaded, it will only be called once. In the onLoad() function, the parameters in the current page path can be obtained through option
  • onShow(): Called when the page is displayed, that is, when the page is switched, or when it is switched to the foreground, it can be triggered multiple times
    Determine whether to write in onShow or onLoad according to your own needs

Principle: Get the version name of the current app, call the interface to compare the current app version name and the latest online version name when onShow is triggered, if the latest online version name is greater than the current version name, download the latest apk and install the update

onShow: function() {
    
    
	console.log('App Show')
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
    
    
	this.version = widgetInfo.version
	uni.request({
    
    
		url: 'http://xxxx.cczu.edu.cn:8081/api/dict',
		success: (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);
				 	}
				 	for (let i of res.data.content) {
    
    
				 		if (i.description === 'app版本') {
    
    
							// 1代表app新包版本号大于本地版本号
				 			if (compareVersion(i.dictDetails[0].value, this.version) === 1) {
    
    
				 				uni.showModal({
    
    
				 					title: '提示',
				 					content: '发现新的应用安装包,点击确定立即更新',
				 					success: function (res) {
    
    
				 					   if (res.confirm) {
    
    
				 					       	console.log('用户点击确定');
											uni.showLoading({
    
    
												title: '更新中……'
											})
				 							uni.downloadFile({
    
    
				 								// 存放最新安装包的地址
				 								url: 'http://xxxx.xxxx.com/__UNI__xxxx.apk',
				 								success: (downloadResult) => {
    
      
													uni.hideLoading();
				 								    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 if (res.cancel) {
    
    
				 					   		console.log('用户点击取消');
				 					   }
				 					}
				 				});
				 			} else {
    
    
				 		}
				 	}
				 }
			}
		});
	});
},

Guess you like

Origin blog.csdn.net/FJ101113/article/details/127962734