直接上代码:
angular.module('webb-ui').factory('authInterceptor', ['$q','$rootScope','$location','$injector','$timeout',function ($q,$scope,$location,$injector,$timeout) { function isNotPRiv(url){ return url.indexOf("/priv/") === -1 } var authInterceptor = { 'request': function(config) { var deferred = $q.defer(); var url = config.url; deferred.resolve(config); return deferred.promise; }, 'response':function(response){ return response; }, 'responseError': function(rejection) { var rootScope = $injector.get('$rootScope'); if(isNotPriv(rejection.config.url)){ var status = rejection.status; if(status === 403){ $scope.four_zero_th = true;//自己的业务逻辑处理代码 rootScope.selectEtl(); return rejection; }else if(status === 401) { $scope.four_zero_one = true; rootScope.selectEtl(); return rejection; } } return $q.reject(rejection); } }; return authInterceptor;}]);说明:responseError 此例子主要是对请求异常进行统一的拦截处理。angular.module('webb-ui').config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider', function ($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) { $httpProvider.interceptors.push('authInterceptor'); /* *这里删除一个$injector,解决不能获取code码的问题。 * */ //$httpProvider.interceptors.pop();}]);
新闻热点
疑难解答