logo

Timeless

花开成景,花落成诗

uni-app应用在线升级

uni-app应用在线升级

二月 25, 2019 13 743 2 分钟

安卓更新在应用内下载安装包,分apk整包更新或wgt热更新。只是前端业务代码、静态资源修改,且App模块、权限不变,打包引擎版本和基座SDK版本一致,可以使用wgt热更新。

安卓更新在应用内下载安装包,分apk整包更新或wgt热更新。只是前端业务代码、静态资源修改,且App模块、权限不变,打包引擎版本和基座SDK版本一致,可以使用wgt热更新。

IOS更新需要跳转app store。

参考官方插件:https://ext.dcloud.net.cn/plugin?id=4542

对比版本号

/**
 * 对比版本号
 * 支持比对	("3.0.0.0.0.1.0.1", "3.0.0.0.0.1")	("3.0.0.1", "3.0")	("3.1.1", "3.1.1.1") 之类的
 * @param {Object} v1
 * @param {Object} v2
 * v1 > v2 return 1
 * v1 < v2 return -1
 * v1 == v2 return 0
 */
export function compare(v1 = '0', v2 = '0') {
  v1 = String(v1).split('.')
  v2 = String(v2).split('.')
  const minVersionLens = Math.min(v1.length, v2.length);

  let result = 0;
  for (let i = 0; i < minVersionLens; i++) {
    const curV1 = Number(v1[i])
    const curV2 = Number(v2[i])

    if (curV1 > curV2) {
      result = 1
      break;
    } else if (curV1 < curV2) {
      result = -1
      break;
    }
  }

  if (result === 0 && (v1.length !== v2.length)) {
    const v1BiggerThenv2 = v1.length > v2.length;
    const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
    for (let i = minVersionLens; i < maxLensVersion.length; i++) {
      const curVersion = Number(maxLensVersion[i])
      if (curVersion > 0) {
        v1BiggerThenv2 ? result = 1 : result = -1
        break;
      }
    }
  }

  return result;
}

检查更新

判断是否需要apk整包更新或wgt热更新,如果有更新则检查本地是否有下载好的安装包。

/**
 * 检查更新
 */
checkUpgrade() {
  plus.runtime.getProperty(plus.runtime.appid, async widgetInfo => {
    const params = {
      appId: plus.runtime.appid,
      appVersion: plus.runtime.version, // 2.0.12.6
      wgtVersion: widgetInfo.version // 2.0.12.7
    }
    uni.showLoading({
      title: '正在检查更新'
    })
    await CheckAppUpgrade(params).then(res => {
      this.packageInfo = res.data
      if(this.packageInfo.fileUrl) {
        this.checkLocalStoragePackage()
      } else {
        // 已经是最新版本
      }
    }).catch(() => {})
    uni.hideLoading()
  })
}

checkLocalStoragePackage() {
  // 如果已经有下载好的包,则直接提示安装
  const localFilePathRecord = uni.getStorageSync(localFilePathKey)
  if (localFilePathRecord) {
    const {
      version,
      savedFilePath,
      installed
    } = localFilePathRecord
    
    // 比对版本
    const newVersion = this.packageInfo.version // 2.0.12.6
    if (!installed && compare(version, newVersion) === 0) {
      this.downloadSuccess = true;
      this.installForBeforeFilePath = savedFilePath;
      this.tempFilePath = savedFilePath
    } else {
      // 如果保存的包版本小 或 已安装过,则直接删除
      this.deleteSavedFile(savedFilePath)
    }
  }
}

deleteSavedFile(filePath) {
  uni.removeStorageSync(localFilePathKey)
  return uni.removeSavedFile({
    filePath
  })
}

下载安装包

展示下载进度建议使用uni-app官方进度条组件。

let downloadTask = null
downloadPackage() {
  this.downLoadPercent = 0
  this.downloading = true;

  //下载包
  downloadTask = uni.downloadFile({
    url: this.packageInfo.fileUrl,
    success: res => {
      if (res.statusCode == 200) {
        this.downloadSuccess = true;
        this.tempFilePath = res.tempFilePath
      } else {
        // 下载失败,请稍后重试
      }
    },
    fail: () => {
      // 下载失败,请检查网络
    },
    complete: () => {
      this.downloading = false;

      this.downLoadPercent = 0
      this.downloadedSize = 0
      this.packageFileSize = 0

      downloadTask = null;
    }
  });

  downloadTask.onProgressUpdate(res => {
    this.downLoadPercent = res.progress;
    this.downloadedSize = (res.totalBytesWritten / Math.pow(1024, 2)).toFixed(2);
    this.packageFileSize = (res.totalBytesExpectedToWrite / Math.pow(1024, 2)).toFixed(2);
  });
}

安装应用包

wgt热更新安装完需要重启,apk整包更新直接拉取系统安装

installPackage() {
  // #ifdef APP-PLUS
  // wgt资源包安装
  if (this.isWGT) {
    this.installing = true;
  }

  plus.runtime.install(this.tempFilePath, {
    force: false
  }, async res => {
    this.installing = false;
    this.installed = true;

    // wgt包,安装后自动重启
    if (this.isWGT) { 
      uni.showLoading({
        icon: 'none',
        title: '安装成功,正在重启…'
      })
      
      setTimeout(() => {
        uni.hideLoading()
        this.restart();
      }, 1000)
    } else {
      const localFilePathRecord = uni.getStorageSync(localFilePathKey)
      uni.setStorageSync(localFilePathKey, {
        ...localFilePathRecord,
        installed: true
      })
    }
  }, async err => {
    // 如果是安装之前的包,安装失败后删除之前的包
    if (this.installForBeforeFilePath) {
      await this.deleteSavedFile(this.installForBeforeFilePath)
      this.installForBeforeFilePath = '';
    }

    // 安装失败需要重新下载安装包
    this.installing = false;
    this.installed = false;
    this.downloadSuccess = false;

    uni.showModal({
      title: `更新失败${this.isWGT ? '' : ',APK文件不存在'},请重新下载`,
      content: err.message,
      showCancel: false
    });
  });

  // 非wgt包,安装跳出覆盖安装,此处直接返回上一页
  if (!this.isWGT) {
    this.show = false
  }
  // #endif
}

 

恰饭区

广告位招租

评论

Power by Node.js + Nuxt.js,由 腾讯云 腾讯云提供服务, 已经稳稳地存活了 8年95天

Copyright © 2018 - 2026 Timeless. All rights reserved.桂ICP备18003656号-1公网安备桂公网安备45092102000147号