路由跳转

This commit is contained in:
禾几海
2020-08-05 22:16:30 +08:00
parent 6fb62adb91
commit 44f251135c
2 changed files with 41 additions and 11 deletions

View File

@@ -1,23 +1,52 @@
import {Injectable, Injector} from '@angular/core';
import {Response} from '../class/HttpReqAndResp';
import {Injectable} from '@angular/core';
import {RequestObj, Response} from '../class/HttpReqAndResp';
import {HttpService} from '../api/http/http.service';
import {environment} from '../../environments/environment';
import {Router} from '@angular/router';
import {ComponentStateService} from './component-state.service';
@Injectable({
providedIn: 'root'
})
export class ErrorService {
constructor(private httpService: HttpService) {
constructor(private httpService: HttpService, private router: Router,
private componentStateService: ComponentStateService) {
}
private static HTTP_ERROR_COUNT: number = 0;
private readonly MAINTAIN_PAGE_PREFIX = '/maintain'
public httpError(err: any) {
console.log('error=>', err)
if (!environment.production) {
console.log('error=>', err)
}
ErrorService.HTTP_ERROR_COUNT++;
// this.httpService.getSubscriptionQueue().map(a => a.unsubscribe())
}
public httpException(response: Response<any>) {
console.log('exception=>', response)
if (!environment.production)
console.log('exception=>', response)
}
public checkConnection() {
// The HTTP_ERROR_COUNT is start with 1 in this function
if (ErrorService.HTTP_ERROR_COUNT === 1) {
const req: RequestObj = {
path: '/headerInfo',
method: 'GET',
url: environment.host + '/headerInfo'
}
this.httpService.get(req).subscribe({
next: data => null,
error: err => {
if (this.componentStateService.currentPath !== this.MAINTAIN_PAGE_PREFIX) {
this.router.navigateByUrl(this.MAINTAIN_PAGE_PREFIX)
}
ErrorService.HTTP_ERROR_COUNT = 0;
}
})
}
}
}