当前位置:网站首页>[untitled]
[untitled]
2022-04-23 02:14:00 【Chang'an CC】
How to prompt the user to force update after the wechat applet releases a new version !
Premise
After the applet is updated , If you do not actively delete the applet, then enter , So the small program still uses the old version code , Unable to update the latest version . In this way, if there is an urgent problem, repair it online , Users can't force updates at the first time .
terms of settlement
The official provided UpdateManager Update manager object , Used to detect whether a new version has been released .
UpdateManager Object provides the following four methods :
UpdateManager.applyUpdate() Force the applet to restart and use the new version . After downloading the new version of the applet ( I will receive
onUpdateReady Callback ) call .UpdateManager.onCheckForUpdate(function callback)
Listen for the event of requesting wechat background to check and update the result . Wechat automatically checks for updates when the applet is cold started , There is no need for developers to actively trigger .UpdateManager.onUpdateReady(function callback)
The listener applet has a version update event . The client actively triggers the download ( No developer trigger required ), Callback after successful downloadUpdateManager.onUpdateFailed(function callback)
Listen for applet update failure events . There is a new version of the applet , The client actively triggers the download ( No developer trigger required ), Download failed ( It may be due to the network, etc ) Back to
Official sample code :
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// Call back after requesting new version information
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: ' Update hints ',
content: ' The new version is ready , Restart application or not ?',
success: function (res) {
if (res.confirm) {
// The new version has been downloaded , call applyUpdate Apply new version and restart
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// New version download failed
})
Official demo in , Only the most basic update tips , No exception handling . And the official also said that this function is from the basic library 1.9.90 Start supporting , Compatibility is required for the lower version .
in addition , If there are major adjustments in the current version update , Users must update , There are two situations :
-
The first method is to give a prompt in the callback when the user clicks cancel , And re enter the version prompt process .
-
The second way is to remove the Cancel button , Only the OK button . Force users to upgrade , To continue to use .
The improved code is as follows :
/** * Detect current applet * Is it the latest version , Need to download 、 to update */
function checkUpdateVersion() {
// Determine whether the wechat version Compatible with applet update mechanism API Use
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
// Check for version updates
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: ' reminder ',
content: ' New version detected , Do you want to restart the applet ?',
showCancel: false,
success: function (res) {
if (res.confirm) {
// The new version has been downloaded , call applyUpdate Apply new version and restart
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// New version download failed
wx.showModal({
title: ' There is a new version ',
content: ' Please delete the applet , Re search into ',
})
})
}
})
} else {
wx.showModal({
title: ' Warm tips ',
content: ' Current wechat version is too low , This feature is not available , Please upgrade to the latest wechat version and try again .'
})
}
}
stay app.js Of onLaunch Add the check method to the method
// Version automatically updates the code
this.checkUpdateVersion()
How to test ?
Be careful :
Wechat developers can use 「 Compile mode 」 Under the 「 Next time you compile a simulation update 」 Switch to debug
Small program development version / The experience version doesn't have 「 edition 」 Concept , So it can't be in the development version / Test the updated version on the experience version

版权声明
本文为[Chang'an CC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230212118052.html
边栏推荐
- Explain JS prototype and prototype chain in detail
- 电源电路设计原来是这么回事
- Why is one plus one equal to two
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- Consider defining a bean of type 'com netflix. discovery. AbstractDiscoveryClientOptionalArgs‘
- tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Analyze the three functions of static proxy IP.
- 007_ Redis_ Jedis connection pool
- 数仓建表111111
- How to call out services in idea and display the startup class in services
猜你喜欢

Is it better to use a physical machine or a virtual machine to build a website?

Analyze the advantages and disadvantages of tunnel proxy IP.

RT_Thread自问自答

What are the common proxy IP problems?

How does Axure set the content of the text box to the current date when the page is loaded

电源电路设计原来是这么回事

【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views

005_ redis_ Set set

001_redis设置存活时间

Open3d point cloud processing
随机推荐
Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
Lane cross domain problem
[chrome extender] content_ Cross domain problem of script
小程序 canvas 画布半圆环
What are the common proxy IP problems?
【dpdk】10. Dpdk DNS learning notes
php 2022年4月20面试题整理
RT_Thread自问自答
005_ redis_ Set set
Today will finally write system out. Println()
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
Flink real-time data warehouse project - Design and implementation of DWS layer
Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
PTA: praise the crazy devil
013_ Analysis of SMS verification code login process based on session
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
Use of push() and pop()
What are the test steps of dynamic proxy IP?
Introduction to esp32 Bluetooth controller API