|
|
|
import { HttpErrorResponse } from "@angular/common/http";
|
|
|
|
import { classToPlain } from "class-transformer";
|
|
|
|
import { PlanComponent } from "src/app/pages/plan/plan.component";
|
|
|
|
import { BuildingBasicInfosService } from "src/app/service/babylon/building-basic-infos.service";
|
|
|
|
import { ObjectsService } from "src/app/service/objects.service";
|
|
|
|
import { InsitutionDataSimple } from "../model/data/institution/institution-data-simple";
|
|
|
|
import { AllMarkPlanData } from "../model/data/mark/mark-plan-data";
|
|
|
|
import { ModeManager, ModeType } from "./mode-manager";
|
|
|
|
|
|
|
|
//服务器通信
|
|
|
|
export class ServeManager {
|
|
|
|
|
|
|
|
static ngAssetsPath = "assets/";//资源根目录
|
|
|
|
|
|
|
|
static instance: ServeManager = null;
|
|
|
|
|
|
|
|
postFilePath: string;//上传文件的相对路径
|
|
|
|
|
|
|
|
static Init(buildingBISrv: BuildingBasicInfosService, objectsSrv: ObjectsService) {
|
|
|
|
ServeManager.instance = new ServeManager(buildingBISrv, objectsSrv);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private buildingBISrv: BuildingBasicInfosService,
|
|
|
|
private objectsSrv: ObjectsService
|
|
|
|
) {
|
|
|
|
//this.fileInput = document.getElementById("file");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//从服务器获取单位信息
|
|
|
|
getInstitutionData(key: string = 'test', onSuccess?: (key: string,
|
|
|
|
data: string
|
|
|
|
) => void, onError?: (key: string, error: any) => void) {
|
|
|
|
this.buildingBISrv.getBuildingBasicInfos(key)
|
|
|
|
.subscribe(data => {
|
|
|
|
//console.log("得到单位信息" + key, data);
|
|
|
|
if (onSuccess) {
|
|
|
|
onSuccess(key, JSON.parse((data as any).result));
|
|
|
|
}
|
|
|
|
}, error => {
|
|
|
|
console.error("From serve" + key + "===" + error);
|
|
|
|
if (onError) {
|
|
|
|
onError(key, error);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//保存单位信息
|
|
|
|
saveInstitutionData(institutionData: any, key: string = 'test', institutionID: number = 1, onSuccess?: (key: string,
|
|
|
|
result: string
|
|
|
|
) => void, onError?: (key: string, error: string) => void) {
|
|
|
|
let l_data = classToPlain(institutionData);
|
|
|
|
|
|
|
|
console.log("保存单位", l_data);
|
|
|
|
this.buildingBISrv.postBuildingBasicInfos(key, institutionID, l_data)
|
|
|
|
.subscribe(data => {
|
|
|
|
console.log("保存单位成功:" + key, l_data);
|
|
|
|
if (onSuccess) {
|
|
|
|
onSuccess(key, data.result);
|
|
|
|
}
|
|
|
|
if (key != "InsList") {
|
|
|
|
PlanComponent.instance.openSnackBar("保存单位成功");
|
|
|
|
// alert("保存单位成功");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//暂时没有失败的回调 onError
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//保存单位列表
|
|
|
|
saveInstitutionListData(institutionList: InsitutionDataSimple[], onSucess?: () => void) {
|
|
|
|
// console.log("======保存单位");
|
|
|
|
|
|
|
|
ServeManager.instance.saveInstitutionData(institutionList, "InsList", -1, () => {
|
|
|
|
//alert("保存单位列表成功");
|
|
|
|
if (onSucess) {
|
|
|
|
onSucess();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region 态势标会
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取态势标会信息
|
|
|
|
* @param institutionID
|
|
|
|
* @param onSuccess
|
|
|
|
* @param onError
|
|
|
|
*/
|
|
|
|
getMarkData(institutionID: string,
|
|
|
|
onSuccess?: (institutionID: string, data: string) => void,
|
|
|
|
onError?: (institutionID: string, error: any) => void) {
|
|
|
|
|
|
|
|
this.buildingBISrv.getMarkData(institutionID)
|
|
|
|
.subscribe(data => {
|
|
|
|
// ModeManager.log(data);
|
|
|
|
if (onSuccess) {
|
|
|
|
onSuccess(institutionID, JSON.parse((data as any).result));
|
|
|
|
}
|
|
|
|
}, (error) => {
|
|
|
|
if (error instanceof HttpErrorResponse) {
|
|
|
|
if (error.status === 404) {
|
|
|
|
console.log("没有标绘数据,新建:" + error.error);
|
|
|
|
if (ModeManager.currentMode == ModeType.Edit) {
|
|
|
|
onSuccess(institutionID, null); //data 为null ,表示新建
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PlanComponent.instance.openSnackBar("暂无数据");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (onError) {
|
|
|
|
onError(institutionID, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (onError) {
|
|
|
|
onError(institutionID, error);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 保存态势标会信息
|
|
|
|
*/
|
|
|
|
saveMarkData(allMarkPlanData: AllMarkPlanData,
|
|
|
|
onSuccess?: (key: string, result: string) => void,
|
|
|
|
onError?: (key: string, error: string) => void) {
|
|
|
|
|
|
|
|
if (allMarkPlanData == null) {
|
|
|
|
console.error("标绘信息为空,无法保存");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let data = classToPlain(allMarkPlanData);
|
|
|
|
this.buildingBISrv.postMarkData(allMarkPlanData.institutionID, data)
|
|
|
|
.subscribe(data => {
|
|
|
|
ModeManager.log("保存标绘信息成功:" + allMarkPlanData.institutionID);
|
|
|
|
if (onSuccess) {
|
|
|
|
onSuccess(allMarkPlanData.institutionID, data.result);
|
|
|
|
}
|
|
|
|
//PlanComponent.instance.openSnackBar("保存标绘信息成功");
|
|
|
|
// alert("保存标绘信息成功");
|
|
|
|
PlanComponent.instance.openSnackBar("保存成功");
|
|
|
|
|
|
|
|
//暂时没有失败的回调 onError
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#region 文件上传
|
|
|
|
/**
|
|
|
|
* 队列性,批量上传
|
|
|
|
* @param index
|
|
|
|
* @param files
|
|
|
|
* @param resPath_out
|
|
|
|
* @param onOneSuccess
|
|
|
|
* @param onEnd
|
|
|
|
*/
|
|
|
|
uploadFile(index: number, files: File[], resPath_out, onOneSuccess: (name: string, path: string, file: File) => void, onEnd: () => void) {
|
|
|
|
if (index < files.length) {
|
|
|
|
console.log("上传文件", files[index].name);
|
|
|
|
ServeManager.instance.openFileSelect(files[index], resPath_out, (name: string, path: string, file: File) => {
|
|
|
|
onOneSuccess(name, path, file);
|
|
|
|
this.uploadFile(index + 1, files, resPath_out, onOneSuccess, onEnd);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
onEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//设置文件路径并上传
|
|
|
|
openFileSelect(file: File, extensionPath: string, onSuccess?: (name: string, path: string, file: File) => void) {
|
|
|
|
this.postFilePath = extensionPath;
|
|
|
|
|
|
|
|
this.onPostFileSuccess = onSuccess;
|
|
|
|
|
|
|
|
let fileSize = file.size || null //上传文件的总大小
|
|
|
|
let shardSize = 5 * 1024 * 1024 //5MB 超过5MB要分块上传
|
|
|
|
if (fileSize >= shardSize) // 超过5MB要分块上传
|
|
|
|
{
|
|
|
|
this.postFileByMul(file);
|
|
|
|
}
|
|
|
|
else //普通上传
|
|
|
|
{
|
|
|
|
this.postFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//上传成功
|
|
|
|
onPostFileSuccess?: (name: string, path: string, file: File) => void;
|
|
|
|
|
|
|
|
//上传文件
|
|
|
|
async postFile(file: File) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
ServeManager.instance.objectsSrv.postFile(this.postFilePath, file).subscribe(data => {
|
|
|
|
let dataObj = data as any;
|
|
|
|
let fileName = dataObj.fileName;
|
|
|
|
let filePath: string = dataObj.objectName;//此路径不全,前面缺少ObjectsService.baseUrl,这部分不可序列化,要在运行时拼接
|
|
|
|
//console.log("上传完成,地址为", filePath);
|
|
|
|
filePath = filePath.replace(fileName, "");//去掉结尾的文件名
|
|
|
|
//filePath = ObjectsService.baseUrl + filePath;
|
|
|
|
ServeManager.instance.onGetPostFileResult(fileName, filePath, file);
|
|
|
|
resolve('success')
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 上传文件的结果
|
|
|
|
* @param data
|
|
|
|
* @param file
|
|
|
|
*/
|
|
|
|
onGetPostFileResult(fileName: string, filePath: string, file: File) {
|
|
|
|
if (ServeManager.instance.onPostFileSuccess) {
|
|
|
|
ServeManager.instance.onPostFileSuccess(fileName, filePath, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 分块上传
|
|
|
|
* @param file
|
|
|
|
*/
|
|
|
|
postFileByMul(file: File) {
|
|
|
|
ServeManager.instance.objectsSrv.postFile_MultipartUpload(this.postFilePath, file).then((value) => {
|
|
|
|
let dataObj = value as any;
|
|
|
|
console.log("分块上传完成", dataObj.filePath);
|
|
|
|
dataObj.filePath = dataObj.filePath.replace(dataObj.name, ""); //去掉结尾的文件名
|
|
|
|
ServeManager.instance.onGetPostFileResult(dataObj.fileName, dataObj.filePath, file);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#region 本地json 加载
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 加载Json
|
|
|
|
* @param path 例如: "assets/configs/markplan-1.json"
|
|
|
|
* @param onSuccess
|
|
|
|
*/
|
|
|
|
loadJson(path: string, onSuccess: (data) => void) {
|
|
|
|
this.buildingBISrv.http.get(path)
|
|
|
|
.subscribe(data => {
|
|
|
|
console.log("loadJson", data);
|
|
|
|
onSuccess(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|