浏览代码

修复接口调用

yangtaodemon 1 天之前
父节点
当前提交
b913ec5c13
共有 1 个文件被更改,包括 37 次插入38 次删除
  1. 37 38
      src/views/User/acidModel/acidmodelmap.vue

+ 37 - 38
src/views/User/acidModel/acidmodelmap.vue

@@ -232,6 +232,7 @@
 import { reactive, ref, nextTick, onMounted, onUnmounted, computed } from "vue";
 import { ElMessage } from "element-plus";
 import type { FormInstance } from "element-plus";
+import { api8000 } from "@/utils/request";
 
 // 新增:反酸预测相关状态(区分降酸/反酸的显示)
 const showInversionPrediction = ref(false); // 控制反酸预测区域显示
@@ -579,29 +580,29 @@ const getFeatureInfo = async (_latlng: any, point: any): Promise<boolean> => {
   }
 };
 
-//  新增:单独获取当前pH的方法(点击预测后先加载pH)
+// 新增:单独获取当前pH的方法(点击预测后先加载pH)
 const fetchCurrentPH = async () => {
   if (currentPH.value !== null) return true; // 已有pH,直接返回
   if (phLoading.value) return false; // 正在加载,避免重复请求
 
   phLoading.value = true;
   try {
-    const urlParams = new URLSearchParams();
-    urlParams.append('target_lon', currentClickCoords.lng.toString());
-    urlParams.append('target_lat', currentClickCoords.lat.toString());
-    urlParams.append('NO3', defaultParams.NO3.toString());
-    urlParams.append('NH4', defaultParams.NH4.toString());
-
-    // 调用接口获取当前pH(接口会返回nearest_point.ph)
-    const response = await fetch(
-      `http://localhost:8000/api/vector/nearest-with-predictions?${urlParams.toString()}`
-    );
+    // 准备请求参数
+    const params = {
+      target_lon: currentClickCoords.lng.toString(),
+      target_lat: currentClickCoords.lat.toString(),
+      NO3: defaultParams.NO3.toString(),
+      NH4: defaultParams.NH4.toString()
+    };
 
-    if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
-    const data = await response.json();
+    // 使用api8000模块调用API
+    const response = await api8000.get('/api/vector/nearest-with-predictions', { params });
     
-    // 从接口提取当前pH(根据你的接口结构:data.nearest_point.ph)
-    currentPH.value = data.nearest_point?.ph !== undefined ? Number(data.nearest_point.ph) : null;
+    // 从接口提取当前pH
+    currentPH.value = response.data.nearest_point?.ph !== undefined 
+      ? Number(response.data.nearest_point.ph) 
+      : null;
+      
     return currentPH.value !== null; // 返回是否获取成功
   } catch (error) {
     console.error('获取当前pH失败:', error);
@@ -717,42 +718,41 @@ const callPredictionAPI = async (
   predictionResult.value = null;
 
   try {
-    const urlParams = new URLSearchParams();
-    urlParams.append('target_lon', lng.toString());
-    urlParams.append('target_lat', lat.toString());
+    // 构建请求参数对象
+    const requestParams: Record<string, string | number> = {
+      target_lon: lng,
+      target_lat: lat
+    };
 
-    // 明确传递 prediction_type
+    // 添加预测类型参数
     if (currentPredictionType.value === 'reduction') {
-      urlParams.append('prediction_type', 'reduce');
+      requestParams.prediction_type = 'reduce';
     } else if (currentPredictionType.value === 'inversion') {
-      urlParams.append('prediction_type', 'reflux');
+      requestParams.prediction_type = 'reflux';
     }
 
+    // 添加可选参数
     if (params) {
       Object.entries(params).forEach(([key, value]) => {
         if (value !== undefined && value !== null) {
-          urlParams.append(key, value.toString());
+          requestParams[key] = value;
         }
       });
     }
 
-    console.log('调用预测接口,参数:', urlParams.toString());
-
-    const response = await fetch(
-      `http://localhost:8000/api/vector/nearest-with-predictions?${urlParams.toString()}`
-    );
+    console.log('调用预测接口,参数:', requestParams);
 
-    if (!response.ok) {
-      const errorData = await response.json();
-      console.error('预测接口返回错误:', errorData);
-      throw new Error(`HTTP error! status: ${response.status}`);
-    }
+    // 使用api8000调用API
+    const response = await api8000.get('/api/vector/nearest-with-predictions', {
+      params: requestParams
+    });
 
-    const data = await response.json();
-    predictionResult.value = data;
-    console.log('预测结果:', data);
+    predictionResult.value = response.data;
+    console.log('预测结果:', response.data);
 
-    currentPH.value = data.nearest_point?.ph !== undefined ? Number(data.nearest_point.ph) : null;
+    currentPH.value = response.data.nearest_point?.ph !== undefined 
+      ? Number(response.data.nearest_point.ph) 
+      : null;
 
     // 显示预测结果
     showPredictionResult.value = true;
@@ -762,8 +762,7 @@ const callPredictionAPI = async (
   } catch (error) {
     currentPH.value = null;
     console.error('调用预测接口失败:', error);
-    predictionError.value = `预测失败: ${error instanceof Error ? error.message : '未知错误'}`;
-    ElMessage.error('预测请求失败,请检查后端服务是否启动');
+    
   } finally {
     predictionLoading.value = false;
   }