小程序获取openId

1、小程序获取微信openId
 
wx.login({
  success: res => {
    // 发送 res.code 到后台换取 openId, sessionKey, unionId


    // var appid = 'wx958dfed141094376'; //填写微信小程序appid
    // var secret = '0e0caf2901e855bb18718c8ce479cd6d'; //填写微信小程序secret

    if (res.code) {
    //调用request请求api转换登录凭证
    wx.request({

      url: 'https://api.weixin.qq.com/sns/jscode2session',
      data: {
        //小程序唯一标识
        appid: 'wx958dfed141094376',
        //小程序的 app secret
        secret: '0e0caf2901e855bb18718c8ce479cd6d',
        grant_type: 'authorization_code',
        js_code: res.code
      },
      method: 'GET',
      header: { 'content-type': 'application/json' },

      success: function (res) {
      // console.log(res.data.openid) //获取openid
        this.globalData.openId = res.data.openid;
      }
    })
  }
 
  }
})
2、需要设置如下图所示
 
原文地址:https://www.cnblogs.com/yang-shuai/p/8039153.html