当前位置:网站首页>Uniapp version update hot update and natural update

Uniapp version update hot update and natural update

2022-06-21 23:09:00 wh20141212

Ha ha ha Today to record uniapp How to update the version of

First, it is divided into hot renewal and natural renewal

1. A hot update is a forced update Must be updated When the system appears bug when Or when new functions affect the use Use hot update
2. Natural renewal is Optional updates Click the download button to download Click Cancel to discard the update

 Insert picture description here

 Insert picture description here

All right. Next, code it

  1. stay App.vue page
    Uninstall onLaunch Inside You can also uninstall methods And then in onLaunch call I use the latter method
methods: {
    
		...mapMutations(['login']),// What is not needed here can be commented out 
			
		/**
		 *  Detection and update implementation of Android applications 
		 */
		AndroidCheckUpdate: function() {
    
			var _this = this;
			var version  = 101;
			// console.log(version);
			uni.request({
    
				// Request address , Set up your own server link 
				// url: '*************/index.php/System/version', // This is the full domain name 
				//  You can also write the domain name in the warehouse   Then it calls   In case the domain name is changed back and forth 
				url: _this.baseUrl +'index.php/System/version', 
				method: 'POST',
				data: {
    },
				success: resMz => {
    
					console.log(" Version number information ",resMz.data);
					// Version number returned 
					var server_version = resMz.data.data.version;
					// Time stamp returned  
					var currTimeStamp = resMz.data.data.timestamp;
					// Status code returned  0  It's natural renewal  1 Is forced update 
					var status = resMz.data.data.update_status;
					console.log(" Local version number version= "+ version + ", Update code ="+status + ", Background version number ="+server_version+ ", Time stamp "+ currTimeStamp);
					if (status == 1) {
    
						// Force update   Also known as hot update    The system appears large bug  Must update 
						_this.MustcheckVersionToLoadUpdate(server_version,version);
					} else if (status == 0) {
    
						// Natural renewal 
						uni.getStorage({
    
							key: 'tip_version_update_time',
							success: function(res) {
    
								var lastTimeStamp = res.data;
								var tipTimeLength = 0;
								console.log( " The time interval ",tipTimeLength);
								let cha = lastTimeStamp + tipTimeLength - currTimeStamp;
								console.log(" Local time stamp =",lastTimeStamp);
								console.log(" Time stamp difference ",cha);
								if (lastTimeStamp + tipTimeLength > currTimeStamp) {
    
									// Don't bother here 
									console.log(' Only when the background timestamp is greater than the local timestamp will it enter ');
								} else {
    
									console.log(' Update immediately ');
									// Reset timestamp 
									_this.setStorageForAppVersion(currTimeStamp);
									// Compare versions and models   And download update requests 
									console.log(server_version, version);
									_this.checkVersionToLoadUpdate(server_version, version);
									
								}
							},
							fail: function(res) {
    
								_this.setStorageForAppVersion(currTimeStamp);
							}
						});
					}
				},
				fail: () => {
    },
				complete: () => {
    }
			});
		},
		/**
		 * // Set the cache information corresponding to the application version number 
		 * @param {
    Object} currTimeStamp  Currently acquired timestamp 
		 */
		setStorageForAppVersion: function(currTimeStamp) {
    
			uni.setStorage({
    
				key: 'tip_version_update_time',
				data: currTimeStamp,
				success: function() {
    
					console.log('setStorage-success');
				}
			});
		},
		/**
		 *  Compare versions and models   And download update requests   Natural renewal 
		 * @param {
    Object} server_version  The server is up to date   Application version number 
		 * @param {
    Object} curr_version  Current application version number 
		 */
		checkVersionToLoadUpdate: function(server_version, curr_version) {
    
			if (server_version > curr_version) {
    
				uni.showModal({
    
					title: ' Version update ',
					content: ' There's a new release , Do you want to download the new version now ?',
					confirmText: ' Update immediately ',
					cancelText: ' Cancel ',
					success: function(res) {
    
						if (res.confirm) {
    
							uni.showToast({
    
								icon: 'none',
								mask: true,
								title: ' There's a new release , The program has started automatic update .',
								duration: 5000
							});
							// Set up   The latest version apk Download link for   It's fixed   Each time you put the package in this link   Made by the back end 
							var downloadApkUrl = 'http://zons.oss-cn-shenzhen.aliyuncs.com/upload/20200616/20200616/159228906014ee22eaba297944c96afdbe5b16c65b.apk';
							console.log(downloadApkUrl);
							plus.runtime.openURL(downloadApkUrl);
						} else if (res.cancel) {
    
							console.log(' Sure next time ');
							
						}
					}
				});
			}
		},
		/**
		 *  Compare versions and models   And download update requests   Natural renewal 
		 * @param {
    Object} server_version  The server is up to date   Application version number 
		 * @param {
    Object} curr_version  Current application version number 
		 */
		MustcheckVersionToLoadUpdate: function(server_version, curr_version) {
    
			if (server_version > curr_version) {
    
				uni.showModal({
    
					title: ' Version update ',
					content: ' There's a new release , It is detected that you are currently Wifi Connect , Do you want to download the new version now ?',
					confirmText: ' Update immediately ',
					showCancel:false,
					success: function(res) {
    
						if (res.confirm) {
    
							uni.showToast({
    
								icon: 'none',
								mask: true,
								title: ' There's a new release , The program has started automatic update .',
								duration: 5000
							});
							// Set up   The latest version apk Download link for   It's fixed 
							var downloadApkUrl = 'http://zons.oss-cn-shenzhen.aliyuncs.com/upload/20200616/20200616/159228906014ee22eaba297944c96afdbe5b16c65b.apk';
							console.log(downloadApkUrl);
							plus.runtime.openURL(downloadApkUrl);
						}
					}
				});
			}
		},
onLaunch: function() {
    
		this.AndroidCheckUpdate();
}

In addition, I refer to the content of a great God Here is a link
https://www.jianshu.com/p/541c286f69ea

原网站

版权声明
本文为[wh20141212]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212116074461.html