微信小程序(踩过的坑)- (2)请求切换太快,同步冲突

(1)问题,tab之间切换太快,其中一个请求还未响应,另外一个请求已经发起

 

(2)解决

  wx.showLoading({
      title: '加载中',
      mask:true
    }) 

// get请求 遮层罩弹框有些请求不需要时,则showLoadFlag传false,不传则为默认为true

function getReq(env, url, header, data, cb, showLoadFlag) {
  var showLoadingFlag = true;
  if (showLoadFlag != undefined && showLoadFlag != null) {
    showLoadingFlag = showLoadFlag;
  }
  if (showLoadingFlag) {
    wx.showLoading({
      title: '加载中',
      mask:true
    })
  }
  wx.request({
    url: geturl(env) + url,
    method: 'get',
    header: header,
    data: data,
    success: function (res) {
      if(res.data.code == -1111){
        console.log('token已失效,刷新token...');
        // 身份验证失败,无效的token或userid不正确! 重新登录
        // 1.0 清除缓存
        wx.clearStorageSync()
        // 2.0 自动登录
        app.login().then(function (res) {
          //console.log(res);
          if (res.status == 200) {
            // 3.0 重新发起请求
            let map = objToStrMap(header)
            map.set("token", app.globalData.context.token);
            map.set("requestid", util.getuuid());
            header = strMapToObj(map);
            getReq(env, url, header, data, cb, showLoadFlag)
          } else{
            wx.showToast({
              title: '登录超时,请重新登录!',
              icon: 'none'
            })
          }  
        })
      }else{
        if (showLoadingFlag) {
          wx.hideLoading();
        }
        return typeof cb == "function" && cb(res.data)
      }
     
    },
    fail: function () {
      if (showLoadingFlag) {
        wx.hideLoading();
      }
      wx.showModal({
        title: '系统错误',
        content: '当前系统异常,请稍后再试!',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // wx.redirectTo({
            //   url: '/pages/errors/systemError/systemError',
            // })
          }
        }
      })
      
      return typeof cb == "function" && cb(false)
    }
  })
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值