### [小程序如何获取手机号信息](https://www.prosaas.cn/article/16791) **Published:** 2020-03-26T03:16:52 **Author:** 5201314 **Excerpt:** 小程序如何获取手机号信息 1、在获取手机号码之前,要先进行登陆。使用wx.login进行登录,登录成功会返回一… ![](https://img.php.cn/upload/article/000/000/040/5e7c1df4b26fb763.jpg) **小程序如何获取手机号信息**     **1、在获取手机号码之前,要先进行登陆。** 使用wx.login进行登录,登录成功会返回一个code,将code传给后台,获取登录密钥session\_key等信息。将这些信息存入data。 推荐学习:小程序开发 **2、使用type="getPhoneNumber"的button获取encryptedData,iv将这两个信息和session\_key传给后台,通过解密返回正确的手机号码。** 通过点击之后获取到的信息为 ![](https://img.php.cn/upload/image/966/422/655/1585192395974372.png) 在 请求后台接口之前要先用wx.checkSession判断登录状态是否过期,如果过期就需要获取新的session\_key。 getPhoneNumber(e) { var msg = e.detail.errMsg, that = this; var that = this; var sessionID=that.data.userinfo.Session\_key, encryptedDataStr=e.detail.encryptedData, iv= e.detail.iv; if (msg == 'getPhoneNumber:ok') { wx.checkSession({ success:function(){ that.deciyption(sessionID,encryptedDataStr,iv); }, fail:function(){ wx.login({ success: res => { console.log(res,'sessionkey过期') wx.request('url',{code:res.code},function(res){ var userinfo=res.data.data; wx.setStorageSync('userinfo',userinfo); that.setData({ userinfo:userinfo }); that.deciyption(userinfo.Session\_key,encryptedDataStr,iv); }) } }) } }) } }, deciyption(sessionID,encryptedDataStr,iv){ wx.request('url', { sessionID: sessionID, encryptedDataStr:encryptedDataStr, iv: iv }, function (res) { //这个res即可返回用户的手机号码 }) }, 以上就是小程序如何获取手机号信息的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** 后端开发 ---