1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
| class ServiceStatus { constructor() { this.isInitialized = false; this.apiUrl = 'Uptime Kuma 状态页面URL api'; this.statusPageUrl = 'Uptime Kuma 状态页面URL api'; this.config = { statusText: { normal: '服务正常', warning: '部分异常', error: '服务异常', loading: '加载中...', unknown: '未知状态', noApi: '未配置API', apiError: 'API错误', networkError: '网络错误', noService: '无监控服务' } }; this.init(); this.injectStyles(); }
injectStyles() { const style = document.createElement('style'); style.textContent = ` .footer-service-status { display: inline-flex; align-items: center; gap: 4px; margin-left: 8px; color: var(--efu-fontcolor); font-size: 0.75rem; font-weight: 400; white-space: nowrap; text-decoration: none; transition: color 0.3s ease; vertical-align: middle; } .footer-service-status:hover { color: var(--efu-theme); } .service-status-indicator { width: 5px; height: 5px; border-radius: 50%; display: inline-block; transition: all 0.3s ease; flex-shrink: 0; } .service-status-indicator.status-normal { background: #52c41a; box-shadow: 0 0 4px rgba(82, 196, 26, 0.4); } .service-status-indicator.status-warning { background: #faad14; box-shadow: 0 0 4px rgba(250, 173, 20, 0.4); } .service-status-indicator.status-error { background: #ff4d4f; box-shadow: 0 0 4px rgba(255, 77, 79, 0.4); } .service-status-indicator.status-unknown { background: #d9d9d9; box-shadow: 0 0 4px rgba(217, 217, 217, 0.4); } .service-status-indicator.status-loading { background: #1890ff; box-shadow: 0 0 4px rgba(24, 144, 255, 0.4); animation: pulse 1.5s ease-in-out infinite; } @keyframes pulse { 0% { opacity: 1; transform: scale(1); } 50% { opacity: 0.7; transform: scale(1.1); } 100% { opacity: 1; transform: scale(1); } } .service-status-text { font-weight: 400; transition: color 0.3s ease; font-size: 0.75rem; } /* 确保copyright内的元素正确对齐 */ .copyright { display: flex; align-items: center; flex-wrap: wrap; flex-direction: row !important; } /* 覆盖footer-bar-left的column布局,让copyright和服务状态在同一行 */ .footer-bar-left { flex-direction: row !important; align-items: center; flex-wrap: wrap; } @media (max-width: 768px) { .footer-service-status { margin-left: 6px; } .service-status-indicator { width: 4px; height: 4px; } .service-status-text { font-size: 0.7rem; } /* 移动端保持垂直布局 */ .footer-bar-left { flex-direction: column !important; align-items: center; } } `; document.head.appendChild(style); }
init() { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => this.initStatus()); } else { this.initStatus(); }
document.addEventListener('pjax:complete', () => this.initStatus()); }
initStatus() { const statusIndicator = document.getElementById('serviceStatusIndicator'); const statusText = document.getElementById('serviceStatusText'); if (!statusIndicator || !statusText) return;
this.isInitialized = true; this.fetchServiceStatus();
}
async fetchServiceStatus() { try { const servicesData = await this.getServicesData(); if (servicesData && servicesData.length > 0) { this.updateStatus(servicesData); } else { this.updateStatus([]); } } catch (error) { console.error('获取服务状态失败:', error); this.updateStatus([]); } }
async getServicesData() { try { if (!this.statusPageUrl) { throw new Error('Status page URL not configured'); }
const response = await fetch(this.statusPageUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' } });
if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); }
const data = await response.json(); if (!data.heartbeatList || Object.keys(data.heartbeatList).length === 0) { return []; }
const services = Object.keys(data.heartbeatList).map(monitorId => { const heartbeats = data.heartbeatList[monitorId]; const latestHeartbeat = heartbeats[heartbeats.length - 1]; const status = latestHeartbeat ? latestHeartbeat.status : 0; const lastUpdateTime = latestHeartbeat ? latestHeartbeat.time : ''; const uptimeKey = `${monitorId}_24`; const uptime = data.uptimeList && data.uptimeList[uptimeKey] ? data.uptimeList[uptimeKey] : 0; return { id: monitorId, name: `服务 ${monitorId}`, status: this.mapUptimeKumaStatus(status), uptime: parseFloat(uptime || '0'), url: `#${monitorId}`, lastUpdateTime: lastUpdateTime }; });
return services;
} catch (error) { console.error('获取服务数据失败:', error); throw error; } }
mapUptimeKumaStatus(uptimeStatus) { switch (uptimeStatus) { case 1: return 'normal'; case 0: return 'error'; case 2: return 'warning'; case 3: return 'loading'; default: return 'unknown'; } }
updateStatus(services) { const statusIndicator = document.getElementById('serviceStatusIndicator'); const statusText = document.getElementById('serviceStatusText'); const serviceStatusLink = document.querySelector('.footer-service-status'); if (!statusIndicator || !statusText) { return; }
if (services.length === 0) { statusIndicator.className = 'service-status-indicator status-loading'; statusText.textContent = this.config.statusText.loading; if (serviceStatusLink) { serviceStatusLink.title = '查看项目状态'; } return; }
const totalServices = services.length; const normalServices = services.filter(s => s.status === 'normal').length; const warningServices = services.filter(s => s.status === 'warning').length; const errorServices = services.filter(s => s.status === 'error').length;
let status = 'normal'; let statusTextContent = '';
if (errorServices > 0) { status = 'error'; statusTextContent = `${errorServices}个服务异常`; } else if (warningServices > 0) { status = 'warning'; statusTextContent = `${warningServices}个服务警告`; } else if (normalServices === totalServices) { status = 'normal'; statusTextContent = '所有服务正常'; } else { status = 'warning'; statusTextContent = `${normalServices}/${totalServices}个服务正常`; }
const latestUpdateTime = this.getLatestUpdateTime(services);
statusIndicator.className = `service-status-indicator status-${status}`; statusText.textContent = statusTextContent; if (serviceStatusLink) { serviceStatusLink.title = `查看项目状态\n最后更新: ${latestUpdateTime}`; } }
getLatestUpdateTime(services) { if (!services || services.length === 0) { return '未知'; }
const updateTimes = services .map(service => service.lastUpdateTime) .filter(time => time) .map(time => { const apiTime = new Date(time); const adjustedTime = new Date(apiTime.getTime() + 8 * 60 * 60 * 1000); return adjustedTime; });
if (updateTimes.length === 0) { return '未知'; }
const latestTime = new Date(Math.max(...updateTimes)); return latestTime.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }); } }
new ServiceStatus();
|