guonan
2025-07-03 ca74058a77d7c9000a485502a2b53fbef5807ef5
src/api/trApi.js
@@ -1,18 +1,18 @@
import instance from "./requestTR.js";
// 获取区域数据
export async function getData(params = {}) {
export async function getRegionData(params = {}) {
  try {
    const defaultParams = {
      id: undefined,
      name: undefined,
      type: undefined,
      pageNum: 1,
      pageSize: 100000
      id: undefined,
      name: undefined,
      type: undefined,
      pageNum: 1,
      pageSize: 100000,
    };
    const finalParams = { ...defaultParams, ...params };
    const response = await instance.get('/region/selectPage', {
      params: finalParams
    const response = await instance.get("/region/selectPage", {
      params: finalParams,
    });
    return response.data;
  } catch (error) {
@@ -24,8 +24,21 @@
// 获取方案列表
export async function getSimData() {
  try {
    const res = await instance.get("/simu/selectPage");
    return res.data; // 返回实际数据(通常 res.data 才是接口返回的内容)
    const res = await instance.get("/simu/selectPage", {
      params: { pageSize: 100 },
    });
    return 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;
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error; // 抛出错误,让调用方可以捕获
@@ -53,8 +66,129 @@
    const res = await instance.delete(`/simu/deleteByIds?ids=${ids}`);
    return res.data;
  } catch (error) {
    console.error('Error deleting simulation data:', 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;
  }
}
// 实时模拟的结果
export async function getSimresult(ids) {
  try {
    const res = await instance.get(`/simu/results?id=${ids}`);
    return res.data;
  } catch (error) {
    console.error("Error deleting simulation data:", error);
    throw error;
  }
}
// 结束实时模拟
export async function stopSim(ids) {
  try {
    const res = await instance.get(`/simu/stop?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,
      watersMaxHeight: jsonData.waters.maxHeight,
      watersMinHeight: jsonData.waters.minHeight,
    };
  } 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 数据
//     // console.log(jsonData, "jsonjsonjsonjson");
//     return parseWaterSimulationData(jsonData); // 调用解析函数
//   } catch (error) {
//     console.error("请求或解析数据时出错:", error);
//     return null;
//   }
// }
export async function fetchWaterSimulationData(serviceInfo, timestamp = null) {
  try {
    // 根据是否提供时间戳来决定文件名
    let fileName = timestamp ? timestamp : 'layer.json';
    const url = `/simu/${serviceInfo}/${fileName}`;
    const response = await fetch(url); // 发起请求
    // console.log(url,'aaaaaaaaa')
    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;
  }
}
// 获取水位水深
export async function getFlowRate(data) {
  // console.log(data,'发送的数据!');
  try {
    const res = await instance.get("/simu/position", {
      params: data
    });
    return res.data; // 返回实际数据(通常 res.data 才是接口返回的内容)
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error; // 抛出错误,让调用方可以捕获
  }
}
// **************************************************************************************************************