小程序如何获取手机号信息

小程序如何获取手机号信息    

1、在获取手机号码之前,要先进行登陆。

使用wx.login进行登录,登录成功会返回一个code,将code传给后台,获取登录密钥session_key等信息。将这些信息存入da优质源码网点我wcqh.cnta。

推荐学习:小程序开发

2、使用type=”getPhoneNumber”的button获取encryptedData,iv将这两个信息和session_key传给后台,通过解密返回正确的手机号码。优质源码网点我wcqh.cn

1

2

3

<button open-type=”getPhoneNumber” bindgetphonenumber=”getPhoneNumber”>

手机号领取

</button>

登录后复制

通过点击之后获优质源码网点我wcqh.cn取到的信息为

在 请求后台接口之前要先用wx.checkSession判断登录状态是否过期,如果过期就需要获取新的session_key。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

2优质源码网点我wcqh.cn1

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

getPhoneNumber(e) {

var msg = e.detail.errMsg, that = this;

var tha优质源码网点我wcqh.cnt = this;

var sessionID=that.data.userinfo.Session_key,

encryptedDataStr=e.detail.encryptedData,

iv= e.优质源码网点我wcqh.cndetail.iv;

if (msg == &#39;getPhoneNumber:ok&#39;) {

wx.checkSession({

success:function(){

that.deciypti优质源码网点我wcqh.cnon(sessionID,encryptedDataStr,iv);

},

fail:function(){

wx.login({

success: res => {

console.log(res,&#39;优质源码网点我wcqh.cnsessionkey过期&#39;)

wx.request(&#39;url&#39;,{code:res.code},function(res){

var userinfo=res.data.data;优质源码网点我wcqh.cn

wx.setStorageSync(&#39;userinfo&#39;,userinfo);

that.setData({

userinfo:userinfo

});

that.deciyption(use优质源码网点我wcqh.cnrinfo.Session_key,encryptedDataStr,iv);

})

}

})

}

})

}

},

deciyption(sessionID,encryptedDataStr,iv){

wx.reque优质源码网点我wcqh.cnst(&#39;url&#39;, {

sessionID: sessionID,

encryptedDataStr:encryptedDataStr,

iv: iv

}, function (res) {

/优质源码网点我wcqh.cn/这个res即可返回用户的手机号码

})

},

登录后复制

以上就是小程序如何获取手机号信息的详细内容,更多请关注php中文网其它相关文章!

© 版权声明
THE END
喜欢就支持一下吧
点赞996 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容