2016-05-09 23:04:56 +08:00
|
|
|
import {Injectable} from "@angular/core";
|
2017-06-11 04:32:56 +08:00
|
|
|
import {Headers, Http, RequestOptions} from "@angular/http";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {Message} from "../../../../common/entities/Message";
|
2017-06-23 03:23:26 +08:00
|
|
|
import {SlimLoadingBarService} from "ng2-slim-loading-bar";
|
2016-05-05 03:08:05 +08:00
|
|
|
import "rxjs/Rx";
|
2017-07-04 01:17:49 +08:00
|
|
|
import {ErrorCodes} from "../../../../common/entities/Error";
|
2016-03-13 05:19:24 +08:00
|
|
|
|
2016-05-05 03:08:05 +08:00
|
|
|
@Injectable()
|
2016-05-09 23:04:56 +08:00
|
|
|
export class NetworkService {
|
2016-03-13 05:19:24 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
_baseUrl = "/api";
|
2016-03-13 05:19:24 +08:00
|
|
|
|
2017-06-22 03:16:04 +08:00
|
|
|
constructor(protected _http: Http, private slimLoadingBarService: SlimLoadingBarService) {
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 05:19:24 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private callJson<T>(method: string, url: string, data: any = {}): Promise<T> {
|
|
|
|
let body = JSON.stringify(data);
|
|
|
|
let headers = new Headers({'Content-Type': 'application/json'});
|
|
|
|
let options = new RequestOptions({headers: headers});
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-22 03:16:04 +08:00
|
|
|
this.slimLoadingBarService.visible = true;
|
|
|
|
this.slimLoadingBarService.start(() => {
|
|
|
|
this.slimLoadingBarService.visible = false;
|
|
|
|
});
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
const process = (data: any): T => {
|
2017-06-22 03:16:04 +08:00
|
|
|
this.slimLoadingBarService.complete();
|
2017-07-04 01:17:49 +08:00
|
|
|
let res = <Message<any>> data.json();
|
|
|
|
if (!!res.error) {
|
|
|
|
if (res.error.code) {
|
|
|
|
res.error['title'] = ErrorCodes[res.error.code];
|
|
|
|
}
|
|
|
|
throw res.error;
|
|
|
|
}
|
|
|
|
return res.result;
|
2017-06-22 03:16:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const err = (err) => {
|
|
|
|
this.slimLoadingBarService.complete();
|
2017-06-23 03:23:26 +08:00
|
|
|
return NetworkService.handleError(err);
|
2017-06-22 03:16:04 +08:00
|
|
|
};
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (method == "get" || method == "delete") {
|
|
|
|
return <any>this._http[method](this._baseUrl + url, options)
|
|
|
|
.toPromise()
|
2017-06-22 03:16:04 +08:00
|
|
|
.then(process)
|
|
|
|
.catch(err);
|
2016-03-20 00:31:42 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
return this._http[method](this._baseUrl + url, body, options)
|
|
|
|
.toPromise()
|
2017-06-22 03:16:04 +08:00
|
|
|
.then(process)
|
|
|
|
.catch(err);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public postJson<T>(url: string, data: any = {}): Promise<T> {
|
|
|
|
return this.callJson("post", url, data);
|
|
|
|
}
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public putJson<T>(url: string, data: any = {}): Promise<T> {
|
|
|
|
return this.callJson("put", url, data);
|
|
|
|
}
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public getJson<T>(url: string): Promise<T> {
|
|
|
|
return this.callJson("get", url);
|
|
|
|
}
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
public deleteJson<T>(url: string): Promise<T> {
|
|
|
|
return this.callJson("delete", url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static handleError(error: any) {
|
2017-07-13 00:31:19 +08:00
|
|
|
if (error.code) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
// TODO: in a real world app do something better
|
2017-06-11 04:32:56 +08:00
|
|
|
// instead of just logging it to the console
|
|
|
|
console.error(error);
|
2017-07-04 01:17:49 +08:00
|
|
|
return Promise.reject(error.message || error || 'Server error');
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 05:19:24 +08:00
|
|
|
}
|