- 使用 wx.login 获取临时登录凭证 code,回传给微信服务器拿到返回值 openid
- 使用云开发获取小程序 openid
步骤:
- 创建云函数
- 使用云函数获取用户 openid
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
// env 参数说明:
// env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
env: ''
// traceUser: true,
})
this.globalData = {
db: wx.cloud.database(),
}
// 判断缓存中是否有appid openid
if (!wx.getStorageSync('idInfo')) {
wx.cloud.callFunction({
name: 'getId',
success: res => {
console.log(res)
this.globalData.userinfo = res.result.event.userInfo
// 写入缓存 ,我这里直接存在本地存储了
wx.setStorageSync('idInfo', this.globalData.userinfo)
},
fail(err) {
console.log(err)
}
})
} else {
this.globalData.userinfo = wx.getStorageSync('idInfo')
}
}
Comments | NOTHING