微信小程序在app.js中异步获取的openid怎么在index.js中调用

getUserInfo: function (cb) {
        var that = this
        if (this.globalData.userInfo) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //调用登录接口
            wx.login({
                success: function (res) {
                    var code = res.code
                    var appid = 'wx66e4ec7b580c2658'
                    var secret = 'd96fa2a84185d0eb2d782a38533fd850'
                    if (res.code) {
                        // 获取微信运动的数据
                        wx.getWeRunData({
                            success(res) {
                                var encryptedData = res.encryptedData
                                var iv = res.iv
                                // 获取用户信息
                                wx.getUserInfo({
                                    success: function (res) {
                                        that.globalData.userInfo = res.userInfo
                                        typeof cb == "function" && cb(that.globalData.userInfo)
                                        var data = {
                                            appid: appid,
                                            secret: secret,
                                            code: code,
                                            encryptedData: encryptedData,
                                            iv: iv,
                                            grant_type: 'authorization_code'
                                        }
                                        console.log(data)
                                        wx.request({
                                            //获取openid接口
                                            url: 'http://192.168.0.189/net_sindcorp_anniutingwenzhen/web/sports/identify',
                                            data: data,
                                            method: 'GET',
                                            success: function (res) {
                                                console.log(res)
                                                that.globalData.openid = res.data.openid
                                                // that.globalData.firstLogin = res.data.firstLogin
                                                that.globalData.stepInfoList = res.data.stepInfoList
                                                that.globalData.identification = res.data.identification
                                              
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }
                },
            })
        }
    },
    globalData: {
        userInfo: null,
        openid: null,
        stepInfoList: null,
        identification: null
    }

直接在index.js中
app.getUserInfo(function (userInfo) {})中写app.globalData.xxxx
是不一定能取到的 值没返回来 这个回调怎么写 或者怎么解决呢

阅读 14.6k
5 个回答

把app.js里面的getUserInfo写成一个Promise,
然后页面在index.js中,

getApp().getUserInfo().then(v => {
    //你需要做的事情......
})

可以在其他页面中通过

getApp().getUserInfo(function(userinfo){console.log(userinfo);})

这种回调函数来实现。

有大神能帮忙看下么

在app.js页面中获取后setData 存在globaldata中。我就是在app.js中获取屏幕尺寸然后供其他页面使用的

clipboard.png
在其他页面可直接使用:

var windowWidth = getApp().globalData.windowWidth //获取屏幕可使用宽度
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题