From dbcd692bae985adeb83566bc62c88b68a87ca966 Mon Sep 17 00:00:00 2001 From: wikiboss Date: Thu, 15 Oct 2026 10:51:48 +0800 Subject: [PATCH] =?UTF-8?q?chore(screen):=20=E5=90=8C=E6=AD=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=A4=A7=E5=B1=8F=E6=8C=82=E8=BD=BD=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=B9=B6=E4=B8=8E=E5=90=8E=E7=AB=AFAPI=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E5=AF=B9=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/screen/js/app.js | 48 ++++++++------ public/screen/js/grain_monitor_core.js | 41 +++++++++--- public/screen/js/xishui_app.js | 90 ++++++++++++++++---------- 3 files changed, 116 insertions(+), 63 deletions(-) diff --git a/public/screen/js/app.js b/public/screen/js/app.js index 3dc28d7..9b5a0f2 100644 --- a/public/screen/js/app.js +++ b/public/screen/js/app.js @@ -136,27 +136,35 @@ async function fetchGranaryData() { // 3.1 加载微气候环境工况 async function fetchEnvData() { - try { - const res = await fetch('/api/granary/env?site=center', { cache: 'no-cache' }); - if (!res.ok) return; - const json = await res.json(); - if (json && json.code === 0 && json.data) { - const d = json.data; - STATE.envData = d; - const inpTmI = document.getElementById('input-forecast-tm-i'); - const inpTmO = document.getElementById('input-forecast-tm-o'); - const inpHmI = document.getElementById('input-forecast-hm-i'); - const inpHmO = document.getElementById('input-forecast-hm-o'); - if (inpTmI && d.tm_i !== undefined) inpTmI.value = d.tm_i; - if (inpTmO && d.tm_o !== undefined) inpTmO.value = d.tm_o; - if (inpHmI && d.hm_i !== undefined) inpHmI.value = d.hm_i; - if (inpHmO && d.hm_o !== undefined) inpHmO.value = d.hm_o; - const pill = document.getElementById('forecast-trend-env-pill'); - if (pill) { - pill.textContent = `工况: 仓内 ${d.tm_i}℃ / ${d.hm_i}% | 仓外 ${d.tm_o}℃ / ${d.hm_o}%`; + const endpoints = [ + '/api/index/env?site=center', + 'https://grain.gynhc.com/api/index/env?site=center', + '/api/granary/env?site=center' + ]; + for (const url of endpoints) { + try { + const res = await fetch(url, { cache: 'no-cache', signal: AbortSignal.timeout(2000) }); + if (!res.ok) continue; + const json = await res.json(); + if (json && (json.code === 1 || json.code === 0) && json.data) { + const d = json.data; + STATE.envData = d; + const inpTmI = document.getElementById('input-forecast-tm-i'); + const inpTmO = document.getElementById('input-forecast-tm-o'); + const inpHmI = document.getElementById('input-forecast-hm-i'); + const inpHmO = document.getElementById('input-forecast-hm-o'); + if (inpTmI && d.tm_i !== undefined) inpTmI.value = d.tm_i; + if (inpTmO && d.tm_o !== undefined) inpTmO.value = d.tm_o; + if (inpHmI && d.hm_i !== undefined) inpHmI.value = d.hm_i; + if (inpHmO && d.hm_o !== undefined) inpHmO.value = d.hm_o; + const pill = document.getElementById('forecast-trend-env-pill'); + if (pill) { + pill.textContent = `工况: 仓内 ${d.tm_i}℃ / ${d.hm_i}% | 仓外 ${d.tm_o}℃ / ${d.hm_o}%`; + } + return; } - } - } catch (e) {} + } catch (e) {} + } } // 3.2 渲染全库储粮安全感知与预警走马灯 diff --git a/public/screen/js/grain_monitor_core.js b/public/screen/js/grain_monitor_core.js index 1d5d5bc..9adaeaa 100644 --- a/public/screen/js/grain_monitor_core.js +++ b/public/screen/js/grain_monitor_core.js @@ -1438,20 +1438,45 @@ export async function fetchGranaryForecast(granaryItem, days = [7, 15, 30, 60], if (typeof fetch !== 'undefined') { const query = new URLSearchParams(params).toString(); - // 1. 优先尝试本地代理,规避纯静态环境 CORS - try { - const res = await fetch(`/api/forecast?${query}`, { signal: AbortSignal.timeout(1800) }); - if (res.ok) { + const isBrowser = typeof window !== 'undefined'; + const forecastUrls = isBrowser + ? [`/api/index/forecast?${query}`, `/api/forecast?${query}`, `https://grain.gynhc.com/api/index/forecast?${query}`] + : [`https://grain.gynhc.com/api/index/forecast?${query}`]; + + for (const fUrl of forecastUrls) { + try { + const res = await fetch(fUrl, { signal: AbortSignal.timeout(600) }); + if (!res.ok) continue; const json = await res.json(); if (json && json.code === '0' && json.output?.targets) { apiData = json; + break; + } else if (json && json.code === 1 && json.data && Array.isArray(json.data.list)) { + const matched = json.data.list.find(i => i.day === day); + if (matched) { + apiData = { + output: { + targets: { + water: matched.forecast_water, + wheat_moisture: matched.forecast_water, + impurity: matched.forecast_impurity, + wheat_GWAC: matched.forecast_impurity, + weight: matched.forecast_weight, + wheat_unitweight: matched.forecast_weight, + imperfect: matched.forecast_imperfect, + wheat_unsoundkernels: matched.forecast_imperfect, + taste: matched.forecast_taste, + wheat_tasteevaluation: matched.forecast_taste + } + } + }; + break; + } } - } - } catch { - // 本地代理失败,尝试直连在线算法服务 + } catch {} } - // 2. 本地代理不可用时,直连公网算法接口 + // 2. 后端接口不可用时,直连公网算法接口 if (!apiData) { try { const directUrl = `http://www.probe-cdy-pests.cn:8116/alg/grainquality/forcast?${query}`; diff --git a/public/screen/js/xishui_app.js b/public/screen/js/xishui_app.js index 8b2091e..31ad8e5 100644 --- a/public/screen/js/xishui_app.js +++ b/public/screen/js/xishui_app.js @@ -89,49 +89,69 @@ function loadGranaryData() { // 尝试连接后台接口同步最新配置 if (typeof fetch !== 'undefined') { - fetch('/api/index/granaryList?site=xishui', { cache: 'no-cache', signal: AbortSignal.timeout(2000) }) - .then(res => res.ok ? res.json() : null) - .then(json => { - if (json && json.data && Array.isArray(json.data.list) && json.data.list.length > 0) { - STATE.list = normalizeGranaryList(json); - if (json.data.coords) { - STATE.customCoords = json.data.coords; + const urls = [ + '/api/index/granaryList?site=xishui', + 'https://grain.gynhc.com/api/index/granaryList?site=xishui', + '/api/granary/list?site=xishui' + ]; + (async () => { + for (const u of urls) { + try { + const res = await fetch(u, { cache: 'no-cache', signal: AbortSignal.timeout(2000) }); + if (!res.ok) continue; + const json = await res.json(); + if (json && json.data && Array.isArray(json.data.list) && json.data.list.length > 0) { + STATE.list = normalizeGranaryList(json); + if (json.data.coords) { + STATE.customCoords = json.data.coords; + } + renderKPIs(); + renderHotspots(); + renderMiniGrid(); + renderDepotAlerts(); + return; } - renderKPIs(); - renderHotspots(); - renderMiniGrid(); - renderDepotAlerts(); - } - }) - .catch(() => {}); + } catch (e) {} + } + })(); } } // 3.1 加载习水子库微气候环境工况 function loadEnvData() { if (typeof fetch !== 'undefined') { - fetch('/api/granary/env?site=xishui', { cache: 'no-cache', signal: AbortSignal.timeout(2000) }) - .then(res => res.ok ? res.json() : null) - .then(json => { - if (json && json.code === 0 && json.data) { - const d = json.data; - STATE.envData = d; - const inpTmI = document.getElementById('input-forecast-tm-i'); - const inpTmO = document.getElementById('input-forecast-tm-o'); - const inpHmI = document.getElementById('input-forecast-hm-i'); - const inpHmO = document.getElementById('input-forecast-hm-o'); - if (inpTmI && d.tm_i !== undefined) inpTmI.value = d.tm_i; - if (inpTmO && d.tm_o !== undefined) inpTmO.value = d.tm_o; - if (inpHmI && d.hm_i !== undefined) inpHmI.value = d.hm_i; - if (inpHmO && d.hm_o !== undefined) inpHmO.value = d.hm_o; - const pill = document.getElementById('forecast-trend-env-pill'); - if (pill) { - pill.textContent = `工况: 仓内 ${d.tm_i}℃ / ${d.hm_i}% | 仓外 ${d.tm_o}℃ / ${d.hm_o}%`; + const urls = [ + '/api/index/env?site=xishui', + 'https://grain.gynhc.com/api/index/env?site=xishui', + '/api/granary/env?site=xishui' + ]; + (async () => { + for (const u of urls) { + try { + const res = await fetch(u, { cache: 'no-cache', signal: AbortSignal.timeout(2000) }); + if (!res.ok) continue; + const json = await res.json(); + if (json && (json.code === 1 || json.code === 0) && json.data) { + const d = json.data; + STATE.envData = d; + const inpTmI = document.getElementById('input-forecast-tm-i'); + const inpTmO = document.getElementById('input-forecast-tm-o'); + const inpHmI = document.getElementById('input-forecast-hm-i'); + const inpHmO = document.getElementById('input-forecast-hm-o'); + if (inpTmI && d.tm_i !== undefined) inpTmI.value = d.tm_i; + if (inpTmO && d.tm_o !== undefined) inpTmO.value = d.tm_o; + if (inpHmI && d.hm_i !== undefined) inpHmI.value = d.hm_i; + if (inpHmO && d.hm_o !== undefined) inpHmO.value = d.hm_o; + const pill = document.getElementById('forecast-trend-env-pill'); + if (pill) { + pill.textContent = `工况: 仓内 ${d.tm_i}℃ / ${d.hm_i}% | 仓外 ${d.tm_o}℃ / ${d.hm_o}%`; + } + renderDepotAlerts(); + return; } - renderDepotAlerts(); - } - }) - .catch(() => {}); + } catch (e) {} + } + })(); } }