动态消息更新优化及请求错误处理

This commit is contained in:
zhouyuejun
2018-12-26 15:00:23 +08:00
parent f49f92ea9f
commit a94a456338
5 changed files with 114 additions and 166 deletions
+34
View File
@@ -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();
}
}