diff --git a/static/js/afterbody.js b/static/js/afterbody.js index 4fb2838..3283c2f 100644 --- a/static/js/afterbody.js +++ b/static/js/afterbody.js @@ -1,279 +1,125 @@ -//公式相关 +const WeatherReport = (function () { + const BASE_WIND_COUNT = 10; + const SPOT_CHECK_COUNT = 5; -const BASE_WIND_COUNT = 10; -const SPOT_CHECK_COUNT = 5; - -// 展开或者关闭公式 -function toggleFormula(element) { - const content = element.nextElementSibling; - content.style.display = content.style.display === "none" ? "block" : "none"; - element.textContent = element.textContent.includes("▶") - ? element.textContent.replace("▶", "▼") - : element.textContent.replace("▼", "▶"); -} - -// 加载时运行 -document.addEventListener('DOMContentLoaded', function() { - renderMathInElement(document.body, { - delimiters: [ - {left: "$$", right: "$$", display: true}, - {left: "$", right: "$", display: false} - ], - throwOnError: false - }); - - // 确保数据已加载 - if (window.weatherData) { - const versionElement = document.querySelector('.version'); - const dbVersion = window.weatherData.version || 'default'; - - // 更新页面显示 - if (versionElement) { - versionElement.textContent = `版本号: ${dbVersion}`; - } - - downloadpdf(window.weatherData); - } else { - console.error("错误:window.weatherData 未定义"); - } -}); - -// 下载按钮 -function downloadpdf(data) { - - const areaTypeMap = { + const AREA_TYPE_MAP = { urban: "城市", rural: "农村", }; - let htmlString = ` - + const SUITABILITY_MAP = { + a: "不利于污染物的扩散和稀释,适宜于进行无组织排放监测", + b: "较不利于污染物的扩散和稀释,较适宜于进行无组织排放监测", + c: "有利于污染物的扩散和稀释,较不适宜于进行无组织排放监测", + d: "很有利于污染物的扩散和稀释,不适宜于进行无组织排放监测", + wind0: "静风", + overall0: "取消监测或改期", + }; + + function formatDate(dateStr) { + const d = new Date(dateStr); + const y = d.getFullYear(); + const m = String(d.getMonth() + 1).padStart(2, "0"); + const day = String(d.getDate()).padStart(2, "0"); + return `${y}-${m}-${day}`; + } + + function getSuitabilityText(value, type) { + const normalized = String(value).toLowerCase(); + if (type === "wind" && normalized === "0") return SUITABILITY_MAP.wind0; + if (type === "overall" && normalized === "0") return SUITABILITY_MAP.overall0; + return SUITABILITY_MAP[normalized] || value; + } + + function formatTime(hours, min) { + return `${hours}:${String(min).padStart(2, "0")}`; + } + + function joinWithComma(arr, start, end) { + return arr.slice(start, end).map((v) => v ?? "").join(","); + } + + function buildReportHTML(data) { + const staticPrefix = window.staticPrefix || '/static'; + const timeStr = `${formatDate(data.date)} ${formatTime(data.hours, data.min)}`; + const version = data.version || "unknown"; + + let html = ` 总体适宜度测量报表 - +

总体适宜度测量报表

- -
-
-
文件编号:
-
${data.id}
-
- -
-
检测类型:
-
${data.inspectiontype}
-
- -
-
委托单号:
-
${data.assignmentnumber}
-
- -
-
项目名称:
-
${data.title}
-
- -
-
经纬度:
-
E ${data.longitude}, N ${data.latitude}
-
- -
-
测试时间:
-
${formatDateToYYYYMMDD(data.date)} ${data.hours}:${data.min}
-
-
- -
- -
-
-
日期序数:
-
${data.daySinceJanFirst}
-
-
-
太阳倾角:
-
${data.solarDeclination}°
-
-
-
太阳高度角:
-
${data.sunAltitude}°
-
-
-
总云量:
-
${data.overallCloudiness}
-
-
-
低云量:
-
${data.lowCloudiness}
-
-
-
辐射等级:
-
${data.solarRadiationLevel}
-
-
-
平均风速:
-
${data.averageWindSpeed}m/s
-
-
-
风速适宜度:
-
${data.windSpeedSuitability}
-
-
-
平均风向:
-
${data.averageWindDirection}°
-
-
-
风向标准差:
-
${data.windDirectionStandardDeviation}°
-
-
-
风向适宜度:
-
${data.windDirectionSuitability}
-
-
-
大气稳定度:
-
${data.atmosphericStability}
-
-
-
稳定度适宜度:
-
${data.suitabilityDegree}
-
-
-
总体适宜度:
-
${replaceSuitabilityText(data.overallSuitability, "overall")}
-
-
- -
- - -
-
-
十次风速值:
-
-
${data.windSpeed.slice(0, BASE_WIND_COUNT).join(',')}
+
+
+
文件编号:
+
${data.id}
+
+
+
检测类型:
+
${data.inspectionType || "-"}
+
+
+
委托单号:
+
${data.assignmentNumber || "-"}
+
+
+
项目名称:
+
${data.title || "-"}
+
+
+
经纬度:
+
E ${data.longitude || "-"}, N ${data.latitude || "-"}
+
+
+
测试时间:
+
${timeStr}
+
-
-
-
十次风向值:
-
-
${data.windDirection.slice(0, BASE_WIND_COUNT).join(',')}
-
-
-
- -${data.hasSpotCheckWindSpeed ? ` -
-
-
抽测风速值:
-
-
${data.windSpeed.slice(BASE_WIND_COUNT, BASE_WIND_COUNT + SPOT_CHECK_COUNT).map(val => val ?? '').join(',')}
-
-
-
-
抽测风向值:
-
-
${data.windDirection.slice(BASE_WIND_COUNT, BASE_WIND_COUNT + SPOT_CHECK_COUNT).map(val => val ?? '').join(',')}
-
-
-
-` : ''} +
-
-
是否有抽测风速:${data.hasSpotCheckWindSpeed ? '是' : '否'}
-
+
+
+
日期序数:
+
${data.daySinceJanFirst}
+
+
+
太阳倾角:
+
${Number(data.solarDeclination).toFixed(2)}°
+
+
+
太阳高度角:
+
${Number(data.sunAltitude).toFixed(2)}°
+
+
+
总云量:
+
${data.overallCloudiness}
+
+
+
低云量:
+
${data.lowCloudiness}
+
+
+
辐射等级:
+
${data.solarRadiationLevel}
+
+
+
平均风速:
+
${data.averageWindSpeed}m/s
+
+
+
风速适宜度:
+
${data.windSpeedSuitability}
+
`; -
- -
-` - // 根据条件显示相应的数据 - if (data.hasMeasuredWindSpeed === true) { - htmlString+=` + if (data.hasMeasuredWindSpeed === true) { + html += `
实测风速:
${data.measuredWindSpeed} m/s
@@ -281,13 +127,12 @@ ${data.hasSpotCheckWindSpeed ? `
转换风速:
${data.convertedWindSpeed} m/s
-
- ` - } else { - htmlString+=` +
`; + } else { + html += `
区域类型:
-
${areaTypeMap[data.areaType]}
+
${AREA_TYPE_MAP[data.areaType] || data.areaType || "-"}
测点高度:
@@ -300,92 +145,156 @@ ${data.hasSpotCheckWindSpeed ? `
推算风速:
${data.calculatedWindSpeed} m/s
+
`; + } + + html += ` +
+
平均风向:
+
${data.averageWindDirection}°
- ` +
+
风向标准差:
+
${data.windDirectionStandardDeviation}°
+
+
+
风向适宜度:
+
${data.windDirectionSuitability}
+
+
+
大气稳定度:
+
${data.atmosphericStability}
+
+
+
稳定度适宜度:
+
${data.suitabilityDegree}
+
+
+
总体适宜度:
+
${getSuitabilityText(data.overallSuitability, "overall")}
+
+
+ +
+ +
+
+
十次风速值:
+
+
${joinWithComma(data.windSpeed, 0, BASE_WIND_COUNT)}
+
+
+
+
十次风向值:
+
+
${joinWithComma(data.windDirection, 0, BASE_WIND_COUNT)}
+
+
+
`; + + if (data.hasSpotCheckWindSpeed) { + html += ` +
+
+
抽测风速值:
+
+
${joinWithComma(data.windSpeed, BASE_WIND_COUNT, BASE_WIND_COUNT + SPOT_CHECK_COUNT)}
+
+
+
+
抽测风向值:
+
+
${joinWithComma(data.windDirection, BASE_WIND_COUNT, BASE_WIND_COUNT + SPOT_CHECK_COUNT)}
+
+
+
`; + } + + html += ` +
+
是否有抽测风速:${data.hasSpotCheckWindSpeed ? "是" : "否"}
+
+ +
+ +
+ +`; + + return html; } - htmlString += ` -
-
- - - - ` - const filename = `${data.id}-${data.version || 'unknown'}.pdf` - var opt = { + + function downloadPDF(htmlContent, filename) { + const options = { margin: 10, filename: filename, - image: { - type: 'jpeg', - quality: 0.99 - }, - html2canvas: { - scale: 2 - }, - jsPDF: { - unit: 'mm', - format: 'a4', - orientation: 'portrait' - } + image: { type: "jpeg", quality: 0.99 }, + html2canvas: { scale: 2 }, + jsPDF: { unit: "mm", format: "a4", orientation: "portrait" }, }; - - // 执行下载,使用htmlString作为内容源 - html2pdf().set(opt).from(htmlString).save(); -}; - -// 工具函数 - -// 函数用于将日期格式化为 xxxx-xx-xx 形式 -function formatDateToYYYYMMDD(date) { - const parsed = new Date(date); - const year = parsed.getFullYear(); - const month = (parsed.getMonth() + 1).toString().padStart(2, '0'); - const day = parsed.getDate().toString().padStart(2, '0'); - return year + '-' + month + '-' + day; -} - -//稳定度描述映射 -function replaceSuitabilityText(data, type) { - // 定义映射关系 - const suitabilityMap = { - 'a': '不利于污染物的扩散和稀释,适宜于进行无组织排放监测', - 'b': '较不利于污染物的扩散和稀释,较适宜于进行无组织排放监测', - 'c': '有利于污染物的扩散和稀释,较不适宜于进行无组织排放监测', - 'd': '很有利于污染物的扩散和稀释,不适宜于进行无组织排放监测', - 'wind0': '静风', - 'overall0': '取消监测或改期' - }; - // 将输入转换为小写(或大写)以实现大小写不敏感 - const normalizedData = String(data).toLowerCase(); - - // 处理特殊类型 - if (type === 'wind' && normalizedData === '0') - return suitabilityMap['wind0']; - - if (type === 'overall' && normalizedData === '0') - return suitabilityMap['overall0']; - - // 处理普通类型 - if (type === 'normal' || type === "wind" || type === "overall") { - // 检查映射表中是否存在转换后的小写字段 - if (suitabilityMap[normalizedData]) { - return suitabilityMap[normalizedData]; + html2pdf().set(options).from(htmlContent).save(); } - // 如果找不到,返回原值 - return data; - } - // 默认返回原值 - return data; -} + function init() { + if (typeof window.weatherData === "undefined") { + console.error("错误:window.weatherData 未定义"); + return; + } -// 显示通知 -function showNotification(message, type = 'success') { - const notification = document.getElementById('notification'); - notification.textContent = message; - notification.style.backgroundColor = type === 'success' ? '#4CAF50' : '#f44336'; - notification.style.display = 'block'; - - setTimeout(() => { - notification.style.display = 'none'; - }, 3000); -} + const data = window.weatherData; + console.log("【调试】从后端获取的 weatherData:", data); + console.log("【调试】inspectionType:", data.inspectionType); + console.log("【调试】assignmentNumber:", data.assignmentNumber); + console.log("【调试】title:", data.title); + + const versionEl = document.querySelector(".version"); + if (versionEl) { + versionEl.textContent = `版本号: ${data.version || "default"}`; + } + + const html = buildReportHTML(data); + const filename = `${data.id}-${data.version || "unknown"}.pdf`; + downloadPDF(html, filename); + } + + document.addEventListener("DOMContentLoaded", function () { + if (typeof renderMathInElement === "function") { + renderMathInElement(document.body, { + delimiters: [ + { left: "$$", right: "$$", display: true }, + { left: "$", right: "$", display: false }, + ], + throwOnError: false, + }); + } + init(); + }); + + function toggleFormula(element) { + const content = element.nextElementSibling; + const isHidden = content.style.display === "none"; + content.style.display = isHidden ? "block" : "none"; + element.textContent = element.textContent.includes("▶") + ? element.textContent.replace("▶", "▼") + : element.textContent.replace("▼", "▶"); + } + + function showNotification(message, type = "success") { + const notification = document.getElementById("notification"); + if (!notification) return; + notification.textContent = message; + notification.className = `notification ${type}`; + notification.style.display = "block"; + setTimeout(() => { + notification.style.display = "none"; + }, 3000); + } + + return { + buildReportHTML, + downloadPDF, + init, + toggleFormula, + showNotification, + }; +})();