//app.js App({ onLaunch: function () { this.getOpenid(); }, getOpenid: function () { var openid; var _this = this; wx.getStorage({ key: 'openid', success: function (res) { openid = res.data _this.globalData.openid = openid }, fail: res => { // 登录 wx.login({ success: res => { console.log(res) // 发送 res.code 到后台换取 openId, sessionKey, unionId wx.request({ url: 'https://caos.tederen.com/api/openid', method: 'GET', data: { code: res.code }, success: res => { if (res.data.data.openid) { _this.globalData.openid = res.data.data.openid wx.setStorage({ key: 'openid', data: res.data.data.openid, }) } } }) } }) } }) }, globalData: { userInfo: null, host:'https://caos.tederen.com', openid: '', } })