微信小程序系列二:获取用户openid

openid作为用户在小程序的唯一标识,获取方法如下:

小程序端操作:app.js

App({
   onLaunch: function () {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: '后台接口',
          data: {
            code: res.code
          },
          success: function (res) {
           
          },
          fail: function (err) {
            
          }
        })
      }
    })  
  }
})

后台操作:

详细操作请看官方文档

猜你喜欢

转载自blog.csdn.net/stp_zsj/article/details/81741571