import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { NumberValueAccessor } from '@angular/forms'; import { Observable, throwError } from 'rxjs'; import { catchError, retry } from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class BuildingBasicInfosService { constructor( public http: HttpClient ) { } /** * 单位信息的api [获取/保存] */ readonly api_buildingInfo = 'api/Services/3D/BuildingBasicInfo'; /** * 事态标绘(电子沙盘)的api [获取/保存] */ readonly api_sandBox = "api/Services/3D/Sandboxie"; /** * 单独保存的一份用于导出的数据 */ readonly api_excelData = "api/Services/3D/GasStationExcelData"; /** * 预案模板 */ readonly api_sandBoxTemplate = "api/Services/3D/SandboxTemplate"; /** * 获取全部的预案模板 */ readonly api_sandBoxTemplateGetAll = "api/Services/3D/SandboxTemplate/GetAll"; /** * 获取三维信息 */ getBuildingBasicInfos(id: number): Observable { return this.getInfos(this.api_buildingInfo, { gasStationId: id }); } /** * 保存三维信息 * @param name * @param data */ postBuildingBasicInfos(name: string, gasstationId: number, data: object): Observable { return this.postInfos(this.api_buildingInfo, data, { name: name, gasstationId: gasstationId }); } /** * 获取事态标绘的信息 * @param name */ getMarkData(id: number): Observable { return this.getInfos(this.api_sandBox, { "gasStationId": id }); } /** * 推送事态标绘的信息 * @param name * @param data */ postMarkData(name: string, id: number, data: object): Observable { return this.postInfos(this.api_sandBox, data, { name: name, "gasStationId": id }); } /** * 推送导出的数据 * @param name * @param id * @param data */ postGasStationExcelData(name: string, id: number, data: object): Observable { return this.postInfos(this.api_excelData, data, { name: name, "gasStationId": id }); } /** * 获取沙盒模板 * @param id */ getSandBoxTemplate(id: number) { return this.getInfos(this.api_sandBoxTemplate, { "Id": id }); } /** * 获取所有沙盒模板 * 较为特殊,所以没走统一的get封装 * @param id */ getAllSandBoxTemplate() { return this.http.get(this.api_sandBoxTemplateGetAll).pipe( catchError((err) => this.handleError(err)) ); } /** * 保存沙盒模板 * @param name * @param id * @param data */ postSandBoxTemplate(name: string, id: number, data: object) { return this.postInfos(this.api_sandBoxTemplate, data, { name: name, "id": id }); } /** * 删除信息 * @param id */ deleteSandBoxTemplate(id: number) { return this.deleteInfos(this.api_sandBoxTemplate, { "Id": id }); } //#region 通用封装,新增api时调用即可,不需关心具体组装规则。若修改规则,只需修改此通用封装 /** * get类api所需的固定内容 */ readonly c_get = "/Get"; /** * post类api所需的固定内容 */ readonly c_post = "/PostOrPut"; /** * 删除信息 */ readonly c_delete = "/Delete"; /** * 通用消息头 */ readonly headers = { "Content-Type": "application/json" }; /** * 通用get 类api的封装 * @param api * @param name */ getInfos(api: string, params = {}) { return this.http.get(api + this.c_get, { params: params }).pipe( catchError((err) => this.handleError(err)) ); } /** * 通用post 类api的封装 * @param api * @param name * @param data */ postInfos(api: string, body: object, params = {}) { let headers = this.headers; let l_body = `'${JSON.stringify(body)}'`; return this.http.post(api + this.c_post, l_body, { headers, params }) .pipe( catchError((err) => this.handleError(err)) ); } /** * 删除信息 * @param api * @param params */ deleteInfos(api: string, params = {}) { return this.http.get(api + this.c_delete, { params: params }).pipe( catchError((err) => this.handleError(err)) ); } //#endregion //#region 错误捕捉 private handleError(error: HttpErrorResponse) { if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. console.error('An error occurred:', error.error.message); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong. if (error.status == 404) { console.log("404===" + error.error); } else { console.error( error + `Backend returned code ${error.status}, ` + `body was: ${error.error}`); } } // Return an observable with a user-facing error message. return throwError( error); } //#endregion }