鍍金池/ 問答/Linux  HTML/ 微信小程序在app.js中異步獲取的openid怎么在index.js中調(diào)用

微信小程序在app.js中異步獲取的openid怎么在index.js中調(diào)用

getUserInfo: function (cb) {
        var that = this
        if (this.globalData.userInfo) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //調(diào)用登錄接口
            wx.login({
                success: function (res) {
                    var code = res.code
                    var appid = 'wx66e4ec7b580c2658'
                    var secret = 'd96fa2a84185d0eb2d782a38533fd850'
                    if (res.code) {
                        // 獲取微信運(yùn)動(dòng)的數(shù)據(jù)
                        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
是不一定能取到的 值沒返回來 這個(gè)回調(diào)怎么寫 或者怎么解決呢

回答
編輯回答
淺淺
2018年8月14日 14:43
編輯回答
墨小白

可以在其他頁面中通過

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

這種回調(diào)函數(shù)來實(shí)現(xiàn)。

2018年4月12日 08:58
編輯回答
苦妄

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

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

var windowWidth = getApp().globalData.windowWidth //獲取屏幕可使用寬度
2018年7月21日 13:49
編輯回答
尐潴豬

把a(bǔ)pp.js里面的getUserInfo寫成一個(gè)Promise,
然后頁面在index.js中,

getApp().getUserInfo().then(v => {
    //你需要做的事情......
})
2018年5月8日 05:09
編輯回答
檸檬藍(lán)

有大神能幫忙看下么

2017年3月1日 23:46