今天在写EmlogAPP在线更新功能的时候有用到版本比较,记录一下
给你们看看效果吧
将两个版本号拆成数字
依次比较版本号每一位大小,当对比得出结果后跳出循环
比较方法来自:CSDN作者luviaWu大佬
compare(curV, reqV) {
if (curV && reqV) {
//将两个版本号拆成数字
var arr1 = curV.split('.'),
arr2 = reqV.split('.');
var minLength = Math.min(arr1.length, arr2.length),
position = 0,
diff = 0;
//依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍)
while (position < minLength && ((diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0)) {
position++;
}
diff = (diff != 0) ? diff : (arr1.length - arr2.length);
//若curV大于reqV,则返回true
return diff > 0;
} else {
return false;
}
},
version() {
var flg = this.compare(this.appData.data.appVersion, this.banbenhao)
if (flg) {
this.upgradeType = 'wgt';
this.upgradeUrl = this.appData.data.appUrl;
this.$refs.yomolUpgrade.show();
}else{
uni.showToast({
title:"已经是最新版本",
icon:'success'
})
}
},