收藏
回答

如何在onload中获取gloabdata的openid

在app.js 中,通过wx.login和wx.getUserInfo获取了code等,然后在wx.getUserInfo的success中向自己的服务器发起request请求,获得的openid给到globaldata中的openid,但是在index页面中的onload和onready都拿不到openid,但是可以拿到userInfo,


回答关注问题邀请回答
收藏

1 个回答

  • 痛快科技
    痛快科技
    2019-04-17

    在index页面调用openid的时候,要保证服务器请求已经返回给小程序并赋值到globaldata中。

    小程序的很多api都是异步的。

    2019-04-17
    有用
    回复 4
    • 2019-04-17

      wx.getUserInfo({

          success: function (res) {

              // console.log({ encryptedData: res.encryptedData, iv: res.iv, code: code })

              // 3.请求自己的服务器,解密用户信息 获取unionId等加密信息

              wx.request({

                  url: 'http://localhost/wanyouyinli/welcome.php',//自己的服务接口地址

                  method: 'GET',

                  data: { encryptedData: res.encryptedData, iv: res.iv, code: code },

                  success: function (res) {

                  // console.log(_this);

                  _this.globalData.openid = res.data.openId


                  if (_this.openIdCallback){            //这是我在网上看的价格回调函数,可是也没有用啊

                  _this.openIdCallback(res.data.openId);    //

          }                                                            //


      }

      })

      },

          fail: function () {

          console.log('获取用户信息失败')

          }

      })


      app.openIdCallback = res => {            

          openid:res                                        //index的onload中的回调

      }


      globalData: {

          userInfo: null,

          openid:null,

      }

      怎么弄才好呢,还请大神指导啊

      2019-04-17
      回复
    • 痛快科技
      痛快科技
      2019-04-17回复

      可以给一个代码片段看看

      2019-04-17
      回复
    • 2019-04-17

      //app.js

      App({

      onLaunch: function () {

      // 展示本地存储能力

      var logs = wx.getStorageSync('logs') || []

      logs.unshift(Date.now())

      wx.setStorageSync('logs', logs)


      var _this = this;

      // 登录

      wx.login({

      success: res => {

      // 发送 res.code 到后台换取 openId, sessionKey, unionId

      var code = res.code;//登录凭证

      if (code) {

      //2、调用获取用户信息接口

      wx.getUserInfo({

      success: function (res) {

      // console.log({ encryptedData: res.encryptedData, iv: res.iv, code: code })

      // 3.请求自己的服务器,解密用户信息 获取unionId等加密信息

      wx.request({

      url: 'http://localhost/wanyouyinli/welcome.php',//自己的服务接口地址

      method: 'GET',

      data: { encryptedData: res.encryptedData, iv: res.iv, code: code },

      success: function (res) {

      console.log(res.data)

      _this.globalData.openid = res.data.openId

      if (_this.openIdCallback){

      _this.openIdCallback(res.data.openId);

      }

      }

      })

      },

      fail: function () {

      console.log('获取用户信息失败')

      }

      })

      } else {

      console.log('获取用户登录态失败!' + res.errMsg)

      }

      }

      })

      // 获取用户信息

      wx.getSetting({

      success: res => {

      if (res.authSetting['scope.userInfo']) {

      // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框

      wx.getUserInfo({

      success: res => {

      // 可以将 res 发送给后台解码出 unionId

      this.globalData.userInfo = res.userInfo

      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回

      // 所以此处加入 callback 以防止这种情况

      if (this.userInfoReadyCallback) {

      this.userInfoReadyCallback(res)

      }

      }

      })

      }

      }

      })

      },

      globalData: {

      userInfo: null,

      openid:null

      }

      })


      //////////////////////////////////////////////////////这是index里的onload函数,

      onLoad: function () {

      ///////////////////////////////////////////////////////////

      app.openIdCallback = res => {

      openid:res                                    //z这是我自己写的,就三行,网上说的回调函数,我就是想用openid区数据库查找信息,然后渲染index页面

      }                                                    //我在这一页里定义了一个openid又,按理说不用我不想再存储一遍的,直接去gloabData里拿就好了,我                                                          //不知道咋办了

      //////////////////////////////////////////////////////


      if (app.globalData.userInfo) {

      this.setData({

      userInfo: app.globalData.userInfo,

      hasUserInfo: true,

      })

      } else if (this.data.canIUse){

      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回

      // 所以此处加入 callback 以防止这种情况

      app.userInfoReadyCallback = res => {

      this.setData({

      userInfo: res.userInfo,

      hasUserInfo: true

      })

      }

      } else {

      // 在没有 open-type=getUserInfo 版本的兼容处理

      wx.getUserInfo({

      success: res => {

      app.globalData.userInfo = res.userInfo

      this.setData({

      userInfo: res.userInfo,

      hasUserInfo: true

      })

      }

      })

      }

      },


      2019-04-17
      回复
    • 痛快科技
      痛快科技
      2019-04-18回复

      index页面执行是在app之后,在index中定义openIdCallback,肯定执行不了这个方法。

      可以在app.js中定义一个方法


      页面调用

      login(function(res){

          var openid = res.data.openId

      })


      getUserInfo接口说明,获取用户信息需要在button按钮中实现



      2019-04-18
      回复
登录 后发表内容