Getting Mini Program User openId via Cloud Function

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed
  • If cloud development is not enabled, see this article

  • Base library version must be 2.3.3* or higher*, recommended to use latest version

  • Create new cloud function:

  • Right-click cloudfunctions, select New Node.js Cloud Function

  • Enter login

    • Current appearance of cloud development console:
  • This function saves openID to local storage and calls error on failure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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()
      }
    })
  }
}
Built with Hugo
Theme Stack designed by Jimmy