|
@@ -1,86 +1,43 @@
|
1
|
|
-import { Injectable } from '@angular/core';
|
2
|
|
-import { HttpInterceptor, HttpEvent, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
|
3
|
|
-import { Observable } from 'rxjs';
|
4
|
|
-import { tap } from 'rxjs/operators';
|
5
|
|
-import { Router } from '@angular/router'
|
6
|
|
-import { NzMessageService } from 'ng-zorro-antd/message';
|
7
|
|
-const ignoreToken = ['login', 'logout', 'table'];
|
8
|
|
-// @Injectable()
|
|
1
|
+import {
|
|
2
|
+ HttpInterceptor,
|
|
3
|
+ HttpEvent,
|
|
4
|
+ HttpHandler,
|
|
5
|
+ HttpRequest,
|
|
6
|
+ HttpResponse,
|
|
7
|
+} from "@angular/common/http";
|
|
8
|
+import { Observable } from "rxjs";
|
|
9
|
+import { tap } from "rxjs/operators";
|
|
10
|
+// const ignoreToken = ['login', 'logout', 'table'];
|
9
|
11
|
export class HttpInterceptorService implements HttpInterceptor {
|
10
|
|
- constructor(private router: Router, private msg: NzMessageService) { }
|
11
|
|
- intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
12
|
|
- // alert(6)
|
13
|
|
- // 先补全请求协议
|
14
|
|
- let url = req.url;
|
15
|
|
- const needToken = ignoreToken.filter(u => url.match(u));
|
16
|
|
- // if (url.indexOf('http://') < 0 || url.indexOf('https://') < 0) {
|
17
|
|
- // url = 'http://' + url;
|
18
|
|
- // }
|
|
12
|
+ constructor() {}
|
|
13
|
+ intercept(
|
|
14
|
+ req: HttpRequest<any>,
|
|
15
|
+ next: HttpHandler
|
|
16
|
+ ): Observable<HttpEvent<any>> {
|
|
17
|
+ // const url = req.url;
|
|
18
|
+ // const needToken = ignoreToken.filter(u => url.match(u));
|
19
|
19
|
// 过滤掉不需要token的请求
|
20
|
|
- if (!needToken.length) {
|
21
|
|
- req = req.clone({
|
22
|
|
- url
|
23
|
|
- });
|
24
|
|
- } else {
|
25
|
|
- req = req.clone({
|
26
|
|
- url,
|
27
|
|
- headers: req.headers.set('token', 'asdqwe')
|
28
|
|
- });
|
29
|
|
- }
|
|
20
|
+ // if (!needToken.length) {
|
|
21
|
+ // req = req.clone({
|
|
22
|
+ // url
|
|
23
|
+ // });
|
|
24
|
+ // } else {
|
|
25
|
+ // req = req.clone({
|
|
26
|
+ // url,
|
|
27
|
+ // headers: req.headers.set('token', 'asdqwe')
|
|
28
|
+ // });
|
|
29
|
+ // }
|
30
|
30
|
return next.handle(req).pipe(
|
31
|
31
|
tap(
|
32
|
|
- event => {
|
|
32
|
+ (event) => {
|
33
|
33
|
if (event instanceof HttpResponse) {
|
34
|
|
- let that = this;
|
35
|
|
- if (event.body == 520) {
|
36
|
|
- // 登录失效
|
37
|
|
- // console.log('event登录失效')
|
38
|
|
- // that.msg.error('您的登录已失效,请重新登录!', {
|
39
|
|
- // nzDuration: 3000
|
40
|
|
- // });
|
41
|
|
- setTimeout(() => {
|
42
|
|
- that.router.navigateByUrl('login');
|
43
|
|
- }, 2000);
|
44
|
|
- localStorage.removeItem('user')
|
45
|
|
- localStorage.removeItem('menu')
|
46
|
|
- localStorage.removeItem('phones')
|
47
|
|
- localStorage.removeItem('index')
|
48
|
|
- return;
|
49
|
|
- }
|
50
|
|
- else if (event.status == 500) {
|
51
|
|
- // 系统异常
|
52
|
|
- console.log('event系统异常')
|
53
|
|
- // that.msg.error(event.body.remarks, {
|
54
|
|
- // nzDuration: 2000
|
55
|
|
- // });
|
56
|
|
- return
|
57
|
|
- }
|
|
34
|
+ console.log(event);
|
58
|
35
|
}
|
59
|
36
|
},
|
60
|
|
- error => {
|
61
|
|
- // console.log(error)
|
62
|
|
- let that = this;
|
63
|
|
- if (error.error && error.error.text == 520) {
|
64
|
|
- // 登录失效
|
65
|
|
- setTimeout(() => {
|
66
|
|
- that.router.navigateByUrl('login');
|
67
|
|
- localStorage.removeItem('user')
|
68
|
|
- localStorage.removeItem('menu')
|
69
|
|
- localStorage.removeItem('phones')
|
70
|
|
- localStorage.removeItem('index')
|
71
|
|
- }, 2000);
|
72
|
|
- return
|
73
|
|
- }
|
74
|
|
- else if (error.status == 500) {
|
75
|
|
- // 系统异常
|
76
|
|
- console.log('error系统异常,请重试!')
|
77
|
|
- // that.msg.error('error系统异常,请重试!', {
|
78
|
|
- // nzDuration: 3000
|
79
|
|
- // });
|
80
|
|
- return
|
81
|
|
- }
|
82
|
|
-
|
83
|
|
- })
|
|
37
|
+ (error) => {
|
|
38
|
+ console.log(error);
|
|
39
|
+ }
|
|
40
|
+ )
|
84
|
41
|
);
|
85
|
42
|
}
|
86
|
43
|
}
|