收藏
回答

怎么解决 bindGetUserInfo 重复执行问题

网络不好的时候,多次点击open-type getUserInfo 按钮,会导致该按钮事件执行多次。怎么解决呢

最后一次编辑于  2019-01-03
回答关注问题邀请回答
收藏

2 个回答

  • 小怪物.
    小怪物.
    2019-01-03

    bindGetUserInfo: function (e) {

    console.log(e)

    wx.showToast({

    title: '点击了button',

    icon: 'success',

    duration: 2000

    })

    console.log("isclick=====bindGetUserInfo=======11111", isclick)

    var that = this

    console.log("userInfo===========")

    console.log(that.data.userInfo)

    // var token = userInfo.token

    // var unionid = userInfo.user.unionid

    var encryptedData = e.detail.encryptedData;

    var iv = e.detail.iv;

    var laravelsession = publicUrl.globalData.laravelsession;

    var xsrftoken = publicUrl.globalData.xsrftoken;

    var sys_token = publicUrl.globalData.sys_token;

    if (e.detail.userInfo != undefined) {

    console.log("isclick=======2222=", isclick)

    if (!that.data.userInfo.user.nickname) {

    if(isclick==true){

    wx.request({

    url: url + '/miniprogram/registeruser',

    method: 'POST',

    data: {

    // unionid: unionid,

    // token: token,

    encryptedData: encryptedData,

    iv: iv,

    rawData: e.detail.rawData,

    signature: e.detail.signature

    //_token: sys_token

    },

    header: publicUrl.globalData.header,

    success: function (res) {

    console.log("注册用户成功==============")

    console.log(res)

    if (res.data.code == 0) {

    isclick = false

    console.log("code================0")

    var laravelsession = res.header["Set-Cookie"].match(/laravel_session=(.*?);/)[1]

    var xsrftoken = res.header["Set-Cookie"].match(/XSRF-TOKEN=(.*?);/)[1]

    var sys_token = res.header["xxvv"]

    publicUrl.globalData.laravelsession = laravelsession

    publicUrl.globalData.xsrftoken = xsrftoken

    publicUrl.globalData.sys_token = sys_token

    var header = {

    'Content-type': 'application/x-www-form-urlencoded',

    'Cookie': 'XSRF-TOKEN=' + xsrftoken + '; ' + 'laravel_session=' + laravelsession,

    'X-CSRF-TOKEN': sys_token,

    'X-Requested-With': 'XMLHttpRequest',

    'uf': '1'

    }

    publicUrl.globalData.header = header

    that.setData({

    userInfo: res.data.result

    })

    // userInfo = res.data.result

    wx.setStorage({

    key: "myuser",

    data: res.data.result

    })

    publicUrl.globalData.userInfo = res.data.result

    wx.setStorage({

    key: "userInfo",

    data: res.data.result

    })

    if (that.data.sid) {

    if (that.data.share == 1) {

    wx.reLaunch({

    url: '/pages/ranking/ranking?storeid=' + that.data.sid + '&share=1'

    })

    } else {

    wx.reLaunch({

    url: '/pages/ranking/ranking?storeid=' + that.data.sid

    })

    }

    } else if (that.data.uunionid) {

    if (that.data.share == 1) {

    wx.reLaunch({

    url: '/pages/personal/personal?uunionid=' + that.data.uunionid + '&share=1'

    })

    } else {

    wx.reLaunch({

    url: '/pages/personal/personal?uunionid=' + that.data.uunionid

    })

    }

    } else {

    wx.reLaunch({

    url: '/pages/city/city?source=login',

    })

    }

    } else if (res.data.code == 5588) {

    login(that)

    } else {

    console.log("code==============else=========", res.data.code)

    }

    },

    fail: function (res) {

    console.log("注册用户失败===============")

    }

    })

    }

    } else {

    var userInfo = publicUrl.globalData.userInfo

    var myuser = wx.getStorageSync('myuser');

    if (that.data.sid) {

    if (that.data.share == 1) {

    wx.reLaunch({

    url: '/pages/ranking/ranking?storeid=' + that.data.sid + '&share=1'

    })

    } else {

    wx.reLaunch({

    url: '/pages/ranking/ranking?storeid=' + that.data.sid

    })

    }

    } else if (that.data.uunionid) {

    if (that.data.share == 1) {

    wx.reLaunch({

    url: '/pages/personal/personal?uunionid=' + that.data.uunionid + '&share=1'

    })

    } else {

    wx.reLaunch({

    url: '/pages/personal/personal?uunionid=' + that.data.uunionid

    })

    }

    } else {

    wx.reLaunch({

    url: '/pages/city/city?source=login',

    })

    }

    }

    } else {

    console.log("您没有授权哦")

    wx.showToast({

    title: '您没有授权哦~',

    icon: 'none',

    duration: 2000

    })

    }

    },


    2019-01-03
    有用
    回复
  • MCC
    MCC
    2019-01-03

    你自己写个事件节流。在getUserInfo绑定的事件里面。

    根据取出的东西。如果取出来一遍之后。即使在点也不走接口

    2019-01-03
    有用
    回复 5
    • 小怪物.
      小怪物.
      2019-01-03

      不太懂,,,,能说的详细点吗

      2019-01-03
      回复
    • 小怪物.
      小怪物.
      2019-01-03回复小怪物.

      bindGetUserInfo: function (e) {

      console.log(e)

      wx.showToast({

      title: '点击了button',

      icon: 'success',

      duration: 2000

      })

      console.log("isclick=====bindGetUserInfo=======11111", isclick)

      var that = this

      console.log("userInfo===========")

      console.log(that.data.userInfo)

      // var token = userInfo.token

      // var unionid = userInfo.user.unionid

      var encryptedData = e.detail.encryptedData;

      var iv = e.detail.iv;

      var laravelsession = publicUrl.globalData.laravelsession;

      var xsrftoken = publicUrl.globalData.xsrftoken;

      var sys_token = publicUrl.globalData.sys_token;

      if (e.detail.userInfo != undefined) {

      console.log("isclick=======2222=", isclick)

      if (!that.data.userInfo.user.nickname) {

      if(isclick==true){

      wx.request({

      url: url + '/miniprogram/registeruser',

      method: 'POST',

      data: {

      // unionid: unionid,

      // token: token,

      encryptedData: encryptedData,

      iv: iv,

      rawData: e.detail.rawData,

      signature: e.detail.signature

      //_token: sys_token

      },

      header: publicUrl.globalData.header,

      success: function (res) {

      console.log("注册用户成功==============")

      console.log(res)

      if (res.data.code == 0) {

      isclick = false

      console.log("code================0")

      var laravelsession = res.header["Set-Cookie"].match(/laravel_session=(.*?);/)[1]

      var xsrftoken = res.header["Set-Cookie"].match(/XSRF-TOKEN=(.*?);/)[1]

      var sys_token = res.header["xxvv"]

      publicUrl.globalData.laravelsession = laravelsession

      publicUrl.globalData.xsrftoken = xsrftoken

      publicUrl.globalData.sys_token = sys_token

      var header = {

      'Content-type': 'application/x-www-form-urlencoded',

      'Cookie': 'XSRF-TOKEN=' + xsrftoken + '; ' + 'laravel_session=' + laravelsession,

      'X-CSRF-TOKEN': sys_token,

      'X-Requested-With': 'XMLHttpRequest',

      'uf': '1'

      }

      publicUrl.globalData.header = header

      that.setData({

      userInfo: res.data.result

      })

      // userInfo = res.data.result

      wx.setStorage({

      key: "myuser",

      data: res.data.result

      })

      publicUrl.globalData.userInfo = res.data.result

      wx.setStorage({

      key: "userInfo",

      data: res.data.result

      })

      if (that.data.sid) {

      if (that.data.share == 1) {

      wx.reLaunch({

      url: '/pages/ranking/ranking?storeid=' + that.data.sid + '&share=1'

      })

      } else {

      wx.reLaunch({

      url: '/pages/ranking/ranking?storeid=' + that.data.sid

      })

      }

      } else if (that.data.uunionid) {

      if (that.data.share == 1) {

      wx.reLaunch({

      url: '/pages/personal/personal?uunionid=' + that.data.uunionid + '&share=1'

      })

      } else {

      wx.reLaunch({

      url: '/pages/personal/personal?uunionid=' + that.data.uunionid

      })

      }

      } else {

      wx.reLaunch({

      url: '/pages/city/city?source=login',

      })

      }

      } else if (res.data.code == 5588) {

      login(that)

      } else {

      console.log("code==============else=========", res.data.code)

      }

      },

      fail: function (res) {

      console.log("注册用户失败===============")

      }

      })

      }

      } else {

      var userInfo = publicUrl.globalData.userInfo

      var myuser = wx.getStorageSync('myuser');

      if (that.data.sid) {

      if (that.data.share == 1) {

      wx.reLaunch({

      url: '/pages/ranking/ranking?storeid=' + that.data.sid + '&share=1'

      })

      } else {

      wx.reLaunch({

      url: '/pages/ranking/ranking?storeid=' + that.data.sid

      })

      }

      } else if (that.data.uunionid) {

      if (that.data.share == 1) {

      wx.reLaunch({

      url: '/pages/personal/personal?uunionid=' + that.data.uunionid + '&share=1'

      })

      } else {

      wx.reLaunch({

      url: '/pages/personal/personal?uunionid=' + that.data.uunionid

      })

      }

      } else {

      wx.reLaunch({

      url: '/pages/city/city?source=login',

      })

      }

      }

      } else {

      console.log("您没有授权哦")

      wx.showToast({

      title: '您没有授权哦~',

      icon: 'none',

      duration: 2000

      })

      }

      },


      2019-01-03
      回复
    • MCC
      MCC
      2019-01-03回复小怪物.


      这个样子

      2019-01-03
      1
      回复
    • 小怪物.
      小怪物.
      2019-01-03回复MCC

      感谢

      2019-01-03
      回复
    • MCC
      MCC
      2019-01-03回复小怪物.

      不客气,给个赞同就好了


      2019-01-03
      1
      回复
登录 后发表内容