App.vue 直接上代码
<script>
export default {
methods: {
//APP 版本升级
Urlupload() {
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (info) => {
// 版本号变量持久化存储
getApp().globalData.version = info.version;
this.ToLoadUpdate(info.versionCode, );
})
//#endif
},
//下载更新
ToLoadUpdate(version, ) {
uni.showModal({
title: `版本更新 ${version}—>${171}`,
content: "有更新版本啦!!!",
confirmText: '立即更新',
cancelText: '稍后更新',
showCancel: true,
success: (res) => {
if (res.confirm) {
if (plus.networkinfo.getCurrentType() != 3) { //获取手机设备的相关信息,判断是否在Wifi状态。
uni.showToast({
title: '检测到您目前非Wifi连接,为节约您的流量,建议您连接WIFI更新!',
icon: 'none',
duration: 5000
});
} else {
uni.showToast({
title: '程序已启动更新,请耐心等待!',
icon: 'none',
duration: 3000
});
}
//设置 最新版本apk的下载链接
const downloadApkUrl =
'https://wfgtest-1631.oss.wefanbot.com/DEV/qw/106658768610001/1727229160089/wczd_v1.0.0.apk'
console.log("downloadApkUrl===", downloadApkUrl);
var dtask = plus.downloader.createDownload(downloadApkUrl, {}, (
d, status) => { //新建下载任务
if (status == 200) { //当下载完成
uni.showModal({
title: '下载成功',
content: '确定现在安装吗?',
confirmText: '立即安装',
confirmColor: '#EE8F57',
success: (res2) => {
if (res2.confirm == true) {
plus.runtime.install(
plus.io
.convertLocalFileSystemURL(
d.filename
), {}, {},
(error) => { //安装应用
uni.showToast({
title: '安装失败',
icon: 'none'
});
})
}
}
})
} else {
uni.showToast({
title: '更新失败',
icon: 'none'
});
}
})
dtask.start();
var prg = 0;
var showLoading = plus.nativeUI.showWaiting("正在下载");
dtask.addEventListener('statechanged', (task,
status) => { //添加下载任务事件监听器
// 给下载任务设置一个监听 并根据状态 做操作
switch (task.state) {
case 1:
showLoading.setTitle("开始下载");
break;
case 2:
showLoading.setTitle("已连接到服务器");
break;
case 3:
prg = parseInt( //下载的进度
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
);
showLoading.setTitle("版本更新,正在下载" + prg +
"% ");
if (prg === 100) {
plus.nativeUI.closeWaiting(); //关闭系统提示框
}
break;
case 4:
plus.nativeUI.closeWaiting(); //关闭系统提示框
//下载完成
break;
}
});
} else if (res.cancel) {}
}
});
},
},
onLaunch() {
this.Urlupload()
},
}
</script>
效果图