獲取用戶openid 需要在售票的時候把該用戶的openid記錄下來。
需要在根目錄新建一個文件夾:functions 然后在projiect.config.json
配置信息:
"cloudfunctionRoot": "functions/",

然后到app.js初始化雲函數:
wx.setStorageSync('logs', logs)
//雲開發初始化
wx.cloud.init({
env: 'xxxxx',
traceUser: true
})
xxxx是打開雲開發控制台,復制環境ID

右鍵點擊functions,點擊新建node.js雲函數
創建名為getOpenid的雲函數
獲取openid的代碼在右邊:
// 雲函數入口文件
const cloud = require('wx-server-sdk')
cloud.init()
//獲取用戶的openid
exports.main = async (event, context) => {
return event.userInfo; //返回用戶信息
}
頁面js獲取用戶openid
Page({ data: { pickerHidden: true, chosen: '', openid:'' }, onLoad: function () { this.getOpenid(); }, // 獲取用戶openid getOpenid() { let that = this; wx.cloud.callFunction({ name: 'getOpenid', complete: res => { console.log('雲函數獲取到的openid: ', res.result.openId) var openid = res.result.openId; that.setData({ openid: openid }) } }) },
最后右鍵點擊雲函數:getOpenid:選擇"上傳並部署:雲端安裝依賴(不上傳node_moudles)"