步骤一:在app.vue 中
//获取本地版本信息
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
console.log(wgtinfo, 'wgtinfowgtinfowgtinfo')
uni.setStorage({
key: 'wgtinfoVersionCode',
data: wgtinfo
})
})
步骤二:在登录页
//获取版本列表
getSysVersion() {
const _this = this;
//获取缓存中,当前app的版本号
uni.getStorage({
key: 'wgtinfoVersionCode',
success: (res) => {
_this.wgtinfoVersionCode = res.data;
uni.request({
url: url,//列表接口地址
method: 'GET',
sslVerify: false,
data: {},
success(res) {
if (res.data.code === 200) {
let appversion = res.data.result.version
var appUrl = res.data.result.fileUrl;
console.log(appUrl, 'appUrlappUrl')
// 后端返回的app版本和当前app版本比较
if (appversion != _this.wgtinfoVersionCode.version) {
uni.showModal({
title: "发现新版本",
content: "确认下载更新",
success: (res) => {
if (res.confirm ==true) { //当用户确定更新,执行更新,下面方法进行下载app
_this.appdownLoad(res.data[0]);
} else {
return false
}
}
})
}
}
},
fail() {
}
})
},
})
},
appdownLoad(url) {
console.log(url, '下载app地址')
var that = this;
uni.showLoading({
title: '安装包下载中……'
})
##下载文件的方法请查看另一篇 下载与保存
plus.runtime.install( //安装
downloadResult
.filename,//文件的保存地址
{
force: true
},
function(res) {
utils.showToast('更新成功,重启中');
plus.runtime.restart();
}
);
}