import {Injectable} from '@angular/core'; import {HttpClient, HttpParams} from '@angular/common/http'; import { AppConsts } from '@shared/AppConsts'; @Injectable() export class RuncardHttp { public restServer; public http; constructor(Http: HttpClient) { this.http = Http; this.restServer = AppConsts.runcardApiUrl + '/'; } public get(url, params?: Object, cb?: Function) { let httpParams = new HttpParams(); const vm = this; if (params) { // tslint:disable-next-line:forin for (const key in params) { httpParams = httpParams.set(key, params[key]); } } vm.http.get(vm.restServer + url, {params: httpParams}) .subscribe(data => { // let result = JSON.parse(data); let result = data; if (result.ErrCode) { if (result.ErrCode === '1') { abp.message.error(result.ErrMsg); return; } } cb(result); }); } public getString(url, params?: Object, cb?: Function) { let httpParams = new HttpParams(); const vm = this; if (params) { // tslint:disable-next-line:forin for (const key in params) { httpParams = httpParams.set(key, params[key]); } } vm.http.get(vm.restServer + url, {params: httpParams}) .subscribe(data => { let result = data; cb(result); }); } public post(url, data?: Object, cb?: Function, options?: Object) { const vm = this; vm.http.post(vm.restServer + url, data, options) .subscribe(res => { let result = JSON.parse(res); if (result.ErrCode === '1') { abp.message.error(result.ErrMsg); return; } cb(res); }); } public put(url, data?: Object, cb?: Function, options?: Object) { const vm = this; vm.http.put(vm.restServer + url, data, options) .subscribe(res => { let result = JSON.parse(res); if (result.ErrCode === '1') { abp.message.error(result.ErrMsg); return; } cb(res); }); } public delete(url, params?: Object, cb?: Function) { let httpParams = new HttpParams(); const vm = this; if (params) { // tslint:disable-next-line:forin for (const key in params) { httpParams = httpParams.set(key, params[key]); } } vm.http.delete(vm.restServer + url, {params: httpParams}) .subscribe(data => { let result = JSON.parse(data); if (result.ErrCode === '1') { abp.message.error(result.ErrMsg); return; } cb(data); }); } }