httpinterceptor.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (function (app) {
  2. app.factory("authHttpInterceptor", ["$q", "$injector", "storage", function ($q, $injector, storage) {
  3. var $http = null, $state = null,msg=null;
  4. var getHttp = function () {
  5. if (!$http) {
  6. $http = $injector.get('$http');
  7. }
  8. return $http;
  9. };
  10. var getState = function () {
  11. if (!$state) {
  12. $state = $injector.get('$state');
  13. }
  14. return $state;
  15. }
  16. var getMsg = function () {
  17. if (!msg) {
  18. msg = $injector.get('msg');
  19. }
  20. return msg;
  21. }
  22. return {
  23. 'request': function (config) {
  24. return config;
  25. },
  26. 'requestError': function (rejection) {
  27. return $q.reject(rejection);
  28. },
  29. 'response': function (response) {
  30. return response;
  31. },
  32. 'responseError': function (rejection) {
  33. // console.log(JSON.stringify(rejection));
  34. if (rejection.status == 401) {
  35. storage.remove('user');
  36. storage.remove('token');
  37. getHttp().defaults.headers.common["Authorization"] = undefined;
  38. getState().go('login');
  39. // alert("not login");
  40. // return $q.reject(rejection);
  41. return;
  42. }
  43. if (rejection.status==400) {
  44. getMsg().error(rejection.data.error_description);
  45. }
  46. return $q.reject(rejection);
  47. }
  48. }
  49. }]);
  50. })(angular.module('app.services'));