在redux中实现两个操作之间的回调

时间:2017-01-07 13:06:12

标签: javascript callback redux react-redux reducers

我有新的反应,我正在从角度改进我的网络应用程序以做出反应。我已经陷入了这个特殊的境地。我的应用程序需要令牌来执行API调用。当一个令牌在特定的API调用中到期时,我会查看失败案例并运行刷新令牌调用。在刷新令牌调用成功后,我重新运行该特定API。它很容易以角度实现,但我无法理解为反应还原。

这是我的角度代码

var fetchAccessToken = function(successCallback,failureCallback){
    if($cookies.get('aid')){
        accountGetATAPI.save({
            a_id    : $cookies.get('aid'),
            d_id    : window.fingerprint.md5hash,
            at      : getAt(),
            cl      : "W",
            rt      : getRefreshToken(),
            k_ey    : getSecret()
        },function (res) {
            var at = res.data.at;
            //console.log("server received token: " + at);
            $cookies.put('at',res.data.at); 
            successCallback(res);
        },function (res) {
            failureCallback(res);
        });
    }
};

var fetchProfiles = function(successCallback,failureCallback,currentTry,maxTry) {
    var aid = data["Account"]["_id"];
    accountProfileFetchAPI.save({
        a_id    : getAccountId(),
        tokn    : getAt()
    },function(res){
        data["Profiles"] = res["data"]["Profile"];
        //200
        successCallback(res);
    },function(res){
        //not 200
        if(res.status == error_constants.TOKEN_EXPIRE_CODE) {
            fetchAccessToken(function(){
                if(currentTry < maxTry) {
                    fetchProfiles(successCallback, failureCallback, ++currentTry, maxTry);
                } else {
                    console.log("maximum tries exceeded");
                }
            },function(res){
                if(res.status == (error_constants.TOKEN_EXPIRE_CODE || error_constants.TOKEN_ERROR_CODE)){
                    failureCallback(error_constants.LOGOUT_USER,res);
                }
            });
        } else {
            failureCallback(null,res);
        }
    });
};

我试图在反应代码中做类似的事情。直到现在我才到达这里。我知道代码是垃圾。但我还是搞清楚了!

export function fetchProfiles(currentTry,maxTry) {
return function(dispatch) {
axios.post("/profile/find",{
  a_id : cookies.load("aid"),
  tokn : cookies.load("at")
}).then((response) => {
    console.log(response);        
    dispatch({type: "FETCH_PROFILES_FULFILLED", payload: response.data.data})
  })
  .catch((err) => {
    console.log(err)
    if(err.response.status == 498){
       fetchAccessToken((res) =>{
         if(currentTry < maxTry) {
             console.log("at successcall ",currentTry,maxTry);
             fetchProfiles(++currentTry, maxTry);
         } else {
             console.log("maximum tries exceeded");
         }
         console.log(res);
       },(err) =>{
         console.log(err);
       });
      return;
    }
    dispatch({type: "FETCH_PROFILES_REJECTED", payload: err})
  })
  }
 }

export function fetchAccessToken(successCallback,failureCallback){
 axios.post("/account/g_at_w",{
   a_id  : cookies.load('aid'),
   d_id  : window.fingerprint.md5hash,
   at    : cookies.load('at'),
   cl    : "W",
   rt    : cookies.load('rt'),
   k_ey  : cookies.load('secret')
 }).then((response) => {
     console.log(response);
     successCallback(response);
     // dispatch({type: "FETCH_PROFILES_FULFILLED", payload:    response.data.data})
   })
   .catch((err) => {
     console.log(err.response);
     failureCallback(err);
     // dispatch({type: "FETCH_PROFILES_REJECTED", payload: err})
   })
 }

我使用redux thunk和axios来执行我的api调用。任何帮助都非常感谢。

0 个答案:

没有答案