baimeng 9 yıl önce
ebeveyn
işleme
0a414f516c
2 değiştirilmiş dosya ile 50 ekleme ve 2 silme
  1. 25 1
      wl/platforms/android/assets/www/js/app.js
  2. 25 1
      wl/www/js/app.js

+ 25 - 1
wl/platforms/android/assets/www/js/app.js

xqd
@@ -8,14 +8,38 @@
        $ionicConfigProvider.backButton.text('后退').icon('ion-chevron-left');
        $ionicConfigProvider.form.checkbox("circle");
        $ionicConfigProvider.templates.maxPrefetch(0);
+   }]).factory('myHttpInterceptor', ["$q", 'data', function ($q, data) {  //$http拦截器定义
+       var initInjector = angular.injector(['ng']);
+       var $http = initInjector.get('$http');
+       return {
+           'request': function (config) {
+               return config;
+           },
+           'requestError': function (rejection) {
+               return $q.reject(rejection);
+           },
+           'response': function (response) {
+                   return response;
+           },
+           'responseError': function (rejection) {
+               if (rejection.status == 401) {
+                   data.remove('user');
+                   data.remove('token');
+                   $http.defaults.headers.common["Authorization"] = undefined;
+                   window.location.href = '#/account/login';
+                   return $q.reject('');
+               }
+               return $q.reject(rejection);
+           }
+       }
    }])
  .config(["$httpProvider", "$windowProvider", function ($httpProvider, $windowProvider) {
      var win = $windowProvider.$get();
      var token=win.localStorage['token'];
-     ////http配置
      if (token) {
          $httpProvider.defaults.headers.common['Authorization'] = 'Bearer ' + token;
      }
+     $httpProvider.interceptors.push('myHttpInterceptor');
  }])
 .run(["$ionicPlatform", "$location", "msg", "$rootScope", "$timeout", "$ionicHistory", "userService", "$state", "$http",
     function ($ionicPlatform, $location, msg, $rootScope, $timeout, $ionicHistory, userService, $state, $http) {

+ 25 - 1
wl/www/js/app.js

xqd
@@ -8,14 +8,38 @@
        $ionicConfigProvider.backButton.text('后退').icon('ion-chevron-left');
        $ionicConfigProvider.form.checkbox("circle");
        $ionicConfigProvider.templates.maxPrefetch(0);
+   }]).factory('myHttpInterceptor', ["$q", 'data', function ($q, data) {  //$http拦截器定义
+       var initInjector = angular.injector(['ng']);
+       var $http = initInjector.get('$http');
+       return {
+           'request': function (config) {
+               return config;
+           },
+           'requestError': function (rejection) {
+               return $q.reject(rejection);
+           },
+           'response': function (response) {
+                   return response;
+           },
+           'responseError': function (rejection) {
+               if (rejection.status == 401) {
+                   data.remove('user');
+                   data.remove('token');
+                   $http.defaults.headers.common["Authorization"] = undefined;
+                   window.location.href = '#/account/login';
+                   return $q.reject('');
+               }
+               return $q.reject(rejection);
+           }
+       }
    }])
  .config(["$httpProvider", "$windowProvider", function ($httpProvider, $windowProvider) {
      var win = $windowProvider.$get();
      var token=win.localStorage['token'];
-     ////http配置
      if (token) {
          $httpProvider.defaults.headers.common['Authorization'] = 'Bearer ' + token;
      }
+     $httpProvider.interceptors.push('myHttpInterceptor');
  }])
 .run(["$ionicPlatform", "$location", "msg", "$rootScope", "$timeout", "$ionicHistory", "userService", "$state", "$http",
     function ($ionicPlatform, $location, msg, $rootScope, $timeout, $ionicHistory, userService, $state, $http) {