2

我有一个实现刷新身份验证令牌的场景。在这种情况下,我为每种操作(Post、Get、Delete)实现了一个通用操作,并使用参数调用它们。

export function Get(param) {
return function (dispatch) {
    var query = param ? !!param.Query ? param.Query : "" : "";
    var funtionName = param ? param.FunctionName : "";
    var url = endPointUrl + funtionName;
    var offset = param.Offset ? param.Offset : "0";
    var limit = param.Limit ? param.Limit : "10";
    const config = { headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem('access-token') } };
    return new Promise((resolve, reject) => {
        axios.get(url, config).then((response) => {
            dispatch({ type: 'FETCH_SUCCEED_' + param.Caller, payload: response.data })
            resolve(response.data);
        }).catch((error) => {
                if (error.request.status == "401" && error.request.statusText == "RB.Exception.TokenExpiredException") {
                    refreshToken().then(() => {
                        dispatch(Get(param));
                    });
                }
            else {
                dispatch({ type: 'FETCH_ERROR_' + param.Caller, payload: error })
                reject(error);
            }

        })
    })
}

每当我收到 TokenExpiredException 时,我都会调用 refreshToken() 然后再次(递归地)调用当前操作,所以我不能在我的组件中使用“then”。你有任何关于使用“承诺”或“那么”的解决方案吗?操作完成后,我需要在组件中呈现一些消息。

4

2 回答 2

3

像这样的东西?

export function Get(param) {
    return function (dispatch) {
        var query = param ? !!param.Query ? param.Query : "" : "";
        var funtionName = param ? param.FunctionName : "";
        var url = endPointUrl + funtionName;
        var offset = param.Offset ? param.Offset : "0";
        var limit = param.Limit ? param.Limit : "10";
        const config = { headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem('access-token') } };
        return new Promise((resolve, reject) => {
            axios.get(url, config).then((response) => {
                dispatch({ type: 'FETCH_SUCCEED_' + param.Caller, payload: response.data })
                resolve(response.data);
            }).catch((error) => {
                if (error.request.status == "401" && error.request.statusText == "RB.Exception.TokenExpiredException") {
                    resolve(withRefresh(dispatch, param));
                }
                else {
                    dispatch({ type: 'FETCH_ERROR_' + param.Caller, payload: error })
                    reject(error);
                }

            })
        })
    }
}

function withRefresh(dispatch, param) {
    return refreshToken().then(() => {
        return dispatch(Get(param));
    });
}
于 2018-06-26T11:27:26.627 回答
0

您必须返回您的第二个承诺,即return dispatch(Get(param)); 请尝试以下代码

export function Get(param) {
return function (dispatch) {
    var query = param ? !!param.Query ? param.Query : "" : "";
    var funtionName = param ? param.FunctionName : "";
    var url = endPointUrl + funtionName;
    var offset = param.Offset ? param.Offset : "0";
    var limit = param.Limit ? param.Limit : "10";
    const config = { headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem('access-token') } };
    return new Promise((resolve, reject) => {
        axios.get(url, config).then((response) => {
            dispatch({ type: 'FETCH_SUCCEED_' + param.Caller, payload: response.data })
            resolve(response.data);
        }).catch((error) => {
                if (error.request.status == "401" && error.request.statusText == "RB.Exception.TokenExpiredException") {
                    refreshToken().then(() => {
                        return dispatch(Get(param));
                    });
                }
            else {
                dispatch({ type: 'FETCH_ERROR_' + param.Caller, payload: error })
                reject(error);
            }

        })
    })
}

于 2018-06-26T11:42:40.050 回答