小程序-在app.js中的onLaunch里的方法执行完成后再执行page页面中的onLoad

因为小程序授权获取不到头像昵称后,我这边就取消了授权的流程,开始仅执行静默授权了,
静默授权我是放在app.js中的onLaunch中的,经过运行代码,发现页面时不时的会先执行页面中的onload,导致页面接口总是报错,让我先登录
所以我这边就有了以下更新
app.js
 // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        let data={
          code:res.code,
          spread_spid:this.globalData.spread_spid?parseInt(this.globalData.spread_spid):'',
          spread_code:this.globalData.spread_code?parseInt(this.globalData.spread_code):''
        }
        console.log('静默授权提交的数据:',data)
        getSilence_auth(data).then(res=>{
          this.globalData.userIdentity=res.data.is_identity;
          this.globalData.userInfo=res.data.user;
          this.globalData.token=res.data.token;
          this.globalData.expires_time=res.data.expires_time;
          console.log('静默授权返回的数据:',res.data)
          console.log("用户的身份:",this.globalData.userIdentity)
          this.globalData.checkLogin = true;
        //由于这里是网络请求,可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.checkLoginReadyCallback){
           this.checkLoginReadyCallback(res);
        }
        })
      }
    })

  

page页面中的onload:
onLoad() {
    wx.showLoading({title:'加载中...'})
    let that = this;
    //判断onLaunch是否执行完毕
    if (app.globalData.checkLogin){
      this.getCommonHome()
      this.getAgreementsysaboutUs();
      this.getCommonJob();
      this.setData({
         activeIndexFooter:app.globalData.userIdentity,
         userInfo:app.globalData.userInfo
      })
    }else{
      app.checkLoginReadyCallback = res => {
        this.getCommonHome()
        this.getAgreementsysaboutUs();
        this.getCommonJob();
        this.setData({
          activeIndexFooter:app.globalData.userIdentity,
          userInfo:app.globalData.userInfo
         })
      };
    }
 
  },

  

posted @ 2022-11-11 10:40  MiniDuck  阅读(876)  评论(0编辑  收藏  举报