WeChat applet compares users’ WeChat client version numbers

WeChat applet compares users’ WeChat client version numbers

1. Background of the article
There is a reason for this. Recently, a small program was developed to use getPhoneNumber to obtain the user’s mobile phone number. The security upgraded version of getPhoneNumber was used. However, the upgraded method has requirements for the customer’s WeChat App version number!
Insert image description here
2. Practical operation, adding judgment to the code.
Therefore, the WeChat client version number is judged in the entry file app.js of the WeChat applet. The code is as follows

// 微信客户端版本号比较
compareVersion(v1, v2) {
    
    
   v1 = v1.split('.')
   v2 = v2.split('.')
   const len = Math.max(v1.length, v2.length)
 
   while (v1.length < len) {
    
    
     v1.push('0')
   }
   while (v2.length < len) {
    
    
     v2.push('0')
   }
 
   for (let i = 0; i < len; i++) {
    
    
     const num1 = parseInt(v1[i])
     const num2 = parseInt(v2[i])
 
     if (num1 > num2) {
    
    
       return 1
     } else if (num1 < num2) {
    
    
       return -1
     }
   }
 
   return 0
 }

// 获取微信版本
const version = wx.getSystemInfoSync().version // 微信版本号 如:8.0.23
const paltsfrom =  wx.getSystemInfoSync().platform // ios 或 安卓
if(paltsfrom == 'ios' || paltsfrom == 'android'){
    
    
	if (this.compareVersion(version, '8.0.16') >= 0) {
    
    
      this.globalData.isGetPhoneNumber = true
	} else {
    
    
	  // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
	  // wx.showModal({
    
    
	  //   title: '提示',
	  //   content: '当前微信版本过低,部分功能将会无法正常使用,请升级到最新微信版本后重试。'
      // })
      this.globalData.isGetPhoneNumber = false
	}
}

3. Portal, related articles
1. [Announcement-oriented programming] Misunderstandings about getPhoneNumber obtaining mobile phone number after security upgrade
2. Lower version compatibility
3. wx.getSystemInfo(Object object) method

Guess you like

Origin blog.csdn.net/TurtleOrange/article/details/125655795