Commit 016bbc33677c58f58a98a50ff226a7a6fb0c0664
1 parent
90b17a3ef8
Exists in
master
interceptor
Showing
2 changed files
with
24 additions
and
19 deletions
Show diff stats
src/js/interceptor.js
| 1 | 1 | angular.module('focaConfiguracion') |
| 2 | - .factory("RequestHeadersInterceptor", [ '$injector', | |
| 2 | + .factory("interceptorRequest", [ '$injector', | |
| 3 | 3 | function($injector) { |
| 4 | 4 | |
| 5 | 5 | function reintentarRequest(httpConfig) { |
| 6 | 6 | |
| 7 | 7 | var $timeout = $injector.get('$timeout'); |
| 8 | 8 | |
| 9 | - $timeout(function() { | |
| 9 | + return $timeout(function() { | |
| 10 | 10 | |
| 11 | 11 | var $http = $injector.get('$http'); |
| 12 | 12 | return $http(httpConfig.config); |
| 13 | - }, 5000); | |
| 14 | - | |
| 15 | - return getDataSqlite(httpConfig); | |
| 13 | + }, 10000); | |
| 16 | 14 | } |
| 17 | 15 | |
| 18 | 16 | function updateDataSqlite(request) { |
| ... | ... | @@ -23,10 +21,11 @@ angular.module('focaConfiguracion') |
| 23 | 21 | request.config.url, |
| 24 | 22 | JSON.stringify(request.config.data), |
| 25 | 23 | request.config.method, |
| 26 | - JSON.stringify(request.data)) | |
| 24 | + JSON.stringify(request.data)); | |
| 27 | 25 | } |
| 28 | 26 | |
| 29 | 27 | function getDataSqlite(request) { |
| 28 | + | |
| 30 | 29 | return new Promise(function(resolve) { |
| 31 | 30 | |
| 32 | 31 | var sqliteService = $injector.get('focaSqliteService'); |
| ... | ... | @@ -37,19 +36,20 @@ angular.module('focaConfiguracion') |
| 37 | 36 | request.config.method, |
| 38 | 37 | respuesta); |
| 39 | 38 | |
| 40 | - function respuesta(config) { | |
| 39 | + function respuesta(config) { | |
| 40 | + | |
| 41 | + if (config) { | |
| 41 | 42 | |
| 42 | 43 | var request = { |
| 43 | - data: JSON.parse(config), | |
| 44 | + data: JSON.parse(config.response), | |
| 44 | 45 | status: 200 |
| 45 | 46 | }; |
| 46 | 47 | |
| 47 | - console.log(request); | |
| 48 | - | |
| 49 | - if (config) { | |
| 50 | - resolve(request); | |
| 51 | - } | |
| 48 | + resolve(request); | |
| 49 | + } else { | |
| 50 | + resolve(false); | |
| 52 | 51 | } |
| 52 | + } | |
| 53 | 53 | }); |
| 54 | 54 | } |
| 55 | 55 | |
| ... | ... | @@ -58,11 +58,14 @@ angular.module('focaConfiguracion') |
| 58 | 58 | |
| 59 | 59 | if (config.status === -1) { |
| 60 | 60 | |
| 61 | - var data = await reintentarRequest(config); | |
| 62 | - | |
| 63 | - config.data = data.data; | |
| 61 | + var data = await getDataSqlite(config); | |
| 64 | 62 | |
| 65 | - return config; | |
| 63 | + if (!data) { | |
| 64 | + return reintentarRequest(config); | |
| 65 | + } else { | |
| 66 | + config.data = data.data; | |
| 67 | + return config; | |
| 68 | + } | |
| 66 | 69 | |
| 67 | 70 | } else { |
| 68 | 71 | return config; |
| ... | ... | @@ -79,4 +82,6 @@ angular.module('focaConfiguracion') |
| 79 | 82 | |
| 80 | 83 | return request; |
| 81 | 84 | } |
| 82 | - ]); | |
| 85 | + ]).config(['$httpProvider', function($httpProvider) { | |
| 86 | + $httpProvider.interceptors.push('interceptorRequest'); | |
| 87 | + }]); |
src/js/service.js
| ... | ... | @@ -5,7 +5,7 @@ angular.module('appWrapperDemo') |
| 5 | 5 | 'ENDPOINT_BASE', |
| 6 | 6 | function($http, $localStorage, ENDPOINT_BASE) { |
| 7 | 7 | return { |
| 8 | - URL: 'http://debonline.dyndns.org:9900', | |
| 8 | + URL: 'http://10.231.45.189:9900', | |
| 9 | 9 | ENDPOINT_BASE: ENDPOINT_BASE, |
| 10 | 10 | setUrl: function(url) { |
| 11 | 11 | this.URL = url; |