当后台使用c#的abp框架时,而前端不使用abp带的angular模版,比如ionic项目,项目接口还是使用swagger生成,这是我们还是需要在请求头上带有token、tenantId信息、包括一些错误处理,可以仿造abp自带模版自己写个http拦截器。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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318import {Observable, of, Subject} from 'rxjs';
import {MessageService} from './message.service';
import {HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpHeaders} from '@angular/common/http';
import {Injectable, Injector} from '@angular/core';
export interface IValidationErrorInfo {
message: string;
members: string[];
}
export interface IErrorInfo {
code: number;
message: string;
details: string;
validationErrors: IValidationErrorInfo[];
}
export interface IAjaxResponse {
success: boolean;
result?: any;
targetUrl?: string;
error?: any;
unAuthorizedRequest: boolean;
__abp: boolean;
}
@Injectable()
export class AbpHttpConfiguration {
constructor(private _messageService: MessageService) {
}
defaultError = {
message: '产生了一个错误!',
details: '服务器没有发送错误详情.'
};
defaultError401 = {
message: '您未经过身份验证!',
details: '为了执行此操作,您应该进行身份验证(登录).'
};
defaultError403 = {
message: '您没有权限!',
details: '您不能执行此操作.'
};
defaultError404 = {
message: '未找到相关资源!',
details: '服务器上找不到请求的资源.'
};
logError(error): void {
console.log(error);
}
showError(error): any {
if (error.details) {
return this._messageService.error(error.details, error.message || this.defaultError.message);
} else {
return this._messageService.error(error.message || this.defaultError.message);
}
}
handleTargetUrl(targetUrl: string): void {
if (!targetUrl) {
location.href = '/';
} else {
location.href = targetUrl;
}
}
handleUnAuthorizedRequest(messagePromise: any, targetUrl?: string): void {
const _this = this;
const self = this;
if (messagePromise) {
messagePromise.done(function () {
_this.handleTargetUrl(targetUrl || '/');
});
} else {
self.handleTargetUrl(targetUrl || '/');
}
}
handleNonAbpErrorResponse(response: HttpResponse<any>): void {
const self = this;
switch (response.status) {
case 401:
self.handleUnAuthorizedRequest(self.showError(self.defaultError401), '/');
break;
case 403:
self.showError(self.defaultError403);
break;
case 404:
self.showError(self.defaultError404);
break;
default:
self.showError(self.defaultError);
break;
}
}
handleAbpResponse(response: HttpResponse<any>, ajaxResponse: IAjaxResponse): HttpResponse<any> {
let newResponse;
if (ajaxResponse.success) {
newResponse = response.clone({
body: ajaxResponse.result
});
if (ajaxResponse.targetUrl) {
this.handleTargetUrl(ajaxResponse.targetUrl);
}
} else {
newResponse = response.clone({
body: ajaxResponse.result
});
if (!ajaxResponse.error) {
ajaxResponse.error = this.defaultError;
}
this.logError(ajaxResponse.error);
this.showError(ajaxResponse.error);
if (response.status === 401) {
this.handleUnAuthorizedRequest(null, ajaxResponse.targetUrl);
}
}
return newResponse;
}
getAbpAjaxResponseOrNull(response: HttpResponse<any>): IAjaxResponse | null {
if (!response || !response.headers) {
return null;
}
const contentType = response.headers.get('Content-Type');
if (!contentType) {
// this._logService.warn('Content-Type is not sent!');
console.warn('Content-Type is not sent!');
return null;
}
if (contentType.indexOf('application/json') < 0) {
// this._logService.warn('Content-Type is not application/json: ' + contentType);
console.log('Content-Type is not application/json: ' + contentType);
return null;
}
const responseObj = JSON.parse(JSON.stringify(response.body));
if (!responseObj.__abp) {
return null;
}
return responseObj;
}
handleResponse(response: HttpResponse<any>): HttpResponse<any> {
const ajaxResponse = this.getAbpAjaxResponseOrNull(response);
if (ajaxResponse == null) {
return response;
}
return this.handleAbpResponse(response, ajaxResponse);
}
blobToText(blob: any): Observable<string> {
return new Observable(function (observer) {
if (!blob) {
observer.next('');
observer.complete();
} else {
const reader = new FileReader();
reader.onload = function () {
observer.next(this.result);
observer.complete();
};
reader.readAsText(blob);
}
});
}
}
@Injectable()
export class AbpHttpInterceptor implements HttpInterceptor {
protected configuration: AbpHttpConfiguration;
// private _tokenService;
// private _utilsService;
constructor(private injector: Injector) {
this.configuration = this.injector.get(AbpHttpConfiguration); // 使用inject注入service
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const _this = this;
const interceptObservable: Subject<HttpEvent<any>> = new Subject();
const modifiedRequest = this.normalizeRequestHeaders(request);
next.handle(modifiedRequest)
.subscribe(function (event) {
_this.handleSuccessResponse(event, interceptObservable);
}, function (error) {
return _this.handleErrorResponse(error, interceptObservable);
});
return interceptObservable;
}
protected normalizeRequestHeaders(request: HttpRequest<any>): HttpRequest<any> {
let modifiedHeaders = new HttpHeaders();
modifiedHeaders = request.headers.set('Pragma', 'no-cache')
.set('Cache-Control', 'no-cache')
.set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT');
modifiedHeaders = this.addXRequestedWithHeader(modifiedHeaders);
modifiedHeaders = this.addAuthorizationHeaders(modifiedHeaders);
modifiedHeaders = this.addAspNetCoreCultureHeader(modifiedHeaders);
modifiedHeaders = this.addAcceptLanguageHeader(modifiedHeaders);
modifiedHeaders = this.addTenantIdHeader(modifiedHeaders);
return request.clone({
headers: modifiedHeaders
});
}
protected addXRequestedWithHeader(headers: HttpHeaders): HttpHeaders {
if (headers) {
headers = headers.set('X-Requested-With', 'XMLHttpRequest');
}
return headers;
}
protected addAspNetCoreCultureHeader(headers: HttpHeaders): HttpHeaders {
// todo: 如果需要再添加.AspNetCore.Culture
/*const cookieLangValue = this._utilsService.getCookieValue('Abp.Localization.CultureName');
if (cookieLangValue && headers && !headers.has('.AspNetCore.Culture')) {
headers = headers.set('.AspNetCore.Culture', cookieLangValue);
}*/
return headers;
}
protected addAcceptLanguageHeader(headers: HttpHeaders): HttpHeaders {
// todo: 如果需要再添加Accept-Language
/*const cookieLangValue = this._utilsService.getCookieValue('Abp.Localization.CultureName');
if (cookieLangValue && headers && !headers.has('Accept-Language')) {
headers = headers.set('Accept-Language', cookieLangValue);
}*/
return headers;
}
protected addTenantIdHeader(headers: HttpHeaders): HttpHeaders {
// todo: 如果需要再添加Abp.TenantId
// const cookieTenantIdValue = this._utilsService.getCookieValue('Abp.TenantId');
// if (cookieTenantIdValue && headers && !headers.has('Abp.TenantId')) {
// headers = headers.set('Abp.TenantId', cookieTenantIdValue);
// }
return headers;
}
protected addAuthorizationHeaders(headers: HttpHeaders): HttpHeaders {
let authorizationHeaders = headers ? headers.getAll('Authorization') : null;
if (!authorizationHeaders) {
authorizationHeaders = [];
}
if (!this.itemExists(authorizationHeaders, function (item) {
return item.indexOf('Bearer ') === 0;
})) {
/*const token = this._tokenService.getToken();
if (headers && token) {
headers = headers.set('Authorization', 'Bearer ' + token);
}*/
}
return headers;
}
protected handleSuccessResponse(event: HttpEvent<any>, interceptObservable: Subject<HttpEvent<any>>): void {
const self = this;
if (event instanceof HttpResponse) {
if (event.body instanceof Blob && event.body.type && event.body.type.indexOf('application/json') >= 0) {
const clonedResponse = event.clone();
self.configuration.blobToText(event.body).subscribe(function (json) {
const responseBody = json === 'null' ? {} : JSON.parse(json);
const modifiedResponse = self.configuration.handleResponse(event.clone({
body: responseBody
}));
interceptObservable.next(modifiedResponse.clone({
body: new Blob([JSON.stringify(modifiedResponse.body)], {type: 'application/json'})
}));
interceptObservable.complete();
});
} else {
interceptObservable.next(event);
interceptObservable.complete();
}
}
}
protected handleErrorResponse(error: any, interceptObservable: Subject<HttpEvent<any>>): Observable<any> {
const _this = this;
const errorObservable = new Subject();
if (!(error.error instanceof Blob)) {
interceptObservable.error(error);
interceptObservable.complete();
return of({});
}
this.configuration.blobToText(error.error).subscribe(function (json) {
const errorBody = (json === '' || json === 'null') ? {} : JSON.parse(json);
const errorResponse = new HttpResponse({
headers: error.headers,
status: error.status,
body: errorBody
});
const ajaxResponse = _this.configuration.getAbpAjaxResponseOrNull(errorResponse);
if (ajaxResponse != null) {
_this.configuration.handleAbpResponse(errorResponse, ajaxResponse);
} else {
_this.configuration.handleNonAbpErrorResponse(errorResponse);
}
errorObservable.complete();
// prettify error object.
error.error = errorBody;
interceptObservable.error(error);
interceptObservable.complete();
});
return errorObservable;
}
private itemExists<T>(items, predicate) {
for (let i = 0; i < items.length; i++) {
if (predicate(items[i])) {
return true;
}
}
return false;
}
}
在 app.module.ts中使用
1 | ... |