今天在做新功能时,用到小程序的获取用户手机号API,如下:

getPhoneNumber: function(e) {     console.log(e.detail.errMsg)     console.log(e.detail.iv)     console.log(e.detail.encryptedData) }复制代码

拿到encryptedData和iv后,就可以在服务端进行解密,解密过程略过~

问题来了,官方给出说明是open-type=getPhoneNumber属性是1.2.0版本开始支持的,所以,1.2.0之前要自己做兼容处理。

然后就用了wx.canIUse进行判断,折腾了一会儿,发现不对,1.5.2基础库上返回也是false,如下:

官方给出说法,建议使用wx.getSystemInfo里面的SDKVersion进行判断。

wx.getSystemInfo的返回值:

wx.getSystemInfo({  success: function(res) {    console.log(res.model)    console.log(res.pixelRatio)    console.log(res.windowWidth)    console.log(res.windowHeight)    console.log(res.language)    console.log(res.version)    console.log(res.platform)    console.log(res.SDKVersion)  }})复制代码

SDKVersion中1.1.0中才出现,所以要做个简单处理,把version转成int,然后进行判断:

function getSDKVersion(SDKVersion){  if (SDKVersion){    SDKVersion = parseInt(SDKVersion.replace(/./g, ''));  }  else SDKVersion = 0;  return SDKVersion;}复制代码

判断sdk版本:

var self = this;wx.getSystemInfo({  success: function(res){    var SDKVersion = res.SDKVersion;    SDKVersion = util.getSDKVersion(SDKVersion);    console.log(res);    console.log(SDKVersion);    if(SDKVersion 120){   // 1.2.0以下版本不支持获取手机号功能      self.setData({        noSupportPhone: true      })    }  }});复制代码

后面就是你自己去处理了~

欢迎关注我:

TAG标签: 微信小程序caniuse

http://www.xiaochengxub.com/read-104885.html