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

因为小程序授权获取不到头像昵称后,我这边就取消了授权的流程,开始仅执行静默授权了,
静默授权我是放在app.js中的onLaunch中的,经过运行代码,发现页面时不时的会先执行页面中的onload,导致页面接口总是报错,让我先登录
所以我这边就有了以下更新
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 登录
   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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 @   MiniDuck  阅读(1058)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示