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('='))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return this.commonProcess(this.httpClient.get(encodeURI(url + paramsString.join('&') + '&t=' + t), options = {}));
|
2018-11-21 18:55:57 +08:00
|
|
|
} else {
|
2018-11-15 18:29:55 +08:00
|
|
|
return this.commonProcess(this.httpClient.get(url + 't=' + t, options = {}));
|
|
|
|
|
}
|
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-21 18:55:57 +08:00
|
|
|
patch(url: string, body, options?): Observable<any> {
|
|
|
|
|
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-21 18:55:57 +08:00
|
|
|
post(url: string, body, options?): Observable<any> {
|
|
|
|
|
return this.commonProcess(this.httpClient.post(url, body, options));
|
2018-10-26 11:33:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* put
|
|
|
|
|
* @param url
|
|
|
|
|
* @param options
|
|
|
|
|
*/
|
2018-11-21 18:55:57 +08:00
|
|
|
put(url: string, body, options?): Observable<any> {
|
|
|
|
|
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> {
|
|
|
|
|
// this.spinService.spin(true);
|
|
|
|
|
return Observable.create((observer) => {
|
|
|
|
|
observable.subscribe(res => {
|
|
|
|
|
// this.spinService.spin(false);
|
|
|
|
|
observer.next(res);
|
|
|
|
|
}, (err) => {
|
|
|
|
|
// this.spinService.spin(false);
|
|
|
|
|
observer.error(err);
|
|
|
|
|
}, () => {
|
|
|
|
|
// this.spinService.spin(false);
|
|
|
|
|
observer.complete();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|