Browse Source

封装与服务器通信的api

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

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

@ -8,82 +8,105 @@ import { catchError, retry } from 'rxjs/operators';
})
export class BuildingBasicInfosService {
readonly c_get = "/Get";
readonly c_post = "/PostOrPut";
constructor(
private http: HttpClient
) { }
/**
*
* api [/]
*/
headers = {
"Content-Type": "application/json"
};
//获取单位信息的api
baseUrl = 'api/Services/3D/BuildingBasicInfo';
readonly api_buildingInfo = '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> {
return this.http.get<string>(this.baseUrl + this.c_get + '?name=' + name);
return this.getInfos(this.api_buildingInfo, name);
}
/**
*
* @param name
* @param data
*/
postBuildingBasicInfos(name: string, data: any): Observable<any> {
let params = {
name: name,
postBuildingBasicInfos(name: string, data: object): Observable<any> {
return this.postInfos(this.api_buildingInfo, name, data);
}
let instance = this;
/**
*
* @param name
*/
getMarkData(name: string): Observable<string> {
return this.getInfos(this.api_sandBox, name);
}
return this.http.post<any>(this.baseUrl + this.c_post, `'${JSON.stringify(data)}'`, { instance.headers, params })
.pipe(
catchError((err) => this.handleError(err))
);
/**
*
* @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
*/
getMarkData(name: string): Observable<string> {
// name = "bb";
getInfos(api: string, name: string) {
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))
);
}
/**
*
* post api的封装
* @param api
* @param name
* @param data
*/
postMarkData(name: string, data: any): Observable<any> {
postInfos(api: string, name: string, data: object) {
let params = {
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(
catchError((err) => this.handleError(err))
);
}
//#endregion
//#region 错误捕捉
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
@ -106,11 +129,5 @@ export class BuildingBasicInfosService {
return throwError(
error);
}
}
/**
*
*/
class HttpResult {
//#endregion
}
Loading…
Cancel
Save