微信小程序 获取用户openid
前言
示例
- 在小程序代码中使用wx.login获取code传递到后端接口调用。
 - 记得小程序中要调用第三方接口需要设置白名单。
 
onLoad() {
          
   
    wx.login({
          
   
      success: function(res) {
          
   
          if (res.code) {
          
   
              wx.request({
          
   
                  url: https://xxx.xx.xx.com/api/openId,
                  data: {
          
   
                      js_code: res.code
                  },
                  method: POST,
                  header: {
          
   
                      content-type: application/json
                  },  
              })
          }
      }
  })
}, 
下面是nodejs服务端代码示例。
- 这是个get请求,除了code还需要三个参数分别为appid、secret、grant_type,这里涉及到知识点:。
 - 得到openid可以进行其他需要openid的接口操作。
 
