目前我將登錄操作寫在了app.js中,獲取到微信的code后,向自己的服務器獲取驗證access-token.代碼如下app.jsonLaunch:function(){wx.login({success:res=>{//發送res.code到后臺換取openId,sessionKey,unionIdconfig.code=res.code;this.initUserInfo();}})},//初始化用戶信息initUserInfo:function(){wx.request({url:config.baseUrl+"/index.php?m=Api&c=User&a=initUserInfo",method:'POST',header:{"Content-Type":"application/x-www-form-urlencoded"},data:{source_type:'wxapp',code:config.code,},success:function(res){if(res.data.status==true){config.uid=res.data.data.uid;config.accessToken=res.data.data.wxapp_access_token;}elseif(res.data.status==false){console.log(res.data.data);}else{}}})},在另一個文件met.js中需要通過獲取的uidaccessToken來請求獲取數據.代碼如下met.jseggGetRemoteWords:function(){varself=this;wx.request({url:app.config.baseUrl+"/index.php?m=Api&c=Learning&a=getLearningWord",method:'POST',header:{"Content-Type":"application/x-www-form-urlencoded"},data:{uid:app.config.uid,access_token:app.config.accessToken},success:function(res){if(res.data.status==true){self.setData({'word.wordArray':res.data.data,})vartotal=self.data.word.wordArray.length;self.setData({"word.wordInfo.total":total});}elseif(res.data.status==false){}else{}}})},但是目前總是met.js中的函數eggGetRemoteWords總早于app.js中initUserInfo登錄函數執行,導致無法獲取到數據因為js異步的原因.請問大家這應該怎么處理呢?我了解到promise請問如果目前的需求將如何實現呢?
關于微信小程序中異步登錄的問題? 文件執行順序的問題 與promise問題
Helenr
2019-05-21 17:19:24