httpinterceptor.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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||rejection.data.message=="Unauthenticated.") {
  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 ;
  41. }
  42. if (rejection.status == 400) {
  43. getMsg().error(rejection.data.message);
  44. }
  45. if (rejection.status==500) {
  46. getMsg().error(rejection.data.message);
  47. }
  48. return $q.reject(rejection);
  49. }
  50. }
  51. }]);
  52. })(angular.module('app.services'));