//app.js App({ onLaunch: function () { // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId } }) // 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId this.globalData.userInfo = res.userInfo console.log(this.globalData.userInfo) const db = wx.cloud.database() db.collection("userInfoData").get({ success: function (user) { if (user.data.length == 0) { db.collection("userInfoData").add({ data: { publisherId: [] }, success: function () { this.globalData.publisherId = [] if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } }.bind(this) }) } else { this.globalData.publisherId = user.data[0].publisherId if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }.bind(this) }) } }) } } }) if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力') } else { wx.cloud.init({ // env 参数说明: // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源 // 此处请填入环境 ID, 环境 ID 可打开云控制台查看 // 如不填则使用默认环境(第一个创建的环境) env: "msg-push-9ga0xxc9c4aa5242", traceUser: true, }) wx.cloud.callFunction({ name: "getOpenId", success: function (res) { this.globalData.openId = res.result.openId }.bind(this) }) } }, globalData: { userInfo: null, openId: null, publisherId: null, noticeIndex: null, vibrate: null } })