AngularJs Http 请求拦截器

   Develop  Javascript  Angular    Angular  Javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Created by oy on 2016/11/29.
*/
(function() {
'use strict';

// 创建angular模块
angular
.module('app.oy_httpinterceptor', []);
})();

/**
* Created by oy on 11/1/16.
*/
(function() {
'use strict';

// 为模块添加工厂代理 代理中为请求拦截
angular
.module('app.oy_httpinterceptor')
.factory('oy_httpinterceptor_config',oy_httpinterceptor_factory)
;
oy_httpinterceptor_factory.$inject = [ '$rootScope', '$q', '$injector','$window'];
function oy_httpinterceptor_factory($rootScope, $q, $injector,$window) {
var httpInterceptor = {
'responseError': function (response) {
if(response.status == -1){
response.status = 200;
}
// console.log('responseError:'.response);
return $q.reject("网络连接错误!");
},
'response': function (response) {
// console.log('response:'.response);
// if(response.error == "Unauthenticated."){
// return
// }
return response;
},
'request': function (config) {
let url = config.url;

// disable cache for all API Not Get requests
if (url && config.method.toLowerCase() != 'GET'.toLowerCase() && url.indexOf('.html') < 0) {
config.params = config.params || {};
config.params.timestamp = new Date().getTime();
}

if(angular.isObject(config.data)) {
var param = [];
angular.forEach(config.data, function(value, key) {
this.push(key + '=' + value);
}, param);
config.data = param.join('&');
}

config.headers = config.headers || {};
config.headers['Content-Type'] = "application/x-www-form-urlencoded";

return config;
},
'requestError': function (config) {
// console.log('requestError:'.config);
return $q.reject("网络连接错误!");
}
}
return httpInterceptor;
}

})();

/**
* Created by oy on 11/1/16.
*/
(function() {
'use strict';

// 为模块配置代理函数
angular
.module('app.oy_httpinterceptor')
.config(oy_httpinterceptor_config)
;
oy_httpinterceptor_config.$inject = ['$httpProvider'];
function oy_httpinterceptor_config($httpProvider) {
$httpProvider.interceptors.push('oy_httpinterceptor_config');
}
})();
AngularJs 时间格式化
Git 本地库提交至远程服务器