当前位置:网站首页>[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
边栏推荐
- 012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
- 都是做全屋智能的,Aqara和HomeKit到底有什么不同?
- tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Redis memory recycling strategy
- 001_ Redis set survival time
- JDBC cannot connect to MySQL, and the error is access denied for user 'root' @ '* * *' (using password: Yes)
- 数仓建表111111
- Halo open source project learning (I): project launch
- R language advanced | generalized vector and attribute analysis
猜你喜欢

010_ StringRedisTemplate

PTA: praise the crazy devil

Halo open source project learning (I): project launch

Leetcode40 - total number of combinations II

每日一题冲刺大厂第十六天 NOIP普及组 三国游戏

005_ redis_ Set set

leetcode:27. 移除元素【count remove小操作】

Shardingsphere read write separation

RT_Thread自问自答

A simple and open source navigation website source code
随机推荐
001_redis设置存活时间
Heap overflow of kernel PWN basic tutorial
LeetCode 349. Intersection of two arrays (simple, array) Day12
[NK] Niuke monthly race 48 D
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
Introduction to esp32 Bluetooth controller API
Network jitter tool clumsy
Campus transfer second-hand market source code
Latin goat (20204-2022) - daily question 1
JDBC cannot connect to MySQL, and the error is access denied for user 'root' @ '* * *' (using password: Yes)
Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
假如404页面是这样的 | 每日趣闻
Easyswool environment configuration
Go language ⌈ mutex and state coordination ⌋
007_Redis_Jedis连接池
What problems will you encounter when dialing VPS?
The importance of ERP integration to the improvement of the company's system
Usage of vector common interface
Open3d point cloud processing
【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views