|
@@ -151,10 +151,11 @@ const router = useRouter();
|
|
|
const REPORT_POLL_INTERVAL = 6000;
|
|
const REPORT_POLL_INTERVAL = 6000;
|
|
|
/** exists=0 时进度条走满的模拟时长 */
|
|
/** exists=0 时进度条走满的模拟时长 */
|
|
|
const PROGRESS_DURATION_MS = 2 * 60 * 1000;
|
|
const PROGRESS_DURATION_MS = 2 * 60 * 1000;
|
|
|
-/** 进度只记这一次;切页回来从上次百分比接着走 */
|
|
|
|
|
|
|
+/** 按农场记进度;切页回来从该农场上次百分比接着走 */
|
|
|
const PROGRESS_STORAGE_KEY = "DIAGNOSIS_REPORT_PROGRESS";
|
|
const PROGRESS_STORAGE_KEY = "DIAGNOSIS_REPORT_PROGRESS";
|
|
|
const PROGRESS_MIN = 10;
|
|
const PROGRESS_MIN = 10;
|
|
|
const PROGRESS_CAP = 99;
|
|
const PROGRESS_CAP = 99;
|
|
|
|
|
+const PROGRESS_NO_FARM_KEY = "nofarm";
|
|
|
/** 无农场时默认作物编码:水稻 */
|
|
/** 无农场时默认作物编码:水稻 */
|
|
|
const NO_FARM_CROP = "PAD";
|
|
const NO_FARM_CROP = "PAD";
|
|
|
/** 无定位时的默认点位(与录入页一致) */
|
|
/** 无定位时的默认点位(与录入页一致) */
|
|
@@ -206,6 +207,8 @@ const editPlantPointPopupRef = ref(null);
|
|
|
// const showCompleteFarmPopup = ref(false); // 无农场时展示「完善农场信息」
|
|
// const showCompleteFarmPopup = ref(false); // 无农场时展示「完善农场信息」
|
|
|
let pollTimer = null;
|
|
let pollTimer = null;
|
|
|
let progressTimer = null;
|
|
let progressTimer = null;
|
|
|
|
|
+/** 当前进度条绑定的农场 key,切页时按它写回缓存 */
|
|
|
|
|
+let progressFarmKey = "";
|
|
|
/** 当前轮询方式:farm | latlon */
|
|
/** 当前轮询方式:farm | latlon */
|
|
|
let pollMode = null;
|
|
let pollMode = null;
|
|
|
let pollParams = null;
|
|
let pollParams = null;
|
|
@@ -450,50 +453,71 @@ function stopProgressTimer() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function readProgressCache() {
|
|
|
|
|
|
|
+/** 有农场用 farm_id;无农场共用 nofarm,避免互相覆盖 */
|
|
|
|
|
+function resolveProgressFarmKey(farm) {
|
|
|
|
|
+ const farmId = farm?.farm_id ?? farm?.id;
|
|
|
|
|
+ if (farmId != null && farmId !== "") return String(farmId);
|
|
|
|
|
+ return PROGRESS_NO_FARM_KEY;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function readProgressStore() {
|
|
|
try {
|
|
try {
|
|
|
- const raw = JSON.parse(localStorage.getItem(PROGRESS_STORAGE_KEY) || "null");
|
|
|
|
|
- const percent = Number(raw?.percent ?? raw);
|
|
|
|
|
- if (!Number.isFinite(percent)) return null;
|
|
|
|
|
- return Math.min(PROGRESS_CAP, Math.max(PROGRESS_MIN, Math.floor(percent)));
|
|
|
|
|
|
|
+ const raw = JSON.parse(localStorage.getItem(PROGRESS_STORAGE_KEY) || "{}");
|
|
|
|
|
+ return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
|
} catch {
|
|
} catch {
|
|
|
- return null;
|
|
|
|
|
|
|
+ return {};
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function writeProgressCache(percent) {
|
|
|
|
|
|
|
+function readProgressCache(farmKey = progressFarmKey) {
|
|
|
|
|
+ if (!farmKey) return null;
|
|
|
|
|
+ const percent = Number(readProgressStore()[farmKey]);
|
|
|
|
|
+ if (!Number.isFinite(percent)) return null;
|
|
|
|
|
+ return Math.min(PROGRESS_CAP, Math.max(PROGRESS_MIN, Math.floor(percent)));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function writeProgressCache(percent, farmKey = progressFarmKey) {
|
|
|
|
|
+ if (!farmKey) return;
|
|
|
const value = Math.min(PROGRESS_CAP, Math.max(PROGRESS_MIN, Math.floor(percent)));
|
|
const value = Math.min(PROGRESS_CAP, Math.max(PROGRESS_MIN, Math.floor(percent)));
|
|
|
- localStorage.setItem(PROGRESS_STORAGE_KEY, JSON.stringify({ percent: value }));
|
|
|
|
|
|
|
+ const store = readProgressStore();
|
|
|
|
|
+ store[farmKey] = value;
|
|
|
|
|
+ localStorage.setItem(PROGRESS_STORAGE_KEY, JSON.stringify(store));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function clearProgressCache() {
|
|
|
|
|
- localStorage.removeItem(PROGRESS_STORAGE_KEY);
|
|
|
|
|
|
|
+function clearProgressCache(farmKey = progressFarmKey) {
|
|
|
|
|
+ if (!farmKey) return;
|
|
|
|
|
+ const store = readProgressStore();
|
|
|
|
|
+ if (!(farmKey in store)) return;
|
|
|
|
|
+ delete store[farmKey];
|
|
|
|
|
+ localStorage.setItem(PROGRESS_STORAGE_KEY, JSON.stringify(store));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** 切页时记下当前进度,回来不要从头走 */
|
|
|
|
|
|
|
+/** 切页时记下当前农场进度,回来不要从头走 */
|
|
|
function saveProgressSnapshot() {
|
|
function saveProgressSnapshot() {
|
|
|
- if (!reportGenerating.value) return;
|
|
|
|
|
- writeProgressCache(progressPercent.value || PROGRESS_MIN);
|
|
|
|
|
|
|
+ if (!reportGenerating.value || !progressFarmKey) return;
|
|
|
|
|
+ writeProgressCache(progressPercent.value || PROGRESS_MIN, progressFarmKey);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** exists=0 才走进度条;从 10% 或已记下的进度接着走到 99 */
|
|
|
|
|
-function beginProgress() {
|
|
|
|
|
|
|
+/** exists=0 才走进度条;按农场从 10% 或已记下的进度接着走到 99 */
|
|
|
|
|
+function beginProgress(farm) {
|
|
|
|
|
+ progressFarmKey = resolveProgressFarmKey(farm);
|
|
|
reportGenerating.value = true;
|
|
reportGenerating.value = true;
|
|
|
if (progressTimer) return;
|
|
if (progressTimer) return;
|
|
|
- const startPercent = readProgressCache() ?? PROGRESS_MIN;
|
|
|
|
|
|
|
+ const startPercent = readProgressCache(progressFarmKey) ?? PROGRESS_MIN;
|
|
|
progressPercent.value = startPercent;
|
|
progressPercent.value = startPercent;
|
|
|
- writeProgressCache(startPercent);
|
|
|
|
|
|
|
+ writeProgressCache(startPercent, progressFarmKey);
|
|
|
const span = PROGRESS_CAP - startPercent;
|
|
const span = PROGRESS_CAP - startPercent;
|
|
|
const remainMs = ((PROGRESS_CAP - PROGRESS_MIN) > 0
|
|
const remainMs = ((PROGRESS_CAP - PROGRESS_MIN) > 0
|
|
|
? span / (PROGRESS_CAP - PROGRESS_MIN)
|
|
? span / (PROGRESS_CAP - PROGRESS_MIN)
|
|
|
: 0) * PROGRESS_DURATION_MS;
|
|
: 0) * PROGRESS_DURATION_MS;
|
|
|
if (remainMs <= 0) return;
|
|
if (remainMs <= 0) return;
|
|
|
|
|
+ const farmKey = progressFarmKey;
|
|
|
const startedAt = Date.now();
|
|
const startedAt = Date.now();
|
|
|
progressTimer = setInterval(() => {
|
|
progressTimer = setInterval(() => {
|
|
|
const ratio = Math.min((Date.now() - startedAt) / remainMs, 1);
|
|
const ratio = Math.min((Date.now() - startedAt) / remainMs, 1);
|
|
|
const next = Math.min(PROGRESS_CAP, Math.floor(startPercent + span * ratio));
|
|
const next = Math.min(PROGRESS_CAP, Math.floor(startPercent + span * ratio));
|
|
|
progressPercent.value = next;
|
|
progressPercent.value = next;
|
|
|
- writeProgressCache(next);
|
|
|
|
|
|
|
+ writeProgressCache(next, farmKey);
|
|
|
if (ratio >= 1) stopProgressTimer();
|
|
if (ratio >= 1) stopProgressTimer();
|
|
|
}, 200);
|
|
}, 200);
|
|
|
}
|
|
}
|
|
@@ -503,8 +527,10 @@ async function revealReport(report, farm, token) {
|
|
|
if (token !== viewToken) return;
|
|
if (token !== viewToken) return;
|
|
|
stopReportPolling();
|
|
stopReportPolling();
|
|
|
const showComplete = reportGenerating.value;
|
|
const showComplete = reportGenerating.value;
|
|
|
|
|
+ const farmKey = progressFarmKey || resolveProgressFarmKey(farm);
|
|
|
stopProgressTimer();
|
|
stopProgressTimer();
|
|
|
- clearProgressCache();
|
|
|
|
|
|
|
+ clearProgressCache(farmKey);
|
|
|
|
|
+ progressFarmKey = "";
|
|
|
if (!showComplete) {
|
|
if (!showComplete) {
|
|
|
reportGenerating.value = false;
|
|
reportGenerating.value = false;
|
|
|
applyReportData(report || {}, farm);
|
|
applyReportData(report || {}, farm);
|
|
@@ -595,7 +621,7 @@ async function fetchLatlonReport(token) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
resetReportView({});
|
|
resetReportView({});
|
|
|
- beginProgress();
|
|
|
|
|
|
|
+ beginProgress({});
|
|
|
startReportPolling("latlon", params, token);
|
|
startReportPolling("latlon", params, token);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
if (token !== viewToken) return;
|
|
if (token !== viewToken) return;
|
|
@@ -628,7 +654,7 @@ async function fetchFarmReport(farm, token) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
resetReportView(farm);
|
|
resetReportView(farm);
|
|
|
- beginProgress();
|
|
|
|
|
|
|
+ beginProgress(farm);
|
|
|
startReportPolling("farm", { farmId, zoneId }, token);
|
|
startReportPolling("farm", { farmId, zoneId }, token);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
if (token !== viewToken) return;
|
|
if (token !== viewToken) return;
|