// 工具函数 const WeatherUtils = { // 格式化日期时间 formatDatetimeLocal(date) { const offset = date.getTimezoneOffset(); const localDate = new Date(date.getTime() - offset * 60 * 1000); return localDate.toISOString().slice(0, 16); }, // 十六进制转十进制 hexToDecimal(hex) { return parseInt(hex, 16).toString(); }, // 十进制转十六进制(保持6位,不足补0) decimalToHex(decimal) { const hex = parseInt(decimal).toString(16).toUpperCase(); return '0'.repeat(Math.max(0, 6 - hex.length)) + hex; }, // 检查是否为十六进制字符串 isHexString(str) { return /^[0-9A-F]+$/i.test(str); }, // 格式化数字(保留2位小数) formatNumber(value, decimals = 2) { if (value === null || value === undefined) return '-'; return Number(value).toFixed(decimals); }, // 检查设备是否在线 isDeviceOnline(lastUpdate) { return new Date(lastUpdate) > new Date(Date.now() - 5 * 60 * 1000); }, // 初始化日期输入 initializeDateInputs() { const now = new Date(); const startDate = new Date(now.getTime() - 24 * 60 * 60 * 1000); // 过去24小时 const endDate = new Date(now.getTime() + 3 * 60 * 60 * 1000); // 未来3小时 document.getElementById('startDate').value = this.formatDatetimeLocal(startDate); document.getElementById('endDate').value = this.formatDatetimeLocal(endDate); } }; // 导出工具对象 window.WeatherUtils = WeatherUtils;