Commit 0a0b4632 authored by wangqinghua's avatar wangqinghua

error message提示

parent d7c91faf
...@@ -31,6 +31,7 @@ import {route} from './app.route'; ...@@ -31,6 +31,7 @@ import {route} from './app.route';
import {LoginGuard} from './shared/common/loginGuard'; import {LoginGuard} from './shared/common/loginGuard';
import {AppMainModule} from './app.main.module'; import {AppMainModule} from './app.main.module';
import {AppService} from './app.service'; import {AppService} from './app.service';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
@NgModule({ @NgModule({
imports: [ imports: [
...@@ -67,7 +68,8 @@ import {AppService} from './app.service'; ...@@ -67,7 +68,8 @@ import {AppService} from './app.service';
multi: true, multi: true,
deps: [ deps: [
LocalStorageService, LocalStorageService,
SessionStorageService SessionStorageService,
NzMessageService,
] ]
}, },
{ {
...@@ -83,7 +85,9 @@ import {AppService} from './app.service'; ...@@ -83,7 +85,9 @@ import {AppService} from './app.service';
useClass: ErrorHandlerInterceptor, useClass: ErrorHandlerInterceptor,
multi: true, multi: true,
deps: [ deps: [
JhiEventManager JhiEventManager,
NzMessageService,
NzNotificationService,
] ]
}, },
{ {
......
...@@ -3,13 +3,14 @@ import { LocalStorageService, SessionStorageService } from 'ngx-webstorage'; ...@@ -3,13 +3,14 @@ import { LocalStorageService, SessionStorageService } from 'ngx-webstorage';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { SERVER_API_URL } from '../../app.constants'; import { SERVER_API_URL } from '../../app.constants';
import {ActivatedRoute, Router} from "@angular/router"; import {ActivatedRoute, Router} from "@angular/router";
import {NzMessageService} from 'ng-zorro-antd';
export class AuthInterceptor implements HttpInterceptor { export class AuthInterceptor implements HttpInterceptor {
constructor( constructor(
private localStorage: LocalStorageService, private localStorage: LocalStorageService,
private sessionStorage: SessionStorageService, private sessionStorage: SessionStorageService,
public router: Router, public router: Router,private message:NzMessageService,
) { ) {
} }
......
import { JhiEventManager } from 'ng-jhipster'; import {JhiEventManager} from 'ng-jhipster';
import { HttpInterceptor, HttpRequest, HttpErrorResponse, HttpHandler, HttpEvent } from '@angular/common/http'; import {HttpInterceptor, HttpRequest, HttpErrorResponse, HttpHandler, HttpEvent} from '@angular/common/http';
import { Observable } from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/do'; import 'rxjs/add/operator/do';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';
export class ErrorHandlerInterceptor implements HttpInterceptor { export class ErrorHandlerInterceptor implements HttpInterceptor {
constructor(private eventManager: JhiEventManager) { constructor(private eventManager: JhiEventManager, private message: NzMessageService,
public notification: NzNotificationService) {
} }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {}, (err: any) => { return next.handle(request).do((event: HttpEvent<any>) => {
}, (err: any) => {
if (err instanceof HttpErrorResponse) { if (err instanceof HttpErrorResponse) {
if (!(err.status === 401 && (err.message === '' || (err.url && err.url.indexOf('/api/account') === 0)))) { this.messageInfo(err);
if (this.eventManager !== undefined) {
this.eventManager.broadcast({name: 'bootappApp.httpError', content: err});
} }
});
} }
messageInfo(err) {
const status = err.status;
switch (status) {
case 401:
this.notification.create('error', '错误', '登录信息已过期', {nzDuration: 2000});
break;
case 404:
this.notification.create('error', '错误', `${err.url}不存在`, {nzDuration: 2000});
break;
case 500:
this.notification.create('error', '错误', `${err.message}`, {nzDuration: 2000});
break;
} }
});
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment