时间显示异常

This commit is contained in:
YueL1331
2026-01-14 14:42:33 +08:00
parent 9ebfd79414
commit f043983d24
4 changed files with 137 additions and 48 deletions

View File

@ -5,7 +5,7 @@
</main>
<footer class="version-footer">
2.2版本 © 2026 Device Monitor
2.4版本 © 2026 Device Monitor
</footer>
</div>
</template>

View File

@ -291,40 +291,76 @@ const fetchData = async () => {
const isOrphanIoT = (item.source === 'iot_card')
const isWhitelist = !!item.is_whitelist
// 1. 数据时效处理
// === 1. 智能时间解析与格式化 (增强版) ===
let diffDays = 0, diffHours = 0, isToday = false, validTime = false
let timeStr = item.latest_time
// 默认显示原始值,稍后如果解析成功则覆盖它
let displayTime = timeStr
if (timeStr && timeStr !== 'N/A') {
const cleanTime = timeStr.toString().replace(/_/g, '-')
const d = new Date(cleanTime)
if (!isNaN(d.getTime())) {
let d = null;
const str = timeStr.toString().trim();
// A. 尝试匹配标准格式: YYYY-MM-DD HH:mm:ss
const matchStandard = str.match(/^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/);
if (matchStandard) {
d = new Date(
parseInt(matchStandard[1]),
parseInt(matchStandard[2]) - 1,
parseInt(matchStandard[3]),
parseInt(matchStandard[4]),
parseInt(matchStandard[5]),
parseInt(matchStandard[6])
);
} else {
// B. 兜底逻辑:处理下划线或其他格式 (如 2026_01_14)
// 先把下划线全换成横杠
let cleanStr = str.replace(/_/g, '-')
// 如果长度不够(只有日期),补全时间,防止 new Date 解析成 UTC 0点导致时差
if (cleanStr.length <= 10) {
cleanStr += ' 00:00:00'
}
// 处理 T 分隔符 (ISO格式)
cleanStr = cleanStr.replace(' ', 'T')
d = new Date(cleanStr);
}
// C. 如果解析成功,强制重新生成统一的显示字符串
if (d && !isNaN(d.getTime())) {
validTime = true
isToday = d.toDateString() === now.toDateString()
const diff = now - d
diffHours = (diff > 0 ? diff : 0) / (1000 * 3600)
diffDays = diffHours / 24
// 🌟 核心修改点:生成标准显示格式 YYYY-MM-DD HH:mm:ss 🌟
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const dd = String(d.getDate()).padStart(2, '0')
const hh = String(d.getHours()).padStart(2, '0')
const mm = String(d.getMinutes()).padStart(2, '0')
const ss = String(d.getSeconds()).padStart(2, '0')
// 这行代码保证了无论后端发什么,前端都显示得很漂亮
displayTime = `${y}-${m}-${dd} ${hh}:${mm}:${ss}`
}
}
// 2. [恢复旧逻辑] 解析监测数值 (用于排序,虽然不显示但保留逻辑以免报错)
// 2. 解析监测数值 (保留逻辑)
let currentValueNum = 0
if (item.current_value) {
// 尝试提取数字,例如 "1024.5 M" -> 1024.5
const match = String(item.current_value).match(/(\d+(\.\d+)?)/)
if (match) {
currentValueNum = parseFloat(match[0])
}
if (match) currentValueNum = parseFloat(match[0])
}
// 3. 流量与过期计算
let trafficNum = 0
let rawTraffic = item.usedTraffic
if ((rawTraffic === undefined || rawTraffic === null) && item.json_data) {
try {
const j = JSON.parse(item.json_data)
rawTraffic = j.usedTraffic
} catch(e) {}
try { const j = JSON.parse(item.json_data); rawTraffic = j.usedTraffic } catch(e) {}
}
if (rawTraffic) {
trafficNum = parseFloat(rawTraffic)
@ -341,21 +377,21 @@ const fetchData = async () => {
}
}
// 4. 状态判定与权重排序 (融合逻辑)
// 4. 状态判定
let statusColor = '#67C23A', statusLabel = '正常', statusType = 'normal', statusLabelColor = '#fff'
let statusReason = ''
let sortWeight = diffHours // 基础权重为滞后小时数
let sortWeight = diffHours
if (item.is_maintaining) {
statusColor = '#409EFF'; statusLabel = '维修中'; statusType = 'maintenance';
sortWeight = Number.MAX_SAFE_INTEGER;
} else if (!validTime || item.status === 'offline') {
statusLabel = '离线'; statusColor = '#F56C6C'; statusType = 'error';
statusReason = validTime ? '设备离线' : '暂无数据(离线)';
statusReason = validTime ? '设备离线' : '暂无数据';
sortWeight = 80000000;
} else if (diffDays > 7) {
statusLabel = '严重滞后'; statusColor = '#F56C6C'; statusType = 'error';
statusReason = `严重滞后 ${Math.floor(diffDays)}`;
statusReason = `滞后 ${Math.floor(diffDays)}`;
} else if (diffHours > 24) {
statusLabel = '滞后'; statusColor = '#E6A23C'; statusType = 'warning';
statusReason = `滞后 ${Math.floor(diffDays)}`;
@ -365,7 +401,7 @@ const fetchData = async () => {
sortWeight = 500;
} else if (expireWarning) {
statusLabel = '即将过期'; statusColor = '#E6A23C'; statusType = 'warning';
statusReason = `卡片即将过期`;
statusReason = `即将过期`;
sortWeight = 400;
} else if (!isToday) {
statusLabel = '昨日数据'; statusColor = '#FAC858'; statusType = 'slight-warning'; statusLabelColor = '#333';
@ -376,6 +412,7 @@ const fetchData = async () => {
return {
...item,
latest_time: displayTime, // <--- 这里使用了我们格式化好的漂亮时间
is_hidden: isHidden,
isOrphanIoT,
isBound,