怎么给 http interceptor 传递初始化参数

2020-08-23 社会 53阅读
找到一个workaround,把checker放在每个$http request的config里,然后在interceptor中检查。
var interceptor = ['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {
function retryAterLogin(response) {
var deferred = $q.defer();
httpBuffer.append(response.config, deferred);
$rootScope.$broadcast('event:auth-loginRequired');
return deferred.promise;
}
function success(response) {
var checker = response.config.authRequiredChecker || function(response) { return false; };
if (checker(response)) {
return retryAterLogin(response);
}
// otherwise, default behaviour
return response;
}
function error(response) {
function defaultChecker(response) {
return response.status === 401 && !response.config.ignoreAuthModule;
};
var checker = response.config.authRequiredChecker || defaultChecker;
if (checker(response)) {
return retryAterLogin(response);
}
// otherwise, default behaviour
return $q.reject(response);
}
return function(promise) {
return promise.then(success, error);
};
但这样实在不优雅。所以还要继续讨教。
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com