Bläddra i källkod

feat:没农场接口

wangsisi 3 veckor sedan
förälder
incheckning
7ba9bddc45
2 ändrade filer med 139 tillägg och 50 borttagningar
  1. 5 0
      src/api/modules/report.js
  2. 134 50
      src/views/old_mini/agri_file/pages/diagnosisReport.vue

+ 5 - 0
src/api/modules/report.js

@@ -26,6 +26,11 @@ module.exports = {
         url: config.base_new_url + "report/generate_6module",
         type: "get",
     },
+    // 无农场:按点位生成 / 查询六模块初始化报告(point_text + crop)
+    report6Latlon: {
+        url: config.base_new_url + "report/report6_latlon",
+        type: "get",
+    },
     // 添加执行图片并完成任务
     addExecuteImgAndComplete: {
         url: config.base_dev_url + "v2/z_farm_work_record/addExecuteImgAndComplete",

+ 134 - 50
src/views/old_mini/agri_file/pages/diagnosisReport.vue

@@ -84,12 +84,14 @@
             @switchGrowing="handleSwitchGrowing"
         />
         <editPlantPointPopup ref="editPlantPointPopupRef" @confirm="handlePlantPointConfirm" />
+        <!-- 暂无农场时不弹「完善农场信息」,逻辑保留备用
         <complete-farm-info-popup
             v-model:show="showCompleteFarmPopup"
             :image="completeFarmPopupImage"
             highlight-text="种植全析报告"
             @confirm="goCompleteFarmInfo"
         />
+        -->
         <restore-planting-popup />
     </div>
 </template>
@@ -97,32 +99,36 @@
 <script setup>
 import { computed, onActivated, onDeactivated, onBeforeUnmount, ref } from "vue";
 import { useRouter } from "vue-router";
-import { useStore } from "vuex";
+// import { useStore } from "vuex";
 import { ElMessage } from "element-plus";
 import { Plus, Edit } from "@element-plus/icons-vue";
 import adjustPhenologyPopup from "../components/adjustPhenologyPopup.vue";
 import restingDatePopup from "../components/restingDatePopup.vue";
 import editPlantPointPopup from "../components/editPlantPointPopup.vue";
-import completeFarmInfoPopup from "@/components/popup/completeFarmInfoPopup.vue";
+// import completeFarmInfoPopup from "@/components/popup/completeFarmInfoPopup.vue";
 import tab1Icon from "@/assets/img/report/tab-1.png";
 import tab2Icon from "@/assets/img/report/tab-2.png";
 import tab3Icon from "@/assets/img/report/tab-3.png";
 import tab4Icon from "@/assets/img/report/tab-4.png";
 import tab5Icon from "@/assets/img/report/tab-5.png";
-import completeFarmPopupImage from "@/assets/img/common/empty-bg-3.png";
+// import completeFarmPopupImage from "@/assets/img/common/empty-bg-3.png";
 import restorePlantingPopup from "@/components/popup/restorePlantingPopup.vue";
 
 // ---------------------------------------------------------------------------
 // Props / Emits — 无
 // ---------------------------------------------------------------------------
 const router = useRouter();
-const store = useStore();
+// const store = useStore();
 
 // ---------------------------------------------------------------------------
 // 常量
 // ---------------------------------------------------------------------------
 /** 报告未生成时轮询间隔(ms) */
-const REPORT_POLL_INTERVAL = 3000;
+const REPORT_POLL_INTERVAL = 6000;
+/** 无农场时默认作物编码:水稻 */
+const NO_FARM_CROP = "PAD";
+/** 无定位时的默认点位(与录入页一致) */
+const DEFAULT_POINT = "POINT(113.6142086995688 23.585836479509055)";
 
 /** module1_basic 字段标签 → 基本信息展示项(labels 含接口原文别名) */
 const BASE_FIELD_META = [
@@ -166,16 +172,17 @@ const activeReportTab = ref("weather");
 const adjustPhenologyPopupRef = ref(null);
 const restingDatePopupRef = ref(null);
 const editPlantPointPopupRef = ref(null);
-const showCompleteFarmPopup = ref(false); // 无农场时展示「完善农场信息」
+// const showCompleteFarmPopup = ref(false); // 无农场时展示「完善农场信息」
 let pollTimer = null;
+/** 当前轮询方式:farm | latlon */
+let pollMode = null;
+let pollParams = null;
 
 // ---------------------------------------------------------------------------
 // 计算属性
 // ---------------------------------------------------------------------------
 const pageLoading = computed(() => loading.value || reportGenerating.value);
-const loadingText = computed(() =>
-    reportGenerating.value ? "报告正在生成中..." : "加载中..."
-);
+const loadingText = "报告生成中,请稍后...";
 
 const activeReportTabMeta = computed(
     () => reportTabs.find((tab) => tab.key === activeReportTab.value) || reportTabs[0]
@@ -190,9 +197,9 @@ const activeReportHtml = computed(() => {
 // ---------------------------------------------------------------------------
 // 工具函数
 // ---------------------------------------------------------------------------
-function getMiniUserId() {
-    return store.state.home.miniUserId || localStorage.getItem("MINI_USER_ID");
-}
+// function getMiniUserId() {
+//     return store.state.home.miniUserId || localStorage.getItem("MINI_USER_ID");
+// }
 
 function getSelectedFarm() {
     try {
@@ -202,6 +209,24 @@ function getSelectedFarm() {
     }
 }
 
+function hasFarmZone(farm = {}) {
+    const farmId = farm.farm_id ?? farm.id;
+    const zoneId = farm.zone_id ?? farm.zoneId;
+    return farmId != null && farmId !== "" && zoneId != null && zoneId !== "";
+}
+
+/** 无农场报告用点位:优先用户定位 */
+function getPointText() {
+    return localStorage.getItem("MINI_USER_LOCATION_POINT") || DEFAULT_POINT;
+}
+
+/** report6_latlon:有 records[0] 即视为报告已生成 */
+function pickLatlonReport(res) {
+    const records = res?.data?.records;
+    if (Array.isArray(records) && records.length) return records[0];
+    return null;
+}
+
 /** 解析 module1_basic:兼容 `**标签**:` 与纯文本 `标签:`(内容可跨行) */
 function parseBasicSections(markdown = "") {
     const text = String(markdown).replace(/\r\n/g, "\n").trim();
@@ -353,39 +378,47 @@ function resetReportView(farm = {}) {
 // ---------------------------------------------------------------------------
 // 业务逻辑:无农场弹窗 / 加载报告
 // ---------------------------------------------------------------------------
-/** 无果园信息时展示「完善农场信息」弹窗 */
-async function checkCompleteFarmPopup() {
-    const id = getMiniUserId();
-    if (!id) {
-        showCompleteFarmPopup.value = false;
-        return;
-    }
-    try {
-        const res = await VE_API.questionnaire.getFarms({ id });
-        const list = Array.isArray(res?.data) ? res.data : [];
-        showCompleteFarmPopup.value = list.length === 0;
-    } catch (e) {
-        console.warn("[diagnosisReport] getFarms failed", e);
-        showCompleteFarmPopup.value = false;
-    }
-}
+/** 无果园信息时展示「完善农场信息」弹窗(暂不启用) */
+// async function checkCompleteFarmPopup() {
+//     const id = getMiniUserId();
+//     if (!id) {
+//         showCompleteFarmPopup.value = false;
+//         return;
+//     }
+//     try {
+//         const res = await VE_API.questionnaire.getFarms({ id });
+//         const list = Array.isArray(res?.data) ? res.data : [];
+//         showCompleteFarmPopup.value = list.length === 0;
+//     } catch (e) {
+//         console.warn("[diagnosisReport] getFarms failed", e);
+//         showCompleteFarmPopup.value = false;
+//     }
+// }
 
 function stopReportPolling() {
     if (pollTimer) {
         clearInterval(pollTimer);
         pollTimer = null;
     }
+    pollMode = null;
+    pollParams = null;
 }
 
-/** 报告未就绪时保持 loading,并定时重试 check */
-function startReportPolling(farmId, zoneId) {
+/** 报告未就绪时保持 loading,并定时重试 */
+function startReportPolling(mode, params) {
     stopReportPolling();
+    pollMode = mode;
+    pollParams = params;
     pollTimer = setInterval(() => {
-        pollDiagnosisReport(farmId, zoneId);
+        if (pollMode === "farm") {
+            pollFarmReport(pollParams);
+        } else if (pollMode === "latlon") {
+            pollLatlonReport(pollParams);
+        }
     }, REPORT_POLL_INTERVAL);
 }
 
-async function pollDiagnosisReport(farmId, zoneId) {
+async function pollFarmReport({ farmId, zoneId }) {
     try {
         const raw = await VE_API.questionnaire.checkReportGenerated({
             farm_id: farmId,
@@ -399,22 +432,62 @@ async function pollDiagnosisReport(farmId, zoneId) {
             applyReportData(report, getSelectedFarm());
         }
     } catch (error) {
-        console.warn("[diagnosisReport] poll failed", error);
+        console.warn("[diagnosisReport] farm poll failed", error);
     }
 }
 
-async function fetchDiagnosisReport() {
-    const farm = getSelectedFarm();
-    const farmId = farm.farm_id ?? farm.id;
-    const zoneId = farm.zone_id ?? farm.zoneId;
-    stopReportPolling();
-    reportGenerating.value = false;
+async function pollLatlonReport({ pointText, crop }) {
+    try {
+        const raw = await VE_API.report.report6Latlon({
+            point_text: pointText,
+            crop,
+        });
+        const res = Array.isArray(raw) ? raw[0] : raw;
+        const report = pickLatlonReport(res);
+        if (res?.code === 200 && report) {
+            stopReportPolling();
+            reportGenerating.value = false;
+            applyReportData(report, {});
+        }
+    } catch (error) {
+        console.warn("[diagnosisReport] latlon poll failed", error);
+    }
+}
 
-    if (farmId == null || farmId === "" || zoneId == null || zoneId === "") {
-        resetReportView(farm);
-        // ElMessage.warning("请先选择农场与分区");
-        return;
+/** 无农场:按定位点 + 水稻拉取 / 轮询六模块报告 */
+async function fetchLatlonReport() {
+    const pointText = getPointText();
+    const crop = NO_FARM_CROP;
+    const params = { pointText, crop };
+
+    loading.value = true;
+    try {
+        const raw = await VE_API.report.report6Latlon({
+            point_text: pointText,
+            crop,
+        });
+        const res = Array.isArray(raw) ? raw[0] : raw;
+        const report = pickLatlonReport(res);
+        if (res?.code === 200 && report) {
+            applyReportData(report, {});
+            return;
+        }
+        resetReportView({});
+        reportGenerating.value = true;
+        startReportPolling("latlon", params);
+    } catch (error) {
+        console.error("fetchLatlonReport failed", error);
+        resetReportView({});
+        reportGenerating.value = false;
+        ElMessage.error("报告加载失败");
+    } finally {
+        loading.value = false;
     }
+}
+
+async function fetchFarmReport(farm) {
+    const farmId = farm.farm_id ?? farm.id;
+    const zoneId = farm.zone_id ?? farm.zoneId;
 
     loading.value = true;
     try {
@@ -428,12 +501,11 @@ async function fetchDiagnosisReport() {
             applyReportData(report, farm);
             return;
         }
-        // 报告尚未生成:保持页面 loading,并轮询直到有数据
         resetReportView(farm);
         reportGenerating.value = true;
-        startReportPolling(farmId, zoneId);
+        startReportPolling("farm", { farmId, zoneId });
     } catch (error) {
-        console.error("fetchDiagnosisReport failed", error);
+        console.error("fetchFarmReport failed", error);
         resetReportView(farm);
         reportGenerating.value = false;
         ElMessage.error("报告加载失败");
@@ -442,6 +514,18 @@ async function fetchDiagnosisReport() {
     }
 }
 
+async function fetchDiagnosisReport() {
+    const farm = getSelectedFarm();
+    stopReportPolling();
+    reportGenerating.value = false;
+
+    if (!hasFarmZone(farm)) {
+        await fetchLatlonReport();
+        return;
+    }
+    await fetchFarmReport(farm);
+}
+
 // ---------------------------------------------------------------------------
 // 事件处理 / 生命周期
 // ---------------------------------------------------------------------------
@@ -477,13 +561,13 @@ const handlePlantPointConfirm = (payload) => {
     console.log("plant point confirm:", payload);
 };
 
-const goCompleteFarmInfo = () => {
-    router.push("/entry_information");
-};
+// const goCompleteFarmInfo = () => {
+//     router.push("/entry_information");
+// };
 
 // tabbar 切换时组件被 keep-alive 缓存,需在激活时重新拉报告 / 校验农场
 onActivated(() => {
-    checkCompleteFarmPopup();
+    // checkCompleteFarmPopup(); // 暂无农场时不弹「完善农场信息」
     fetchDiagnosisReport();
 });