<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>
uniapp wgt,apk升级
最新推荐文章于 2024-06-22 23:46:25 发布