Commit fe401d3b by Sweet Zhang

增加拦截器

parent 2813f6a9
...@@ -8,7 +8,7 @@ import {Router} from '@angular/router'; ...@@ -8,7 +8,7 @@ import {Router} from '@angular/router';
import {AuthModule} from './auth/auth.module'; import {AuthModule} from './auth/auth.module';
import {HttpClientModule} from '@angular/common/http'; import {HttpClientModule} from '@angular/common/http';
import {LifeCommonModule} from './common/life-common.module'; import {LifeCommonModule} from './common/life-common.module';
import {HashLocationStrategy, LocationStrategy} from "@angular/common"; import {httpInterceptorProviders} from './http-interceptors/index';
@NgModule({ @NgModule({
declarations: [ declarations: [
...@@ -23,16 +23,8 @@ import {HashLocationStrategy, LocationStrategy} from "@angular/common"; ...@@ -23,16 +23,8 @@ import {HashLocationStrategy, LocationStrategy} from "@angular/common";
HttpClientModule, HttpClientModule,
AppRoutingModule AppRoutingModule
], ],
providers: [], providers: [httpInterceptorProviders],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { export class AppModule {
// // Diagnostic only: inspect router configuration
// constructor(router: Router) {
// // Use a custom replacer to display function names in the route configs
// const replacer = (key, value) => (typeof value === 'function') ? value.name : value;
//
// console.log('Routes: ', JSON.stringify(router.config, replacer, 2));
// }
// { provide: LocationStrategy, useClass: HashLocationStrategy }
} }
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Observable, of} from 'rxjs'; import {Observable, of} from 'rxjs';
import {tap, delay} from 'rxjs/operators';
import {environment} from '../../environments/environment'; import {environment} from '../../environments/environment';
import {HttpClient, HttpHeaders} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {retry} from 'rxjs/internal/operators';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
...@@ -20,51 +18,21 @@ export class AuthService { ...@@ -20,51 +18,21 @@ export class AuthService {
// 经纪人登陆 // 经纪人登陆
login(loginInfo): Observable<any> { login(loginInfo): Observable<any> {
const api = this.API + '/practitionerLogin'; const api = this.API + '/practitionerLogin';
const httpOptions = { return this.http.post(api, JSON.stringify(loginInfo));
headers: new HttpHeaders({
'Content-Type': 'application/json',
'X-Authorization': localStorage.getItem('lifeToken') ? localStorage.getItem('lifeToken') : ''
}
)
};
return this.http.post(api, JSON.stringify(loginInfo), httpOptions)
.pipe(
retry(3),
);
} }
// 发送验证码 // 发送验证码
verificationCode(verticalCode) { verificationCode(verticalCode) {
const url = this.API + '/verificationCode'; const url = this.API + '/verificationCode';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'X-Authorization': localStorage.getItem('lifeToken') ? localStorage.getItem('lifeToken') : ''
}
)
};
return this.http return this.http
.post(url, JSON.stringify(verticalCode), httpOptions) .post(url, JSON.stringify(verticalCode));
.pipe(
tap(response => response)
);
} }
// 校验验证码 // 校验验证码
compare(compareCode) { compare(compareCode) {
const url = this.API + '/checkVerificationCode'; const url = this.API + '/checkVerificationCode';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'X-Authorization': localStorage.getItem('lifeToken') ? localStorage.getItem('lifeToken') : ''
}
)
};
return this.http return this.http
.post(url, JSON.stringify(compareCode), httpOptions) .post(url, JSON.stringify(compareCode));
.pipe(
tap(response => response)
);
} }
/** /**
...@@ -75,17 +43,6 @@ export class AuthService { ...@@ -75,17 +43,6 @@ export class AuthService {
const ticketObj = { const ticketObj = {
ticket: 'life' ticket: 'life'
}; };
const httpOptions = { return this.http.post(url, JSON.stringify(ticketObj));
headers: new HttpHeaders({
'Content-Type': 'application/json'
}
)
};
return this.http.post(url, JSON.stringify(ticketObj), httpOptions)
.pipe(
tap(response => {
return response;
})
);
} }
} }
import {Injectable} from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import {Observable} from "rxjs/index";
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({
headers: req.headers.set('X-Authorization', localStorage.getItem('lifeToken') ? localStorage.getItem('lifeToken') : ''),
setHeaders: {'Content-Type': 'application/json'}
});
return next.handle(authReq);
}
}
/* "Barrel" of Http Interceptors */
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { AuthInterceptor } from './auth-interceptor';
import { TrimNameInterceptor } from './trim-name-interceptor';
/** Http interceptor providers in outside-in order */
export const httpInterceptorProviders = [
{ provide: HTTP_INTERCEPTORS, useClass: TrimNameInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
];
/**
* Created by Sweet on 2019/12/5.
*/
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class TrimNameInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const body = req.body;
if (!body || !body.name ) {
return next.handle(req);
}
// copy the body and trim whitespace from the name property
const newBody = { ...body, name: body.name.trim() };
// clone request and set its body
const newReq = req.clone({ body: newBody });
// send the cloned request to the next handler.
return next.handle(newReq);
}
}
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