uniapp APP版本自动更新安装的方法应用

<script>
export default {
  data() {
    return {
      androidVersion: '',
      newappUrl: '',
      getVersion: '',
    }
  },
  methods: {
    AndroidCheckUpdate() {
      this.http.post('api/App_Update/GetAppVersion', {}, true).then((res) => {
        //后端传入数据进行处理
        // this.newappUrl = this.http.ipAddress+res.rows.AppUrl;//apk下载链接
        this.newappUrl = res.rows.AppUrl //apk下载链接
        //后端传入最新版本的版本号,每次发布新版都要修改
        this.getVersion = res.rows.Versions
        console.log('res', this.newappUrl, this.getVersion)
        //对版本号进行对比
        if (parseFloat(res.rows.Versions) > parseFloat(this.androidVersion)) {
          uni.showModal({
            content: '当前版本为旧版,邀您更新最新版本',
            success: (res) => {
              if (res.confirm) {
                uni.showLoading({
                  title: '正在更新中'
                })
                this.upload()
              } else if (res.cancel) {
                uni.hideLoading()
              }
            }
          })
        }
      })
    },
    upload() {
      //使用下载api(uni.downloadFile)
      let uploadTask = uni.downloadFile({
        url: this.newappUrl,
        success: (downloadResult) => {
          this.cs = downloadResult
          //使用安装wgtapi(plus.runtime.install)
          plus.runtime.install(downloadResult.tempFilePath, { force: true }, function () {
            uni.hideLoading()
            uni.showModal({
              title: '系统提示',
              content: '新版本已经更新完成,需要重启应用',
              showCancel: false,
              //用户点击确认重启
              success: function (res) {
                if (res.confirm) {
                  // 调用重启api(plus.runtime.restart)
                  plus.runtime.restart()
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          })
        },
        fail() {
          plus.nativeUI.toast('安装失败')
        }
      })

      //在这里监听用户下载进度
      uploadTask.onProgressUpdate((res) => {
        console.log('rr23', res)
        this.pro = res.progress
        console.log(this.pro)
        if (this.pro == 100) {
          this.isDown = false
        }
      })
    },
    
  },
  onLaunch() {
    // #ifdef APP-PLUS
    const script = document.createElement('script')
    script.src = './static/config.js'
    document.head.appendChild(script)
    // #endif
   
  },
  created() {
    // #ifdef APP-PLUS
    this.androidVersion = plus.runtime.version //获取版本,并且记录
    uni.getSystemInfo({
      success: (res) => {
        if (res.platform == 'ios') {
        } else if (res.platform == 'android') {
          this.AndroidCheckUpdate()
        }
      }
    })
    // #endif
  },
  onShow() {
    // #ifdef APP-PLUS
    this.androidVersion = plus.runtime.version //获取版本,并且记录
    uni.getSystemInfo({
      success: (res) => {
        if (res.platform == 'ios') {
        } else if (res.platform == 'android') {
          this.AndroidCheckUpdate()
        }
      }
    })
    // this.AndroidCheckUpdate()
    // #endif
 
  },
  onHide: function () {
    console.log('App Hide')
  },
  mounted() {}
}
</script>

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值