1、在app.vue里面获取当前版本号
plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
uni.setStorageSync('version', wgtinfo.version)
})
2、在首页获取到后端返回的最新版本号做对比(不一定非要是首页,在哪里都行)
getSysVersion() {
const _this = this;
//获取缓存中,当前app的版本号
this.version = uni.getStorageSync('version')
console.log(this.version);
this.$u.post('versionUpgrade').then(res => {
console.log(this.version);
console.log(String(res.data.versionNumber));
if (this.version != res.data.versionNumber) {
// 强制更新
if (res.data.versionDemand == 0) {
uni.showModal({
title: "发现新版本",
content: "确认下载更新",
success: (res1) => {
if (res1.confirm == true) {
//当用户确定更新,执行更新,下面方法进行下载app
this.appdownLoad(res.data.versionAddress);
} else {
// 取消后退出应用
if (uni.getSystemInfoSync().platform == 'ios') {
plus.ios.import("UIApplication").sharedApplication()
.performSelector("exit")
} else if (uni.getSystemInfoSync().platform == 'android') {
plus.runtime.quit();
}
}
}
})
// 非强制更新
} else if (res.data.versionDemand == 1) {
uni.showModal({
title: "发现新版本",
content: "确认下载更新",
success: (res1) => {
if (res1.confirm == true) { //当用户确定更新,执行更新,下面方法进行下载app
this.appdownLoad(res.data.versionAddress);
} else {
return false
}
}
})
}
}
})
},
3、用户点击确认后开始更新
appdownLoad(url) {
var that = this;
uni.showLoading({
title: '安装包下载中……',
mask: true,
})
const downloadTask = uni.downloadFile({
url: url, //服务器 app 的下载地址
success: (downloadResult) => {
uni.hideLoading();
console.log(downloadResult)
if (downloadResult.statusCode === 200) {
uni.showModal({
title: '',
content: '下载成功,是否允许安装新版本?',
confirmText: '确定',
success: function(res) {
if (res.confirm == true) {
plus.runtime.install( //安装
downloadResult.tempFilePath, {
force: true
},
function(res) {
utils.showToast('更新成功,重启中');
plus.runtime.restart();
}
);
}
}
});
}
}
});
},