Files
airport-chengdu-web/src/app/services/http-client.service.ts
T

124 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-10-26 11:33:25 +08:00
import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest, HttpHeaders, HttpParams } from '@angular/common/http';
import { HttpObserve } from '@angular/common/http/src/client';
import { Observable } from 'rxjs';
/**
* httpclient服务
*/
@Injectable()
export class HttpClientService {
constructor(
private httpClient: HttpClient,
) { }
/**
* request
*
* @return any
*/
2018-11-21 18:55:57 +08:00
request(method: string, url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.request(method, url, options));
2018-10-26 11:33:25 +08:00
}
/**
* delete
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
delete(url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.delete(url, options));
2018-10-26 11:33:25 +08:00
}
/**
* get
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
get(url: string, params?: object, options?): Observable<any> {
2018-11-15 18:29:55 +08:00
let paramsString = [];
url = url + '?'
let t = Math.random();
2018-11-21 18:55:57 +08:00
if (params && params) {
2018-11-15 18:29:55 +08:00
for (const key in params) {
if (params[key] !== '') {
paramsString.push([key, params[key]].join('='))
}
}
2018-11-27 18:16:25 +08:00
return this.commonProcess(this.httpClient.get(encodeURI(url + paramsString.join('&') + '&t=' + t), options));
2018-11-21 18:55:57 +08:00
} else {
2018-11-27 18:16:25 +08:00
return this.commonProcess(this.httpClient.get(url + 't=' + t, options));
2018-11-15 18:29:55 +08:00
}
2018-10-26 11:33:25 +08:00
}
/**
* head
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
head(url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.head(url, options));
2018-10-26 11:33:25 +08:00
}
/**
* options
* @param url
* @param options
*/
2018-11-21 18:55:57 +08:00
options(url: string, options?): Observable<any> {
return this.commonProcess(this.httpClient.options(url, options));
2018-10-26 11:33:25 +08:00
}
/**
* patch
* @param url
* @param options
*/
2018-11-27 18:16:25 +08:00
patch(url: string, body?, options?): Observable<any> {
2018-11-21 18:55:57 +08:00
return this.commonProcess(this.httpClient.patch(url, body, options));
2018-10-26 11:33:25 +08:00
}
/**
* post
* @param url
2018-11-21 18:55:57 +08:00
* @param body
2018-10-26 11:33:25 +08:00
* @param options
*/
2018-11-27 18:16:25 +08:00
post(url: string, body?, options?): Observable<any> {
2018-11-21 18:55:57 +08:00
return this.commonProcess(this.httpClient.post(url, body, options));
2018-10-26 11:33:25 +08:00
}
/**
* put
* @param url
* @param options
*/
2018-11-27 18:16:25 +08:00
put(url: string, body?, options?): Observable<any> {
2018-11-21 18:55:57 +08:00
return this.commonProcess(this.httpClient.put(url, body, options));
2018-10-26 11:33:25 +08:00
}
/**
* 公共处理
* @param observable
*/
commonProcess(observable: Observable<any>): Observable<any> {
return Observable.create((observer) => {
observable.subscribe(res => {
observer.next(res);
}, (err) => {
observer.error(err);
}, () => {
observer.complete();
});
});
}
}