小程序如何获取用户openid,唯一的,像微信号

小程序如何获取用户openid,唯一的,像微信号

wx.getUserInfo(OBJECT)只能获取nickName,avatarUrl,gender这些

A:openid两种获取方法。

1.login获得code。在用code换openid和一个密匙

2.userinfo有个加密的data。解密获得openid

  1.   onLoad: function () {
  2.     var that = this
  3.     wx.login({
  4.       success: function (res) {
  5.         console.log("res.code====="+res.code);
  6.         if (res.code) {
  7.           //发起网络请求
  8.           wx.request({
  9.             url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxaasdf22we1sdffe3&secret=83ebdsdfsdfa7sdfsdf3448ff3f71&js_code=' + res.code + '&grant_type=authorization_code',
  10.             method: "POST",
  11.             success: function (res) {
  12.                
  13.               that.setData({
  14.                 openid: res.data.openid
  15.               })
  16.             }
  17.           })
  18.         } else {
  19.           console.log('获取用户登录态失败!' + res.errMsg)
  20.         }
  21.       }
  22.     });
  23.   }
复制代码

猜你喜欢

转载自bkjsw.iteye.com/blog/2374565