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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/// <reference path="../../app.js" />
// apiInterceptor is responsible to handle the aspect of each request and response.
webservices.factory('apiInterceptor', ['$q', 'loginContext', '$log', '$window', '$injector','$cookies',
function ($q, loginContext, $log, $window, $injector,$cookies) {
'use strict';
$log.debug('apiInterceptor.ctor()...');
var apiToken = loginContext.apiToken;
var tokenType = loginContext.tokenType;
var webApiHostUrl = loginContext.apiHost + constant.webapi.prefix;
//for vat api
var vatWebApiHostUrl = loginContext.vatApiHost + constant.webapi.prefix;
var redirectToLogOut = loginContext.redirectToLogOutFunction;
return {
//token save to services for further usage
tokenType: tokenType,
apiToken: function () {
return apiToken;
},
webApiHostUrl: webApiHostUrl,
//for vat api
vatWebApiHostUrl: vatWebApiHostUrl,
// On request success
request: function (config) {
// Check if this request is a web api call, we should only update url and header for each web api request.
// Because angular will use the $http service as well internally, such as ng-include to request a html file from server.
// Set this config to send request to a different web server will break the functions of angularJS.
if (config.isWebApiRequest) {
config.headers = config.headers || {};
config.headers.Authorization = tokenType + ' ' + apiToken;
if (config.apiType == 'vat') {
config.url = vatWebApiHostUrl + config.url;
}
else {
config.url = webApiHostUrl + config.url;
}
config.withCredentials = true;
//废弃 token有效期会自动判断
// before each api call, try to ensure user account has not been expired
// otherwise, force user to login again
// var $http = $injector.get('$http');
// $http.get(loginContext.serverUrl + '/Account/CheckLoginStatus?_=' + (new Date).valueOf(), { isBusyRequest: false }).success(function (data) {
// if (data === false) {
// redirectToLogOut();
// }
// });
}
return config;
},
// On request failure
requestError: function (rejection) {
$log.error(rejection); // Contains the data about the error on the request.
// Return the promise rejection.
return $q.reject(rejection);
},
// On response success
response: function (response) {
if (response.status === 401) {
redirectToLogOut();
}
var tmpToken = response.headers('refreshToken');
if (!!tmpToken) {
apiToken = tmpToken;
$log.info('refresh token: ' + apiToken);
}
return response || $q.when(response);
},
// On response failture
responseError: function (rejection) {
$log.error(rejection); // Contains the data about the error.
if (rejection.status === 401) {
redirectToLogOut();
}
// Return the promise rejection.
return $q.reject(rejection);
}
};
}])
.factory('requestNotificationInterceptor', ['$q', '$injector', '$log', function ($q, $injector, $log) {
'use strict';
$log.debug('busyRequestNotificationInterceptor.ctor()...');
var busyRequestNotificationHub;
var busyRequestUrl = null;
var requestOntheWay = 0;
function onRequestStarted(config) {
if (config.isWebApiRequest && config.isBusyRequest) {
//$log.debug('busyRequestNotificationInterceptor: Start busy request ' + config.url);
//busyRequestNotificationHub = busyRequestNotificationHub || $injector.get('busyRequestNotificationHub');
//busyRequestNotificationHub.requestStarted();
//busyRequestUrl = config.url;
}
if (config && typeof (config.ignoreLoadingBar) !== 'undefined' && !config.ignoreLoadingBar) {
requestOntheWay++;
}
if (requestOntheWay > 0) {
$('#busy-indicator-container').show();
}
}
function onRequestEnded(config) {
if (busyRequestUrl && config.url === busyRequestUrl) {
//$log.debug('busyRequestNotificationInterceptor: Finish busy request ' + config.url);
//busyRequestNotificationHub = busyRequestNotificationHub || $injector.get('busyRequestNotificationHub');
//busyRequestNotificationHub.requestEnded();
//busyRequestUrl = null;
}
if (config && typeof (config.ignoreLoadingBar) !== 'undefined' && !config.ignoreLoadingBar) {
requestOntheWay--;
}
if (requestOntheWay <= 0) {
$('#busy-indicator-container').hide();
}
}
return {
// On request success
request: function (config) {
onRequestStarted(config);
return config;
},
// On request failure
requestError: function (rejection) {
onRequestEnded(rejection.config);
return $q.reject(rejection);
},
// On response success
response: function (response) {
onRequestEnded(response.config);
return response || $q.when(response);
},
// On response failture
responseError: function (rejection) {
onRequestEnded(rejection.config);
return $q.reject(rejection);
}
}
}]);
webservices.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('apiInterceptor');
$httpProvider.interceptors.push('requestNotificationInterceptor');
}]);
// All web api request should use this service to create its config for request.
webservices.factory('apiConfig', ['$log', 'vatSessionService',
function ($log, vatSessionService) {
$log.debug('apiConfig.ctor()...');
return {
create: function (config) {
var cfg = {
apiType: 'admin',
ignoreLoadingBar: false
};
if (config) {
cfg = _.extend(cfg, config);
}
cfg.isWebApiRequest = true;
if (config && config.dbName) {
cfg.headers = { 'from': config.dbName + '@cn.pwc.com' };
}
else {
if (vatSessionService.project && vatSessionService.project.dbName) {
cfg.headers = { 'from': vatSessionService.project.dbName + '@cn.pwc.com' };
}
}
return cfg;
},
createVat: function (config) {
var cfg = {
apiType: 'vat',
ignoreLoadingBar: false
};
if (config) {
cfg = _.extend(cfg, config);
}
cfg.isWebApiRequest = true;
if (config && config.dbName) {
cfg.headers = { 'from': config.dbName + '@cn.pwc.com' };
}
else {
cfg.headers = { 'from': vatSessionService.project.dbName + '@cn.pwc.com' };
}
//$httpProvider.defaults.headers.common['from'] = vatSessionService.project.dbName + '@cn.pwc.com';
//cfg.headers.from = vatSessionService.project.dbName+'@cn.pwc.com';
return cfg;
}
};
}]);