在首页或者登录页面判断是否有新版本发布,若有则跳到更新页面下载
updateApp() {
uni.getSystemInfo({
success: res => {
//检测当前平台,如果是安卓则启动安卓更新
if (res.platform == 'android') {
// 判断版本问题
plus.runtime.getProperty(plus.runtime.appid, info => {
uni.request({
url: `http://localhost:8080/app-version/getLatestAppVersion&version=${info.version}`,
success: res => {
console.log(res);
if (res.data.code == 0) {
const version = res.data.data.newVersion;
if (version == '1') {
uni.showModal({
title: '版本发布更新',
content: '发现新版本,请进行更新',
confirmText: '更新',
confirmColor: '#EE8F57',
success: res => {
if (res.confirm) {
uni.navigateTo({
url: '/pages/downloadApp/downloadApp'
});
} else if (res.cancel) {
// 在这可以写取消后再次点击更新的逻辑
uni.showToast({
icon: 'none',
title: '你取消了更新'
});
}
}
});
}
}
},
fail(err) {
console.log(err);
}
});
});
}
}
});
},
下载页面
<template>
<view class="container">
<view class="progressBox"><progress :percent="progress" show-info stroke-width="12" /></view>
<text class="textBox">{{ progress >= 100 ? '下载完成' : '下载中....' }}</text>
</view>
</template>
<script>
import httpURL from '../../api/httpURL.js';
export default {
data() {
return {
progress: 0
};
},
/* 生命周期函数--监听页面加载 */
onLoad: function() {
this.doUpData();
},
methods: {
doUpData: function() {
let appUrl = `http://localhost:8080/userlimit/app-version/downloadLatestApp`;
let downloadTask = uni.downloadFile({
url: appUrl,
success: downloadResult => {
console.log(downloadResult);
// 下载成功
if (downloadResult.statusCode === 200) {
uni.showModal({
title: '',
content: '下载成功,确定现在更新吗?',
confirmText: '更新',
confirmColor: '#EE8F57',
success: function(res) {
// console.log(res)
if (res.confirm) {
plus.runtime.install(
//安装
downloadResult.tempFilePath,
{
force: true
},
function() {
console.log('更新成功');
plus.runtime.restart();
uni.navigateBack();
},
function(e) {
console.log('更新失败');
}
);
}
}
});
}
},
complete: () => {
// uni.hideLoading();
}
});
downloadTask.onProgressUpdate(res => {
// console.log('下载进度' + res.progress);
this.progress = res.progress;
// console.log('已经下载的数据长度' + res.totalBytesWritten);
// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
// // 测试条件,取消下载任务。
// if (res.progress > 50) {
// // downloadTask.abort();
// }
});
}
}
};
</script>
<style>
.container {
width: 100%;
}
.progressBox {
padding: 50rpx 20rpx;
}
.textBox {
display: flex;
justify-content: center;
}
</style>