Browse Source

封装与服务器通信的api

dev
刘向辉 3 years ago
parent
commit
1053801371
  1. 101
      src/app/service/babylon/building-basic-infos.service.ts

101
src/app/service/babylon/building-basic-infos.service.ts

@ -8,52 +8,35 @@ import { catchError, retry } from 'rxjs/operators';
}) })
export class BuildingBasicInfosService { export class BuildingBasicInfosService {
readonly c_get = "/Get"; constructor(
readonly c_post = "/PostOrPut"; private http: HttpClient
) { }
/** /**
* * api [/]
*/ */
headers = { readonly api_buildingInfo = 'api/Services/3D/BuildingBasicInfo';
"Content-Type": "application/json"
};
//获取单位信息的api
baseUrl = 'api/Services/3D/BuildingBasicInfo';
/** /**
* ()api * ()api [/]
*/ */
markUrl = "api/Services/3D/Sandboxie"; readonly api_sandBox = "api/Services/3D/Sandboxie";
constructor(
private http: HttpClient
) { }
/** /**
* *
*/ */
getBuildingBasicInfos(name: string): Observable<string> { getBuildingBasicInfos(name: string): Observable<string> {
return this.http.get<string>(this.baseUrl + this.c_get + '?name=' + name); return this.getInfos(this.api_buildingInfo, name);
} }
/** /**
* *
* @param name * @param name
* @param data * @param data
*/ */
postBuildingBasicInfos(name: string, data: any): Observable<any> { postBuildingBasicInfos(name: string, data: object): Observable<any> {
let params = { return this.postInfos(this.api_buildingInfo, name, data);
name: name,
}
let instance = this;
return this.http.post<any>(this.baseUrl + this.c_post, `'${JSON.stringify(data)}'`, { instance.headers, params })
.pipe(
catchError((err) => this.handleError(err))
);
} }
/** /**
@ -61,29 +44,69 @@ export class BuildingBasicInfosService {
* @param name * @param name
*/ */
getMarkData(name: string): Observable<string> { getMarkData(name: string): Observable<string> {
// name = "bb"; return this.getInfos(this.api_sandBox, name);
}
/**
*
* @param name
* @param data
*/
postMarkData(name: string, data: object): Observable<any> {
return this.postInfos(this.api_sandBox, name, data);
}
//#region 通用封装,新增api时调用即可,不需关心具体组装规则。若修改规则,只需修改此通用封装
/**
* get类api所需的固定内容
*/
readonly c_get = "/Get";
/**
* post类api所需的固定内容
*/
readonly c_post = "/PostOrPut";
/**
*
*/
readonly headers = {
"Content-Type": "application/json"
};
/**
* get api的封装
* @param api
* @param name
*/
getInfos(api: string, name: string) {
let id = { "name": name }; let id = { "name": name };
return this.http.get<string>(this.markUrl + this.c_get, { params: id }).pipe( //'?name='
return this.http.get<string>(api + this.c_get, { params: id }).pipe(
catchError((err) => this.handleError(err)) catchError((err) => this.handleError(err))
); );
} }
/** /**
* * post api的封装
* @param api
* @param name * @param name
* @param data * @param data
*/ */
postMarkData(name: string, data: any): Observable<any> { postInfos(api: string, name: string, data: object) {
let params = { let params = {
name: name, name: name,
//content: JSON.stringify(data), };
}
return this.http.post<any>(this.markUrl + this.c_post, JSON.stringify(data), { params }) let headers = this.headers;
return this.http.post<any>(api + this.c_post, `'${JSON.stringify(data)}'`, { headers, params })
.pipe( .pipe(
catchError((err) => this.handleError(err)) catchError((err) => this.handleError(err))
); );
} }
//#endregion
//#region 错误捕捉
private handleError(error: HttpErrorResponse) { private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) { if (error.error instanceof ErrorEvent) {
@ -106,11 +129,5 @@ export class BuildingBasicInfosService {
return throwError( return throwError(
error); error);
} }
} //#endregion
/**
*
*/
class HttpResult {
} }
Loading…
Cancel
Save