小程序重新加载的时候偶尔会出现有的接口不执行,再刷新又正常的情况
发布于 6 年前 作者 haolei 10765 次浏览 来自 问答

这是正常的时候,onLogin是写在onLanuch里的,其他的都是index里的

有时候会这样子

请问这是为什么呢?@官方

5 回复

对,有时候刷新几次偶尔就不执行了,刚刚又出现了,然后我重新扫码登录了一下开发者工具又好了,这个原因其实还是没有找到,不明白为什么,

如果login的方法我写成这样的, 然后在onLanuch里执行that.login();正常来说应该是先执行这个呢,还是先执行首页的onLoad里的东西呢

login: function () {
    var that = this;
    wx.showLoading({
      title: '请稍等...',
    })
    return new Promise(function(resolve, reject){
      wx.login({
        success: function (res) {
          if (res.code) {
            // 获取新的token
            util.req('&m=member&a=onLogin', { code: res.code }, function (data) {
              if (data.flag == 1) {
                wx.removeStorageSync('token');
                that.setToken(data.reset.session3rd);
                that.setIsReg(data.reset.is_reg);
 
                if (that.userInfoReadyCallback) {
                  that.userInfoReadyCallback(data)
                }

 
                wx.hideLoading();
 
                resolve();
              }
            });
          } else {
            console.log('获取用户登录态失败!' + res.errMsg)
          }
        }
      })
    })
  },

你好,请具体描述问题出现的流程,并提供能复现问题的简单代码示例。

在手机上有出现过这个问题吗?

意思是说,wx.login 的 success 回调函数不会每次都执行吗?

app.js 里有一个wx.login

onLaunch: function (options) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          // 获取新的token
          util.req('&m=member&a=onLogin', { code: res.code }, function (data) {
            if (data.flag == 1) {
              wx.removeStorageSync('token');
              that.setToken(data.reset.session3rd);
              that.setIsReg(data.reset.is_reg);
 
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(data)
              }
 
              //是否有新消息
              util.req('&m=member&a=isUnread', {
                session3rd: data.reset.session3rd
              }, function (data) {
                if (data.flag == 1 && data.unread_num > 0) {
                  that.globalData.hasUnread = true;
                } else {
                  that.globalData.hasUnread = false;
                }
 
                if (that.isUnreadReadyCallback) {
                  that.isUnreadReadyCallback(that.globalData.hasUnread)
                }
              })
 
              wx.hideLoading();
            }
          });
        } else {
          console.log('获取用户登录态失败!' + res.errMsg)
        }
      }
    })
 
  },

小程序首页

请求banner和请求分类每次都会正常执行,但是login并不会每次都执行,偶尔会出现只执行首页里的banner和分类请求,不执行login的请求,而且之前还出现过先请求banner和分类再执行app.js中login方法的现象

onLoad: function (options) {
    var that = this;
 
 
    if (app.globalData.token) {
      fetchInfoList(that, 0, app.globalData.token);
    } else {
      app.userInfoReadyCallback = res => {
        fetchInfoList(that, 0, res.reset.session3rd);
      }
    }
 
     
    // 获取地理位置
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            that.setData({ address: res.result.address_component.district })
          },
          fail: function (res) {
          },
          complete: function (res) {
          }
        });
      }
    })
 
    // 请求banner
    util.getReq('&m=ad&a=getAdsList', { recommended: 1 }, function(data) {
      if (data.flag == 1) {
        that.setData({
          banners: data.result
        })
      } else {
        util.errorTips(data.msg)
      }
    })
 
    //请求分类
    util.getReq('&m=category&a=getCgList', { type: 0 }, function (data) {
      if (data.flag == 1) {
        that.setData({
          category: data.result
        })
      } else {
        util.errorTips(data.msg);
      }
    })
  },
 
  onShow: function() {
    var that = this;
    if (app.globalData.token) {
      util.req('&m=member&a=isUnread', {
        session3rd: app.globalData.token
      }, function (data) {
        if (data.flag == 1 && data.unread_num > 0) {
          app.globalData.hasUnread = true;
          that.setData({ hasUnread: true });
        } else {
          app.globalData.hasUnread = false;
          that.setData({ hasUnread: false });
        }
      })
    }
  },
回到顶部