实战:
1、【openId】获取openId
有如下几种方法:
- 通过wx.login()获取临时登录凭证 code,然后通过code2session获取openId
- wx.login():https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
- code2session:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/code2Session.html
- 代码暂略
- 通过云函数
- 云函数中自带openId,直接返回即可
- 云函数:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/capabilities.html#%E4%BA%91%E5%87%BD%E6%95%B0
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
const {
OPENID,
APPID,
UNIONID,
} = cloud.getWXContext()
return OPENID
}