一般写在App.vue里,根据实际情况写在onShow
或者onLoad
里
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
this.version = widgetInfo.version
console.log('this.version==',this.version)
uni.request({
url: '自己url',
success: (resa) => {
let res = resa.data.result
console.log('res===',res)
function compareVersion(version1, version2) {
const newVersion1 = `${version1}`.split('.').length < 3 ? `${version1}`.concat('.0') : `${version1}`;
const newVersion2 = `${version2}`.split('.').length < 3 ? `${version2}`.concat('.0') : `${version2}`;
//计算版本号大小,转化大小
function toNum(a){
const c = a.toString().split('.');
const num_place = ["", "0", "00", "000", "0000"],
r = num_place.reverse();
for (let i = 0; i < c.length; i++){
const len=c[i].length;
c[i]=r[len]+c[i];
}
return c.join('');
}
// 检测版本号是否需要更新
function checkPlugin(a, b) {
const numA = toNum(a);
const numB = toNum(b);
return numA > numB ? 1 : numA < numB ? -1 : 0;
}
return checkPlugin(newVersion1 ,newVersion2);
}
if (res.code == 1) {
// 1代表app新包版本号大于本地版本号
if (compareVersion(res.appVersion, this.version) === 1) {
uni.showModal({
title: '提示',
showCancel:false,
content: '发现新的应用安装包,点击确定立即更新',
success: function (resModal) {
if (resModal.confirm) {
uni.showLoading({
title: '更新中……'
})
uni.downloadFile({
// 存放最新安装包的地址
url: res.url,
success: (downloadResult) => {
uni.hideLoading();
console.log('downloadResult===',downloadResult)
if (downloadResult.statusCode === 200) {
uni.hideLoading();
plus.runtime.install(downloadResult.tempFilePath,{
force: false
}, function() {
console.log('install success...');
plus.runtime.restart();
}, function(e) {
uni.hideLoading();
console.error('install fail...');
});
}
}
});
}
}
});
} else {
}
}
}
});
});