Browse Source

[完善]消防设施网络获取失败

guangxi
邵佳豪 3 years ago
parent
commit
a29982e204
  1. 22
      src/app/http-interceptors/base-interceptor.ts
  2. 841
      src/app/key-unit/fire-fighting-device/fire-fighting-device.component.ts

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

@ -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 './cache-token.service'
import { Router, ActivatedRoute } from '@angular/router'
import { CacheTokenService } from './cache-token.service'
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
//baseurl
@ -15,19 +15,19 @@ import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
@Injectable()
export class BaseInterceptor implements HttpInterceptor {
constructor(private http:HttpClient,private router:Router,private route:ActivatedRoute,public token:CacheTokenService,public snackBar: MatSnackBar) {}
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute, public token: CacheTokenService, public snackBar: MatSnackBar) { }
intercept(req, next: HttpHandler) {
let newReq = req.clone({
url: req.hadBaseurl ? `${req.url}` : `${req.url}`,
});
if(!req.cancelToken) {
if (!req.cancelToken) {
/*获取token*/
let token = sessionStorage.getItem("token")
/*此处设置额外请求头,token令牌*/
newReq.headers =
newReq.headers.set('Authorization', `Bearer ${token}`)
newReq.headers.set('Authorization', `Bearer ${token}`)
}
// 携带请求头发送下一次请求
@ -43,7 +43,7 @@ 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()
@ -51,19 +51,19 @@ 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.status === 403) {
const config = new MatSnackBarConfig();
config.verticalPosition = 'top';
config.duration = 3000
this.snackBar.open('对不起,您无此权限','确定',config);
this.snackBar.open('对不起,您无此权限', '确定', config);
}
if (error.status === 400) {
const config = new MatSnackBarConfig();
config.verticalPosition = 'top';
config.duration = 3000
this.snackBar.open('请核对您的输入格式是否正确','确定',config);
this.snackBar.open('请核对您的输入格式是否正确', '确定', config);
}
if (error.error instanceof ErrorEvent) {
@ -72,9 +72,7 @@ export class BaseInterceptor implements HttpInterceptor {
} else {
// 服务端返回http状态码
// 服务端返回错误信息
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
console.error(error);
}
// 返回带有面向用户的错误信息
return throwError(

841
src/app/key-unit/fire-fighting-device/fire-fighting-device.component.ts

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save