我们的机制是这样,app onLaunch初始化代码,当小程序发送某几个请求的时候,去调用updateManager检查更新,模拟器测试没有问题,但是线上不生效,通过log看,显示updateManage.onCheckForUpdate未检查到更新
代码如下:
const STATUS_MAP = { NOTBEGIN: 0, PENDING: 1, SUCCESS: 2, FAILD: 3, } const MAX_FAIL_TIMES = 3; // 运行期间超过三次失败就不再尝试 let infos = {}; let updateManager; let inited = false ; let status = STATUS_MAP.NOTBEGIN; let failCount = 0; const RULES = [ { name: 'x-system' , rule: system => infos.system.toLowerCase().indexOf(system.toLowerCase()) > -1, }, { name: 'x-uid' , rule: uids => { const app = getApp(); const user = app.$user.userId; if (user && uids.split( ',' ).indexOf(user.toString()) > -1) { return true ; } return false ; }, }]; function collectInfo() { return { inited, infos, status, failCount, lastUpdate: wx.getStorageSync( 'lastUpdate' ), }; } function logError(err) { const pages = getCurrentPages(); const len = pages.length; if (!len) return ; err.info = collectInfo(); if ( typeof wx.getRealtimeLogManager === 'function' ) { const logger = wx.getRealtimeLogManager(); logger.error(err.message, err); } } function log(message) { const args = collectInfo(); if ( typeof wx.getRealtimeLogManager === 'function' ) { const logger = wx.getRealtimeLogManager(); logger.info(message, args); } console.log(`-------${message}-------`, args); } function checkRules(headers = {}) { const app = getApp(); const lastUpdate = headers[ 'x-update' ]; if (!lastUpdate || app.lastUpdate >= lastUpdate) { return false ; } for (let i = 0; i < RULES.length; i++) { const item = RULES[i]; const header = headers[item.name]; if (!header) { continue ; } if (header instanceof Array && header.length === 0) { continue ; } if ( typeof header === 'object' && Object.keys(header).length === 0) { continue ; } if (!item.rule(headers[item.name])) { return false ; } } return true } function checkForUpdate(timestamp) { const app = getApp(); if (status === STATUS_MAP.PENDING) return ; status = STATUS_MAP.PENDING; updateManager = wx.getUpdateManager(); updateManager.onCheckForUpdate(res => { if (res && res.hasUpdate) { updateManager.onUpdateReady(() => { wx.showModal({ title: '更新提示' , content: '系统升级,请重启获取最新版本' , showCancel: false , success(modalRes) { if (modalRes.confirm) { app.lastUpdate = timestamp; wx.setStorageSync( 'lastUpdate' , timestamp); // status = STATUS_MAP.SUCCESS; 不能改变状态,否则会再次检查强制更新逻辑 log( 'force-update update success' ); setTimeout(() => { updateManager.applyUpdate(); }, 2000); } }, }) }); updateManager.onUpdateFailed(() => { status = STATUS_MAP.FAILD; failCount += 1; logError({ message: 'force-update update fail' , }); }); } else { status = STATUS_MAP.SUCCESS; app.lastUpdate = timestamp; wx.setStorageSync( 'lastUpdate' , timestamp); log( 'force-update check for no update' ); } }); } module.exports = { init(ctx) { if (inited) return ; try { ctx.lastUpdate = ctx.store.get( 'lastUpdate' ) || 0; status = STATUS_MAP.NOTBEGIN; const { system, version: wechat, SDKVersion: lib } = ctx.system(); infos = { system, wechat, lib, }; inited = true ; } catch (error) { setTimeout(() => { logError({ message: 'force-update init fail' , error, }); }, 2000); } }, checkForceUpdate(headers) { try { if (status === STATUS_MAP.PENDING) { return ; } if (inited && failCount < MAX_FAIL_TIMES && headers[ 'x-update' ] && checkRules(headers)) { // 必须有更新时间,必须已经初始化过,失败次数未超过三次 checkForUpdate(headers[ 'x-update' ]); } } catch (error) { if (status === STATUS_MAP.PENDING) { failCount += 1; status = STATUS_MAP.FAILD; } logError({ message: 'force-update check for update exception' , error, }); } }, } |
这真的仅仅是版本更新的代码?为啥我们的只有3行代码?