微信小程序检查更新

kyaa111 1年前 ⋅ 253 阅读

当小程序版本低于指定的最低版本时, 通知其进行更新

let remoteVersion = await api.minVersion()
let localVersion = version.split('.')
let needUpdate = false
for (let i = 0; i < localVersion.length; i++) {
    if (localVersion[i] < remoteVersion[i]) {
        needUpdate = true
        break
    }
}
if (needUpdate) {
    if (wx.canIUse('getUpdateManager')) {
        wx.getUpdateManager().onCheckForUpdate(function (res) {
            if (res.hasUpdate) {
                wx.showLoading();
                updateManager.onUpdateReady(function () {
                    wx.hideLoading()
                    updateManager.applyUpdate()
                })
                updateManager.onUpdateFailed(function () {
                    wx.hideLoading()
                    wx.showModal({
                        title: '已经有新版本了',
                        content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开',
                    })
                })
            }
        })
    }
}