uniapp通过fir.im 实现应用更新

1 篇文章 0 订阅
1 篇文章 0 订阅

fir.im官方文档:fir.im - 免费应用内测托管平台|iOS应用Beta测试分发|Android应用内测分发 (betaqr.com)https://www.betaqr.com/docs

1.获取本地app版本

    // 获取本地应用资源版本号
    plus.runtime.getProperty(plus.runtime.appid, (info) => {
	    console.log(JSON.stringify(info)); //本地应用信息
	    this.version = info.version.replace(/\./g,'')
	    this.versionCode = info.versionCode
	}) 

version一般是1.0.0、1.0.1,比较难直接比较,本文使用replace(/\./g,'')将‘.’去掉,直接比较100和101,如有bug恳请大家在评论区告诉我,谢谢!

2.利用fir.im查询最新app

uni.request({
    //url参数外的{{}}要去掉,只保留参数即可,id和api_token在fir里可以查
	url:'http://api.bq04.com/apps/latest/{{id}}?api_token={{api_token}}'
}).then(res=>{
	let data = res[1].data
	if(data.versionShort.replace(/\./g,'')>this.version || data.version>this.versionCode){
		//fir上有新版本,执行下载方法,可根据自身情况添加确认框等
        //执行3.下载和安装
	}
	else{
		this.$u.toast('已经是最新版本!')
	}
})

附:id和api_token查询位置

 3.下载和安装(Android)

//第一步 获取 download_token
uni.request({
	url:'http://api.bq04.com/apps/{{id}}/download_token?api_token={{api_token}}'
}).then(res=>{

//第二步 下载
	var downloadTask = uni.downloadFile({
	    url: 'http://download.bq04.com/apps/{{id}}/install?download_token='+res[1].data.download_token,
	    success: (res) => {
		    console.log(res)
		    this.u = res.tempFilePath
	    },
	    fail: (res) =>{
		    console.log(res)
	    }
    })
    //监听下载进度
    downloadTask.onProgressUpdate((res) => {
	    if(res.progress>=100){
	    	uni.hideLoading();
//第三步 安装
	    	plus.runtime.install(this.u)  
	    }
    });
})

uni.downloadFile 是uniapp官方给的下载方法,具体文档查阅uniapp官网

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值