guonan
2025-05-20 f1fbe8049ba01186f033037e6ae36d51915c7418
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import instance from "./requestTR.js";
 
// 获取区域数据
export async function getRegionData(params = {}) {
  try {
    const defaultParams = {
      id: undefined,
      name: undefined,
      type: undefined,
      pageNum: 1,
      pageSize: 100000,
    };
    const finalParams = { ...defaultParams, ...params };
    const response = await instance.get("/region/selectPage", {
      params: finalParams,
    });
    return response.data;
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error;
  }
}
 
// 获取方案列表
export async function getSimData() {
  try {
    const res = await instance.get("/simu/selectPage");
    return res.data; // 返回实际数据(通常 res.data 才是接口返回的内容)
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error; // 抛出错误,让调用方可以捕获
  }
}
 
// 根据方案id获取方案列表
export async function getSimDataById(id) {
  try {
    const res = await instance.get(`/simu/selectPage?id=${id}`);
    return res.data; // 返回实际数据(通常 res.data 才是接口返回的内容)
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error; // 抛出错误,让调用方可以捕获
  }
}
 
// 新建仿真方案
export async function createSimData(simData) {
  try {
    const res = await instance.post("/simu/insert", simData, {
      headers: {
        "Content-Type": "application/json",
      },
    });
    return res;
  } catch (error) {
    console.error("Error creating simulation data:", error);
    throw error; // 抛出错误,让调用方可以捕获
  }
}
 
// 删除仿真方案
export async function deleteSimData(ids) {
  try {
    const res = await instance.delete(`/simu/deleteByIds?ids=${ids}`);
    return res.data;
  } catch (error) {
    console.error("Error deleting simulation data:", error);
    throw error;
  }
}
 
// 开始模拟
export async function getSimStart(ids) {
  try {
    const res = await instance.get(`/simu/start?id=${ids}`);
    return res.data;
  } catch (error) {
    console.error("Error deleting simulation data:", error);
    throw error;
  }
}
// **************************************************************************************************************
// 解析json获取泥石流参数
export function parseWaterSimulationData(jsonData) {
  try {
    const startTime = jsonData.duration.start; // 直接使用原始时间
    const endTime = jsonData.duration.end; // 直接使用原始时间
    const extension = jsonData.extension;
    const terrainSizes = jsonData.terrain.size; // 分辨率
    const waterTimestamps = jsonData.waters.data.map((timestamp) => {
      return timestamp; // 不进行时间格式化
    });
    return {
      startTime: startTime, // 原始时间
      endTime: endTime, // 原始时间
      extension: {
        maxHeight: extension.maxHeight,
        minHeight: extension.minHeight,
        maxX: extension.maxx,
        maxY: extension.maxy,
        minX: extension.minx,
        minY: extension.miny,
      },
      terrainSizes: terrainSizes,
      flowUrl: jsonData.flowUrl,
      waterUrl: jsonData.waterUrl,
      version: jsonData.version,
      waterTimestamps: waterTimestamps, // 原始时间数组
    };
  } catch (error) {
    console.error("解析水模拟数据时出错:", error);
    return null;
  }
}
 
// 通过接口去请求json,将请求的json解析获取泥石流参数
export async function fetchWaterSimulationData(serviceInfo) {
  try {
    const response = await fetch(`/simu/${serviceInfo}/layer.json`); // 发起请求
    // const response = await fetch(`/simu/c2h1dc/layer.json`); // 发起请求
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const jsonData = await response.json(); // 解析 JSON 数据
    return parseWaterSimulationData(jsonData); // 调用解析函数
  } catch (error) {
    console.error("请求或解析数据时出错:", error);
    return null;
  }
}
// **************************************************************************************************************