动态消息更新优化及请求错误处理
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { HttpClient, HttpRequest, HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
import { HttpObserve } from '@angular/common/http/src/client';
|
||||
import { OperateSpinServiceService } from './operate-spin-service.service';
|
||||
import { ToastService } from '../components/toast/toast.service';
|
||||
import { ToastConfig, ToastType } from '../components/toast/toast-model';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@@ -11,7 +15,10 @@ import { Observable } from 'rxjs';
|
||||
export class HttpClientService {
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private httpClient: HttpClient,
|
||||
private operateSpinServiceService: OperateSpinServiceService,
|
||||
private toastService: ToastService,
|
||||
) { }
|
||||
|
||||
|
||||
@@ -113,6 +120,27 @@ export class HttpClientService {
|
||||
observable.subscribe(res => {
|
||||
observer.next(res);
|
||||
}, (err) => {
|
||||
switch (err.status) {
|
||||
case 401: {
|
||||
this.operateSpinServiceService.hideSpin();
|
||||
let cookieMsg = ['SESSION', 'token']
|
||||
let sessionStorageMsg = ['userInfo']
|
||||
sessionStorageMsg.forEach(element => {
|
||||
sessionStorage.removeItem(element)
|
||||
})
|
||||
cookieMsg.forEach(element => {
|
||||
this.delCookie(element)
|
||||
})
|
||||
this.router.navigate(['/login']);
|
||||
break;
|
||||
};
|
||||
case 500: {
|
||||
this.operateSpinServiceService.hideSpin();
|
||||
const toastCfg = new ToastConfig(ToastType.ERROR, '', '数据请求失败!', 5000);
|
||||
this.toastService.toast(toastCfg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
observer.error(err);
|
||||
}, () => {
|
||||
observer.complete();
|
||||
@@ -120,4 +148,10 @@ export class HttpClientService {
|
||||
});
|
||||
}
|
||||
|
||||
delCookie($name) {
|
||||
let myDate = new Date();
|
||||
myDate.setTime(-1000);
|
||||
document.cookie = $name + "=''; expires=" + myDate.toUTCString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user