import { NaHttpInterceptorPreparable, NaHttpRequestHandler } from '../na-http-interceptor-preparable'; import { HttpRequest } from '@angular/common/http'; import { NaHttpService } from '../../na-http.service'; /** * 处理url和withCredentials * * @export * @class NaBaseurlInterceptorPreparable * @implements {NaHttpInterceptorPreparable} */ export class NaBaseurlInterceptorPreparable implements NaHttpInterceptorPreparable { private readonly method = ['POST', 'PUT', 'PATCH']; constructor(protected naHttpService: NaHttpService) { } prepare(req: HttpRequest, next: NaHttpRequestHandler): HttpRequest { let url = req.url; if (!(url.startsWith('http://') || url.startsWith('https://') || url.startsWith('./'))) { url = this.naHttpService.concatUrl(this.naHttpService.baseUrl, url); } let newReq = req; if (this.method.indexOf(req.method) >= 0) { newReq = req.clone({ url: url, withCredentials: true }); } else { newReq = req.clone({ url: url, withCredentials: true }); } return next.handle(newReq); } }