uniapp wgt,apk升级

<template>
	<view>
		<view>version {{versions}}</view>
		<view>appis {{appids}}</view>
		<view>wgtversion {{wgtversion}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				versions: '',
				appids: '',
				wgtversion: '',
				packageInfo: {
					title: '升级', // 标题
					contents: '该升级了', // 升级内容
					is_mandatory: '1', // 是否强制更新
					url: '', // 安装包下载地址
					platform: 'android', // 安装包平台
					type: 'wgt' // 安装包类型
				},
				wgturl: 'http://www.www.top/index.php'
			}
		},
		
		
		
		onLoad() {
			this.versions = plus.runtime.version
			this.appids = plus.runtime.appid
			plus.runtime.getProperty(this.appids, function (wgtinfo) {
				this.wgtversion = wgtinfo.version
			});
			
			uni.request({
			    url: this.wgturl,
			    success: (res) => {
			        this.packageInfo.url = res.data;
					console.log(this.packageInfo.url)
					this.upVersion(this.packageInfo)
			    }
			})
		},
		
		methods: {
			
			upVersion(packageInfo){
				const {
					title, // 标题
					contents, // 升级内容
					is_mandatory, // 是否强制更新
					url, // 安装包下载地址
					platform, // 安装包平台
					type // 安装包类型
				} = packageInfo;
				if(!url) return
			
				let isWGT = type === 'wgt'
				let isiOS = !isWGT ? platform.includes('iOS') : false;
				let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'
										
				uni.showModal({  
					title:title,  
					content: contents,  
					confirmText: confirmText,
					success: (res) => {
						if (res.cancel) return;
						
						// 安装包下载
						if (isiOS) {
							plus.runtime.openURL(url);
							return;
						}
						
						const dtask = plus.downloader.createDownload(url, {force: true}, function ( d, status ) {
							if (status == 200) { // 下载成功    
							   var path = (d.filename);    
							   plus.runtime.install(path,{ force: true},function(){
								  plus.nativeUI.closeWaiting();
								  console.log("安装wgt文件成功!");
								  plus.nativeUI.alert("应用资源更新完成!",function(){
									  plus.runtime.restart();
								  });
							  },function(e){
								  plus.nativeUI.closeWaiting();
								  console.log("安装wgt文件失败["+e.code+"]:"+e.message);
								  plus.nativeUI.alert("安装wgt文件失败["+e.code+"]:"+e.message);
							  });
							} else {//下载失败    
							   alert( "Download failed: " + status );     
							}      
						})
						 
						dtask.start();   
						let prg = 0;  
						let showLoading = plus.nativeUI.showWaiting("正在下载");  
						dtask.addEventListener('statechanged', function(task, status) {  
							// 给下载任务设置一个监听 并根据状态  做操作  
							switch (task.state) {  
								case 1:  
								  showLoading.setTitle("正在下载");  
								  break;  
								case 3:  
								  prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);  
								  showLoading.setTitle("  正在下载" + prg + "%  ");  
								  break;  
								case 4:  
								   plus.nativeUI.closeWaiting();  
									//下载完成  
								  break;  
							}  
						})
					}  
				})
			}	
		}
	}
	
	
	
</script>

<style>

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值