Browse Source

[完善]中化http拦截器的修改

dev
邵佳豪 3 years ago
parent
commit
ab100326a0
  1. 41
      src/app/http-interceptors/base-interceptor.ts

41
src/app/http-interceptors/base-interceptor.ts

@ -1,12 +1,13 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { import {
HttpClient, HttpInterceptor, HttpHandler, HttpRequest, HttpClient, HttpInterceptor, HttpHandler, HttpRequest,
HttpErrorResponse HttpErrorResponse,
HttpResponse
} from '@angular/common/http'; } from '@angular/common/http';
import { throwError } from 'rxjs' import { throwError } from 'rxjs'
import { catchError } from 'rxjs/operators'; import { catchError, tap, finalize } from 'rxjs/operators';
import { Router } from '@angular/router' import { Router } from '@angular/router'
import { CacheTokenService } from '../service/cache-token.service'
import { NzMessageService } from 'ng-zorro-antd/message'; import { NzMessageService } from 'ng-zorro-antd/message';
//baseurl //baseurl
// const baseurl = 'http://39.106.78.171:8008'; // const baseurl = 'http://39.106.78.171:8008';
@ -14,7 +15,7 @@ import { NzMessageService } from 'ng-zorro-antd/message';
@Injectable() @Injectable()
export class BaseInterceptor implements HttpInterceptor { export class BaseInterceptor implements HttpInterceptor {
constructor(private router: Router, public token: CacheTokenService, private message: NzMessageService) { } constructor(private router: Router, private message: NzMessageService) { }
intercept(req, next: HttpHandler) { intercept(req, next: HttpHandler) {
@ -34,15 +35,33 @@ export class BaseInterceptor implements HttpInterceptor {
/*获取token*/ /*获取token*/
let token = sessionStorage.getItem("token") let token = sessionStorage.getItem("token")
/*此处设置额外请求头,token令牌*/ /*此处设置额外请求头,token令牌*/
newReq.headers = if (!!token) {
newReq.headers.set('Authorization', `Bearer ${token}`) newReq.headers = newReq.headers.set('Authorization', `Bearer ${token}`)
}
} }
// 携带请求头发送下一次请求 // 携带请求头发送下一次请求
return next.handle(newReq) return next.handle(newReq)
.pipe( .pipe(
//箭头函数,注意this指向 tap(event => {
catchError((err) => this.handleError(err)) if (event instanceof HttpResponse) {
// 成功
// console.log('成功', event.headers.get('x-refresh-encryptedtoken'))
if (!!event.headers.get('x-refresh-token')) {
sessionStorage.setItem('token', event.headers.get('x-refresh-token'))
sessionStorage.setItem('encryptedAccessToken', event.headers.get('x-refresh-encryptedtoken'))
}
}
}, error => {
// 失败
console.log('请求http失败', error)
this.handleError(error)
}),
finalize(() => {
// 请求完成
// console.log('complete')
})
) )
} }
@ -50,10 +69,8 @@ export class BaseInterceptor implements HttpInterceptor {
//401 token过期 403没权限!!! 400参数错误 404未找到 614刷新令牌过期!!! //401 token过期 403没权限!!! 400参数错误 404未找到 614刷新令牌过期!!!
private handleError(error: HttpErrorResponse) { private handleError(error: HttpErrorResponse) {
console.log('http错误', error)
// 用户认证失败返回登录页 // 用户认证失败返回登录页
if (error.status === 401 || error.status === 614) { if (error.status === 401 || error.status === 614) {
this.token.delete()
sessionStorage.clear() sessionStorage.clear()
// window.localStorage.clear() // window.localStorage.clear()
localStorage.removeItem("isautologin") localStorage.removeItem("isautologin")
@ -66,7 +83,9 @@ export class BaseInterceptor implements HttpInterceptor {
if (error.status === 400) { if (error.status === 400) {
this.message.create('error', `请核对您的输入信息或格式是否正确!`); this.message.create('error', `请核对您的输入信息或格式是否正确!`);
} }
if (error.status === 500) {
this.message.create('error', `状态500,服务器错误!`);
}
if (error.error instanceof ErrorEvent) { if (error.error instanceof ErrorEvent) {
// 发生客户端或网络错误。相应处理。 // 发生客户端或网络错误。相应处理。
console.error('An error occurred:', error.error.message); console.error('An error occurred:', error.error.message);

Loading…
Cancel
Save