小程序获得openid实例

时间:2019-03-25 20:52:21 类型:微信小程序
字号:    
小程序获得openid实例
App({
  onLaunch: function () {
    var _this = this;
    wx.login({
      //获取code
      success: function (res) {
        //得到openid 开始
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            appid: _this.globalData.appid,
            secret:_this.globalData.appsecret,
            js_code:res.code,
            grant_type:'authorization_code'
          },
          method:'GET',
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);
            var openid = res.data.openid; //返回openid
          }
        })
        //得到openid结束
      }
    })
    //用户登陆结束
  },
  globalData: {
    appid:'wx99777c04115e0238',
    appsecret:'4c0f48c8003550b399aab023424sdf',
   
  }
})