云函数获取openid
代码如下: app.js: //如果担心openid的安全,就用这个函数
getCloudOpenid: async function () {
return this.openid = this.openid || (await wx.cloud.callFunction({name: 'login'})).result.OPENID
},
//最佳方案。
getOpenid: async function () {
(this.openid = this.openid || wx.getStorageSync('openid')) || wx.setStorageSync('openid', await this.getCloudOpenid())
return this.openid
},
任何page: onLoad: async function () {
console.log(this.openid = await getApp().getOpenid())
},
//在本page的其他函数里获得openid。
yourFunc: function(){
console.log(this.openid)
}
云函数login: const cloud = require('wx-server-sdk')
cloud.init()
exports.main=async()=>{return cloud.getWXContext()}