是否从 Angular 拦截器发出 $http 请求?

Do $http request from Angular Interceptor?

我正在尝试实施 Angular 异常拦截器。至少对于一个。我有一个令牌,当它旧的 enogh 后端抛出 TokenAlmostExpired exception。此异常包含 errorCode = 101。在拦截器中,我正在检查代码是否为 101,然后我需要将 POST request 发送到后端的 /refresh endpoint 以便我可以刷新令牌。

.factory('errorInjector',['$injector', function ($q, $injector) {

    var vm = this;

    var errorInjector = {
        'response': function (response) {
            console.log(response);
            return response;
        },
        'responseError': function (rejection) {
            if (JSON.stringify(rejection.data.errorCode) === JSON.stringify(101)) {
                vm.getRefreshToken();
            }
            return $q.reject(rejection);
        }
    };
    return errorInjector;
}]);

.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.interceptors.push('errorInjector');
    }]);

$http

但是在拦截器级别有一个问题,我不能只依赖于 $http,因为有 Circular dependency found: $http <- errorInjector <- $http <- $templateFactory <- $view <- $state

$scope

而且我不能将 getRefreshToken() 函数放到 $scope,因为 $scope 依赖项还提供了 'Circular dependency'.

$喷油器

var http = $injector.get('$http');

效果不佳,给我错误。

那么我怎样才能在拦截器中捕获异常,然后从那里发出 $http 请求?

.factory('errorInjector',['$injector', function ($q, $injector) { .... }]);

更改为:

.factory('errorInjector',['$q', function ($q) { .... }]);

拦截器

(function (angular) {
    'use strict';

    angular.module('services').factory("httpInterceptor", [
        'errorLauncher',
        '$q',
        function (errorLauncher, $q) {
            return {
                'requestError': function (rejection) {
                    if (rejection.status === 101) {
                        errorLauncher.pushInErrorMessage(rejection);
                    }
                    return $q.reject(rejection);
                },
                'responseError': function (rejection) {
                    if (rejection.status === 101) {
                        errorLauncher.pushInErrorMessage(rejection);
                    }
                    return $q.reject(rejection);
                }
            };
        }]);
})(angular);

和错误处理程序服务

(function (angular) {
    'use strict';

    angular.module('services').factory("errorLauncher", [
        '$rootScope',
        function ($rootScope) {
            return {
                'pushInErrorMessage': function (rejection) {
                    $rootScope.$emit('theTokenWillDieSoon');
                }
            };
        }]);
})(angular);

现在是主应用程序控制器

(function (angular) {
    'use strict';

    angular.module('controllers').controller("globalCtrl", [
        '$rootScope',
        '$http',
        function ($rootScope, $http) {
            $rootScope.$on('theTokenWillDieSoon', function () {
                // http from here
            });
        }]);
})(angular);

所以我已经通过服务完成了。谢谢大家!

拦截器:

.factory('errorInjector',['$injector', function ($q, $injector) {

    var errorInjector = {
        'response': function (response) {
            ....
        },
        'responseError': function (rejection) {
            if (JSON.stringify(rejection.data.errorCode) === JSON.stringify(101)) {
                var refreshTokenService = $q.get('refreshTokenService');
                refreshTokenService.refreshTokenService();
                $.notify({message: data.data.detailMessage}, {type: 'warning'});
            }
            return $q.reject(rejection);
        }
    };

    return errorInjector;
}]);

refreshTokenService:

.service('refreshTokenService', ['$http', function ($http) {

    this.refreshTokenService = function () {
        $http.post('/refresh').then(
            function success(response) {
               .....
            },
            function error(data) {
                .....
            }
        );
    };

}]) ;