uniapp WeChat Mini Program: Update

Mini Program Operation Mechanism - Mini Program Startup

  1. Cold start : If the user opens the applet for the first time, or is opened again by the user after the applet is destroyed, the applet needs to be reloaded and started, that is, cold start.
  2. Hot start : If the user has already opened a small program, and then opens the small program again within a certain period of time, the small program is not destroyed at this time, but enters the foreground state from the background state, this process is a hot start.

Small program update mechanism

  1. Synchronous update at startup
    1.1. Regularly check for version updates: When WeChat is running, it will regularly check whether there are updates for the recently used applets. If so, open the new version next time you start it.
    1.2. The user has not used the applet for a long time: it will be forced to check for version updates synchronously. (Failed to download the new version, the local old version will still be used)
  2. Asynchronous update at startup
    2.1. Every time the applet is cold-started, it will asynchronously check whether there is an updated version. If a new version is found, the code package of the new version will be downloaded asynchronously. However, the current startup will still use the old version of the client's local code, that is, the new version of the applet will not be used until the next cold startup.

So what we have to deal with is:热启动 check for updates from time to time, and force an update if there is one. Listen during onShowthe lifecycle.

uni.getUpdateManager

const updateManager = uni.getUpdateManager();

export default {
    
    
	data() {
    
     return {
    
    };	},
	onShow: function() {
    
    
		// 当向小程序后台请求完新版本信息,会进行回调。res: {hasUpdate: true, version: 1.0.0}
		updateManager.onCheckForUpdate(function (res) {
    
    
			if (res.hasUpdate) {
    
    					 // 有更新
				uni.showLoading({
    
    title:'更新中...'}); // 开始下载前,显示Loading
			}
		});
		// 当新版本下载完成,会进行回调
		updateManager.onUpdateReady(function () {
    
    
			uni.hideLoading();   // 关闭 Loading 
			uni.showModal({
    
    		// 弹确认框(强制更新)
				title:'更新提示',
				content:'更新完毕,是否重启?',
				success:function (res) {
    
    
					if (res.confirm) {
    
    
						updateManager.applyUpdate(); // 强制小程序重启并使用新版本。
					}
				}
			})
		});
		// 当新版本下载失败,会进行回调
		updateManager.onUpdateFailed(function () {
    
    
			uni.hideLoading();	// 关闭 Loading 
			uni.showToast({
    
     title:'更新失败,稍后再试...', icon:"error" });
		});
	},
	methods: {
    
    	}
};

If it is used in the above external code, it can unibe directly replaced by the WeChat applet project wx.

Development environment debugging

  1. On the WeChat developer tool, you can debug through the switch of "Next Compilation Simulation Update" under "Compile Mode"
  2. The mini program development version/experimental version does not have the concept of "version", so it is impossible to test the version update on the development version/experimental version

References

uni.getUpdateManager()

Mini-program operating mechanism
Mini-program update mechanism
wx.getUpdateManager

Guess you like

Origin blog.csdn.net/jx520/article/details/131598529