| | |
| | | const response = await fetch(api + path, { ...options, headers: { 'Content-Type': 'application/json' } }); |
| | | if (!response.ok) { |
| | | let message = `请求失败 (${response.status})`; |
| | | try { message = (await response.json()).message || message; } catch (_) { } |
| | | try { const error = await response.json(); message = error?.msg || error?.message || message; } catch (_) { } |
| | | throw new Error(message); |
| | | } |
| | | return response.json(); |
| | | const payload = await response.json(); |
| | | if (payload && payload.data && typeof payload.data === 'object' |
| | | && !Array.isArray(payload.devices) && payload.data.devices) return payload.data; |
| | | return payload; |
| | | } |
| | | |
| | | function showError(error) { |
| | |
| | | |
| | | function render(state) { |
| | | errorBox.classList.remove('visible'); |
| | | if (!state || typeof state !== 'object') throw new Error('模拟器响应缺少状态数据'); |
| | | if (state.data && typeof state.data === 'object' && !Array.isArray(state.devices)) state = state.data; |
| | | state = { |
| | | ...state, |
| | | devices: Array.isArray(state.devices) ? state.devices : [], |
| | | collectors: Array.isArray(state.collectors) ? state.collectors : [], |
| | | recentRequests: Array.isArray(state.recentRequests) ? state.recentRequests : [], |
| | | }; |
| | | const runtime = document.querySelector('#runtime'); |
| | | runtime.textContent = state.running ? '运行中' : '已停止'; |
| | | runtime.classList.toggle('online', state.running); |