第一步:通过plus.runtime.getProperty获取app相关信息,checkUpgrade为调用接口事件 plusReady() { var that = this plus.runtime.getProperty(plus.runtime.appid, function(inf) { that.checkUpgrade(inf.version) }) plus.screen.lockOrientation('portrait-primary') // Android返回键监听事件 plus.key.addEventListener( 'backbutton', () => { if (this.$route.meta.index > 0) { // 非顶级页面,调用路由回退 this.$router.back() } else { // 顶级页面,第一次提示,第二次退出 this.backButtonPress++ if (this.backButtonPress > 1) { plus.runtime.quit() } else { plus.nativeUI.toast('再按一次退出应用') } setTimeout(() => { this.backButtonPress = 0 }, 1000) } }, false, )}
第二步:checkUpgrade事件根据封装方法的不同返回相关的版本信息
async checkUpgrade(version) {
const wgtUrl = await checkVersion(version)
installUpgrade(wgtUrl)
},
/** * checkVersion检查是否有新版本 */ export async function checkVersion(ver) { // 当前客户端版本号 // const curVersion = config.ENVS.VERSION // 服务端记录的最新版本号和 wgt 增量包地址 const resData = await versionAPI.getDetail() const { appVersion, wgtUrl } = resData.data.data // console.log(appVersion, 'app') // 如果客户端版本小于服务端最新版本号,需要进行热更 // console.info(`当前版本号: ${curVersion},服务端版本号: ${appVersion}`) // if (getVersionCode(curVersion) < getVersionCode(appVersion.toString())) { // return wgtUrl // } if (ver !== appVersion) { return wgtUrl } else { console.info('跳过更新') return false } }
/** * 安装更新包 */ export async function installUpgrade(wgtUrl) { if (!wgtUrl) { console.warn('更新包地址为空,已跳过更新') return } console.info('准备下载 wgt 文件: ', wgtUrl) const downToak = plus.downloader.createDownload(wgtUrl, {}, (result, status) => { if (status === 200) { // 安装下载的文件 plus.runtime.install( result.filename, {}, () => { Toast({ message: '更新完毕,新版本将在下次启动后应用' }) }, e => console.error('安装 wgt 失败: ', e), ) } else { console.error('下载 wgt 文件失败: ', status) } }) downToak.start() }