|
|
|
@ -5,8 +5,8 @@ import {
|
|
|
|
|
} from '@angular/common/http'; |
|
|
|
|
import { throwError } from 'rxjs' |
|
|
|
|
import { catchError, retry } from 'rxjs/operators'; |
|
|
|
|
import { Router,ActivatedRoute } from '@angular/router' |
|
|
|
|
import {CacheTokenService} from '../service/cache-token.service' |
|
|
|
|
import { Router, ActivatedRoute } from '@angular/router' |
|
|
|
|
import { CacheTokenService } from '../service/cache-token.service' |
|
|
|
|
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar'; |
|
|
|
|
import { CookieService } from 'ngx-cookie-service'; |
|
|
|
|
|
|
|
|
@ -16,20 +16,20 @@ import { CookieService } from 'ngx-cookie-service';
|
|
|
|
|
@Injectable() |
|
|
|
|
export class BaseInterceptor implements HttpInterceptor { |
|
|
|
|
|
|
|
|
|
constructor(private http:HttpClient,private router:Router,private route:ActivatedRoute,public token:CacheTokenService, |
|
|
|
|
public snackBar: MatSnackBar,private cookieService: CookieService) {} |
|
|
|
|
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute, public token: CacheTokenService, |
|
|
|
|
public snackBar: MatSnackBar, private cookieService: CookieService) { } |
|
|
|
|
|
|
|
|
|
intercept(req, next: HttpHandler) { |
|
|
|
|
|
|
|
|
|
let newReq = req.clone({ |
|
|
|
|
url: req.hadBaseurl ? `${req.url}` : `${req.url}`, |
|
|
|
|
}); |
|
|
|
|
if(!req.cancelToken) { |
|
|
|
|
if (!req.cancelToken) { |
|
|
|
|
/*获取token*/ |
|
|
|
|
let token = this.cookieService.get("token") |
|
|
|
|
/*此处设置额外请求头,token令牌*/ |
|
|
|
|
newReq.headers = |
|
|
|
|
newReq.headers.set('Authorization', `Bearer ${token}`) |
|
|
|
|
newReq.headers.set('Authorization', `Bearer ${token}`) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 携带请求头发送下一次请求
|
|
|
|
@ -45,12 +45,12 @@ export class BaseInterceptor implements HttpInterceptor {
|
|
|
|
|
|
|
|
|
|
private handleError(error: HttpErrorResponse) { |
|
|
|
|
// 用户认证失败返回登录页
|
|
|
|
|
if (error.status === 401||error.status === 614) { |
|
|
|
|
if (error.status === 401 || error.status === 614) { |
|
|
|
|
this.token.delete() |
|
|
|
|
sessionStorage.clear() |
|
|
|
|
window.localStorage.clear() |
|
|
|
|
this.cookieService.set("token",'',new Date(new Date().getTime() + 1),'/') |
|
|
|
|
this.cookieService.set("refreshToken",'',new Date(new Date().getTime() + 1),'/') |
|
|
|
|
this.cookieService.set("token", '', new Date(new Date().getTime() + 1), '/') |
|
|
|
|
this.cookieService.set("refreshToken", '', new Date(new Date().getTime() + 1), '/') |
|
|
|
|
this.snackBar.open('用户认证信息过期,请重新登录', '确定', { |
|
|
|
|
duration: 3000 |
|
|
|
|
}); |
|
|
|
@ -65,7 +65,7 @@ export class BaseInterceptor implements HttpInterceptor {
|
|
|
|
|
const config = new MatSnackBarConfig(); |
|
|
|
|
config.verticalPosition = 'top'; |
|
|
|
|
config.duration = 3000 |
|
|
|
|
this.snackBar.open('请核对您的输入格式是否正确','确定',config); |
|
|
|
|
this.snackBar.open('请核对您的输入格式是否正确', '确定', config); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (error.error instanceof ErrorEvent) { |
|
|
|
@ -80,6 +80,6 @@ export class BaseInterceptor implements HttpInterceptor {
|
|
|
|
|
} |
|
|
|
|
// 返回带有面向用户的错误信息
|
|
|
|
|
return throwError( |
|
|
|
|
error.error); |
|
|
|
|
error); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|