uniapp打包app自动更新笔记

App.vue

<script>
	import {baseUrl} from './config/baseUrl.js'
	export default {
		onLaunch: function() {
			
			// #ifdef APP-PLUS
			uni.getSystemInfo({
			  success:(res) => {  
			    //检测当前平台,如果是安卓则启动安卓更新  
				console.log(res.platform=="android")
				
			    if(res.platform=="android"){  
			      this.AndroidCheckUpdate();  
			    }  
			  }  
			})
			// #endif

		},
		methods:{
			AndroidCheckUpdate() {
			  
			  plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
			    let  version = wgtinfo.version //客户端版本号
				console.log(version)
				this.getUpdateVersion(version)
			  })
			  //this.getUpdateVersion()
			},
			async getUpdateVersion(version) {
			  
			  // 获取当前app版本信息
			  let params ={
				projectName:"wendeng",
				actionName:"h5_pda_deposit",   // 寄存PDA
				appType:"android",
				appVersion:version,
				clientType:"byg" 
			  }
			   let result = await uni.request({
			   	url:`${baseUrl}/client/init`,
				data:params,
				method:'POST',
			   })
			   console.log(result,'接口')
			   let res = result[1].data
			 
			   if(res.code ==1){
				   let {updateFlag, apkUrl} = res.data
				 //请求成功
				 // // 默认是不更新 是否强制更新 1=不升级,2=强制升级,3=可以升级
				 if(updateFlag!=1){
					 //更新
					 this.downWgt(apkUrl)
				 }
			   }else{
				   //请求失败
				 //   uni.showToast({
				 //   	title:res.message,
					// icon:'error'
				 //   })
				   return 
			   }
			   // uni.showToast({
			   // 	title:JSON.stringify(res),
			   // 	icon:"none",
			   // 	duration:100000000000000000000
			   // })
			  // alert('res.data:' + JSON.stringify(res.data))
			},
			downWgt(url) {
			 console.log(url,'开始下载')
			  uni.showLoading({
			    title: '更新中……'
			  })
			
			  const downloadTask = uni.downloadFile({//执行下载
			    url: url, //下载地址
			    timeout: 1000 * 60, //60秒超时时间
			    success: downloadResult => {//下载成功
			    
			      uni.hideLoading();
			     
				 
				  console.log(downloadResult.statusCode,'下载状态')
			      if (downloadResult.statusCode == 200) {
			       
			        uni.showModal({
			          title: '',
			          content: '更新成功,确定现在重启吗?',
			          confirmText: '重启',
			          confirmColor: '#EE8F57',
			          success: function(res) {
			            if (res.confirm == true) {
			              plus.runtime.install(//安装
			                downloadResult.tempFilePath, {
			                  force: true
			                },
			                function(res) {
			                
			                  plus.runtime.restart();
			                }
			              );
			            }
			          }
			        });
			      }
			    },
			    fail: err => {
			      uni.hideLoading();
			      console.log(err)
			    },
			    complete: com => {
					uni.hideLoading();
			    }
			  });
			
			  // 下载进度
			//   downloadTask.onProgressUpdate(res => {
			//     // that.$u.toast(res.progress)
			//     that.downloadNum = res.progress
			//     console.log('下载进度' + that.downloadNum);
			//     // console.log('已经下载的数据长度' + res.totalBytesWritten);
			//     // console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
			
			//     // 满足测试条件,取消下载任务。
			//     // if (res.progress > 50) {
			//     // 	downloadTask.abort();
			//     // }
			//   });
			},

		},
		
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		
	}
</script>

<style lang="scss">
	/* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-ui/index.scss";
	page{
		width:100%;
		height: 100%;
		color: #030E1C;
	}
	body{
		width:100%;
		height: 100%;
	}
	view{
		box-sizing: border-box;
	}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值