Use cloud functions to get the openId of the applet user

How to use cloud functions to get the openId of the applet user? Proceed as follows:

  • If you have not opened cloud development, please read this
  • The basic library version needs to be 2.3.3 or higher , and it is recommended to set it to the latest version
  • New cloud function:
  • Right click cloudfunctionsand select新建Node.js云函数
  • enterlogin
    • Now the cloud development console looks like:
      Insert picture description here
  • This function saves the openID to local storage and calls it when an error occurs error:
function saveOpenID(error) {
    
    
  if (!wx.getStorageSync('openID')) {
    
    
    wx.cloud.callFunction({
    
    
      name: 'login',
      data: {
    
    },
      success: res => {
    
    
        console.log('Got user openid: ', res.result.openid)
        wx.setStorageSync('openID', res.result.openid)
      },
      fail: err => {
    
    
        console.error(err)
        error()
      }
    })
  }
}

Guess you like

Origin blog.csdn.net/write_1m_lines/article/details/104158905