import {forwardRef, Inject, Injectable, Injector} from '@angular/core'; import {RequestObj} from '../../class/HttpReqAndResp'; import {HttpClient, HttpResponse} from '@angular/common/http'; import {environment} from '../../../environments/environment'; import {LocalStorageService} from '../../services/local-storage.service'; import {Response} from '../../class/HttpReqAndResp'; import {Observable, Observer, Subscription} from 'rxjs'; import {ErrorService} from '../../services/error.service'; @Injectable({ providedIn: 'root', }) export class HttpService { constructor(private httpClient: HttpClient, protected localStorageService: LocalStorageService, private injector: Injector) { } private subscriptionQueue: Subscription[] = []; // private errorDispatch: ErrDispatch; public getSubscriptionQueue = () => this.subscriptionQueue; // setErrDispatch(errDispatch: ErrDispatch) { // this.errorDispatch = errDispatch; // } Service(request: RequestObj) { const errorService = this.injector.get(ErrorService); request.url = null; // 设置默认值 request.contentType = request.contentType == null ? 'application/x-www-form-urlencoded' : request.contentType; request.header = { 'Content-Type': request.contentType }; const token = this.localStorageService.getToken(); if (token != null) { request.header.Authorization = token; } request.url = this.checkUrl(request); let observable: Observable>>; switch (request.method) { case 'GET': observable = this.get>(request); break; case 'DELETE': observable = this.delete>(request); break; case 'PUT': observable = this.put>(request); break; case 'POST': observable = this.post>(request); break; } let observer: Observer>; const oob = new Observable>(o => observer = o); const subscription = observable.subscribe({ next: o => { const tokenFromReps = o.headers.get('Authorization'); if (tokenFromReps) { this.localStorageService.setToken(tokenFromReps); } if (o.body.code !== 0) { observer.error(o.body); errorService.httpException(o.body) // if (this.errorDispatch) { // this.errorDispatch.errHandler(o.body.code, o.body.msg, request); // } } else { observer.next(o.body); } observer.complete(); }, error: err => { errorService.httpError(err); this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) }, complete: () => this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) }); this.subscriptionQueue.push(subscription); return oob; } private get(request: RequestObj) { return this.httpClient.get(request.url, { headers: request.header, withCredentials: true, observe: 'response' }); } private post(request: RequestObj) { return this.httpClient.post(request.url, request.data, { headers: request.header, withCredentials: true, observe: 'response' }); } private put(request: RequestObj) { return this.httpClient.put(request.url, request.data, { headers: request.header, withCredentials: true, observe: 'response' }); } private delete(request: RequestObj) { return this.httpClient.delete(request.url, { headers: request.header, withCredentials: true, observe: 'response' }); } /** * 验证并且处理拼接 URl * @param req Request */ private checkUrl(req: RequestObj): string { let tmpUrl = environment.host; if (req.path.length === 0) { return tmpUrl; } if (req.path.substr(0, 1) !== '/') { tmpUrl += '/'; } let queryStr = ''; const keys = req.queryParam == null ? [] : Object.keys(req.queryParam); if (keys.length === 0) { return tmpUrl + req.path; } for (const key of keys) { queryStr += '&' + key + '=' + req.queryParam[key]; } queryStr = queryStr.substr(1, queryStr.length); return tmpUrl + req.path + '?' + queryStr; } }