Commit fe401d3b by Sweet Zhang

增加拦截器

parent 2813f6a9
......@@ -8,7 +8,7 @@ import {Router} from '@angular/router';
import {AuthModule} from './auth/auth.module';
import {HttpClientModule} from '@angular/common/http';
import {LifeCommonModule} from './common/life-common.module';
import {HashLocationStrategy, LocationStrategy} from "@angular/common";
import {httpInterceptorProviders} from './http-interceptors/index';
@NgModule({
declarations: [
......@@ -23,16 +23,8 @@ import {HashLocationStrategy, LocationStrategy} from "@angular/common";
HttpClientModule,
AppRoutingModule
],
providers: [],
providers: [httpInterceptorProviders],
bootstrap: [AppComponent]
})
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 {Observable, of} from 'rxjs';
import {tap, delay} from 'rxjs/operators';
import {environment} from '../../environments/environment';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {retry} from 'rxjs/internal/operators';
import {HttpClient} from '@angular/common/http';
@Injectable({
providedIn: 'root',
......@@ -20,51 +18,21 @@ export class AuthService {
// 经纪人登陆
login(loginInfo): Observable<any> {
const api = this.API + '/practitionerLogin';
const httpOptions = {
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),
);
return this.http.post(api, JSON.stringify(loginInfo));
}
// 发送验证码
verificationCode(verticalCode) {
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
.post(url, JSON.stringify(verticalCode), httpOptions)
.pipe(
tap(response => response)
);
.post(url, JSON.stringify(verticalCode));
}
// 校验验证码
compare(compareCode) {
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
.post(url, JSON.stringify(compareCode), httpOptions)
.pipe(
tap(response => response)
);
.post(url, JSON.stringify(compareCode));
}
/**
......@@ -75,17 +43,6 @@ export class AuthService {
const ticketObj = {
ticket: 'life'
};
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}
)
};
return this.http.post(url, JSON.stringify(ticketObj), httpOptions)
.pipe(
tap(response => {
return response;
})
);
return this.http.post(url, JSON.stringify(ticketObj));
}
}
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