微信小程序版本號比較

首先獲取到當前版本號 :

wx.getSystemInfo({
  success (res) {
    console.log(res.model)   //獲取設備名:iPhone,Meizu......
    console.log(res.version)  //獲取版本號
  }
})

 版本號比較:

function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  var len = Math.max(v1.length, v2.length)
 
  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }
 
  for (var i = 0; i < len; i++) {
    var num1 = parseInt(v1[i])
    var num2 = parseInt(v2[i])
 
    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }
  return 0
}
 
compareVersion('7.0.5', '6.0.6') // => 1 // 1表示 1比2要新
compareVersion('7.0.5', '1.11.0') // => 0 // 0表示1和2是同一個版本
compareVersion('7.0.5', '6.0.6') // => -1 // -1表示1比 2要老

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章