2023年11月26日发(作者:)
⼩程序设置⾃动更新
前⾔:我们开发⼩程序的时候,每次更新好发布版本,⽤户都需要把旧版本删除,重新搜索⼩程序才能体验到新版本。
随着更新次数频繁,这样的操作会让⼈很反感,所以就想把项⽬添加⾃动更新。
微信开发者⽂档的API是有关于⾃动更新这⼀块的demo的,下⾯贴⼀下⾃⼰在⽹上搬运的解决⽅法。
1.
if (e('getUpdateManager')) {
const updateManager = ateManager()
kForUpdate(function (res) {
('onCheckForUpdate====', res)
// 请求完新版本信息的回调
if (ate) {
('ate====')
teReady(function () {
dal({
title: '更新提⽰',
content: '新版本已经准备好,是否重启应⽤?',
success: function (res) {
但是会有⼀个缺点,他会在你进⼊⼩程序过个五六秒才会提⽰更新,这很影响⽤户体验的,所以优化⼀下代码
// ⼩程序⾃动更新
onShow: function() {
('App Launch');
App({
autoUpdate: function() {
var self = this
// 获取⼩程序更新机制兼容
if (e('getUpdateManager')) {
const updateManager = ateManager()
//1. 检查⼩程序是否有新版本发布
kForUpdate(function(res) {
// 请求完新版本信息的回调
if (ate) {
//检测到新版本,需要更新,给出提⽰
dal({
title: '更新提⽰',
content: '检测到新版本,是否下载新版本并重启⼩程序?',
success: function(res) {
if (m) {
//2. ⽤户确定下载更新⼩程序,⼩程序下载及更新静默进⾏
adAndUpdate(updateManager)
} else if () {
//⽤户点击取消按钮的处理,如果需要强制更新,则给出⼆次弹窗,如果不需要,则这⾥的代码都可以删掉了
dal({
title: '温馨提⽰~',
content: '本次版本更新涉及到新的功能添加,旧版本⽆法正常访问的哦~',
showCancel:false,//隐藏取消按钮
confirmText:"确定更新",//只保留确定更新按钮
success: function(res) {
if (m) {
//下载新版本,并重新应⽤
adAndUpdate(updateManager)
}


发布评论