/** * 车辆数据页面组件 */ export default class Vehicle { constructor(container, route) { this.container = container; this.route = route; this.init(); } async init() { try { this.render(); this.bindEvents(); this.loadVehicleData(); } catch (error) { console.error('Vehicle init error:', error); this.showError(error); } } render() { this.container.innerHTML = `
数据查询
车辆数据列表
共 0 条数据
ID 车辆ID 车架号 数据类型 数据内容 时间戳 操作
加载中...
`; } bindEvents() { // 查询按钮 document.getElementById('search-btn').addEventListener('click', () => { this.loadVehicleData(); }); // 初始化示例数据 document.getElementById('init-sample-btn').addEventListener('click', () => { this.initSampleData(); }); // 添加数据按钮 document.getElementById('add-data-btn').addEventListener('click', () => { this.showAddDataModal(); }); // 保存数据 document.getElementById('save-data-btn').addEventListener('click', () => { this.saveVehicleData(); }); // 输入框回车查询 ['vehicle_id', 'vehicle_vin'].forEach(id => { document.getElementById(id).addEventListener('keypress', (e) => { if (e.key === 'Enter') { this.loadVehicleData(); } }); }); } async loadVehicleData() { const vehicleId = document.getElementById('vehicle_id').value.trim(); const vehicleVin = document.getElementById('vehicle_vin').value.trim(); const dataType = document.getElementById('data_type').value; const limit = document.getElementById('limit').value; const params = new URLSearchParams(); if (vehicleId) params.append('vehicle_id', vehicleId); if (vehicleVin) params.append('vehicle_vin', vehicleVin); if (dataType) params.append('data_type', dataType); if (limit) params.append('limit', limit); try { const response = await fetch(`/api/vehicle/data?${params}`); const data = await response.json(); if (Array.isArray(data)) { this.renderVehicleData(data); document.getElementById('data-count').textContent = `共 ${data.length} 条数据`; } else { this.showErrorInTable('数据格式错误'); } } catch (error) { console.error('加载车辆数据失败:', error); this.showErrorInTable('加载数据失败'); } } renderVehicleData(data) { const tbody = document.getElementById('vehicle-data-body'); if (data.length === 0) { tbody.innerHTML = ` 暂无数据 `; return; } tbody.innerHTML = data.map(item => { const timestamp = new Date(item.timestamp).toLocaleString(); let dataValue = item.data_value; try { const parsed = JSON.parse(dataValue); dataValue = JSON.stringify(parsed, null, 2); } catch (e) { // 如果不是JSON格式,保持原样 } return ` ${item.id} ${item.vehicle_id} ${item.vehicle_vin || '-'} ${item.data_type}
${dataValue}
${timestamp} `; }).join(''); } showErrorInTable(message) { const tbody = document.getElementById('vehicle-data-body'); tbody.innerHTML = ` ${message} `; } async initSampleData() { if (!confirm('确定要初始化示例车辆数据吗?这将会添加一些测试数据。')) { return; } try { const response = await fetch('/api/vehicle/init-sample-data', { method: 'POST' }); const data = await response.json(); if (data.success) { if (window.showToast) { window.showToast('示例数据初始化成功', 'success'); } this.loadVehicleData(); } else { if (window.showToast) { window.showToast(data.message || '初始化失败', 'error'); } } } catch (error) { console.error('初始化示例数据失败:', error); if (window.showToast) { window.showToast('网络错误', 'error'); } } } showAddDataModal() { const modal = new bootstrap.Modal(document.getElementById('addDataModal')); modal.show(); } async saveVehicleData() { const form = document.getElementById('add-data-form'); const formData = new FormData(form); const data = { vehicle_id: formData.get('vehicle_id'), vehicle_vin: formData.get('vehicle_vin') || null, data_type: formData.get('data_type'), data_value: formData.get('data_value') }; // 验证JSON格式 try { JSON.parse(data.data_value); } catch (e) { if (window.showToast) { window.showToast('数据内容必须是有效的JSON格式', 'error'); } return; } try { const response = await fetch('/api/vehicle/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (result.success) { if (window.showToast) { window.showToast('数据添加成功', 'success'); } // 关闭模态框 const modal = bootstrap.Modal.getInstance(document.getElementById('addDataModal')); modal.hide(); // 清空表单 form.reset(); // 重新加载数据 this.loadVehicleData(); } else { if (window.showToast) { window.showToast(result.message || '添加失败', 'error'); } } } catch (error) { console.error('保存数据失败:', error); if (window.showToast) { window.showToast('网络错误', 'error'); } } } showError(error) { this.container.innerHTML = `

页面加载失败

${error.message || '未知错误'}

`; } } // 全局函数,用于查看数据详情 window.showDataDetails = function(id) { // 这里可以实现查看详细数据的功能 console.log('查看数据详情:', id); if (window.showToast) { window.showToast('详情查看功能开发中', 'info'); } };