// src/js/tools/carInfoTool.js import BaseTool from '../baseTool.js'; import i18n from '../i18n.js'; class CarInfoTool extends BaseTool { constructor() { super('car-info', '车辆信息查询'); this.dom = {}; this.apiUrl = 'https://api.jkyai.top/API/clxxcx.php'; } render() { return `

${i18n.t('tool.carInfo.title', '车辆信息查询')}

${i18n.t('tool.carInfo.description', '查询车辆品牌、系列、价格等详细信息')}

`; } init() { this._log('车辆信息工具初始化'); this.dom = { queryForm: document.getElementById('car-info-query-form'), msgInput: document.getElementById('car-info-msg'), queryBtn: document.getElementById('car-info-query-btn'), resultSection: document.getElementById('car-info-result-section'), carGrid: document.getElementById('car-info-car-grid'), loadingContainer: document.getElementById('car-info-loading-container'), errorContainer: document.getElementById('car-info-error-container'), errorMessage: document.getElementById('car-info-error-message') }; this.dom.queryForm.addEventListener('submit', (e) => { e.preventDefault(); this.queryCarInfo(); }); // 返回工具箱按钮 document.getElementById('back-to-toolbox-btn')?.addEventListener('click', () => { window.mainPage.navigateTo('toolbox'); window.mainPage.updateActiveNavButton(document.getElementById('toolbox-btn')); }); } async queryCarInfo() { const msg = this.dom.msgInput.value.trim(); if (!msg) { this.showError(i18n.t('tool.carInfo.enterVehicleName', '请输入车辆名称或品牌')); return; } this.showLoading(); const startTime = Date.now(); try { const params = new URLSearchParams(); params.append('msg', msg); params.append('type', 'json'); const requestUrl = `${this.apiUrl}?${params.toString()}`; const response = await fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (!response.ok) { throw new Error(`HTTP Error! Status code: ${response.status}`); } const data = await response.json(); const responseTime = Date.now() - startTime; if ((data.code === 200 || data.code === 201) && data.data) { this.showResults(data.data); this._log(`成功查询车辆信息,耗时 ${responseTime}ms`); } else { this.showError(data.message || i18n.t('tool.carInfo.queryFailed', '查询失败')); this._log(`查询车辆信息失败: ${data.message || '未知错误'}`); } } catch (error) { console.error('Query failed:', error); this.showError(i18n.t('tool.carInfo.queryFailed', '查询失败') + ': ' + error.message); this._log(`查询车辆信息失败: ${error.message}`); } finally { this.hideLoading(); } } showResults(carList) { this.dom.carGrid.innerHTML = ''; if (carList.length > 0) { carList.forEach(car => { const card = this.createCarCard(car); this.dom.carGrid.appendChild(card); }); } else { this.dom.carGrid.innerHTML = `
${i18n.t('tool.carInfo.noResults', '未找到车辆信息')}
`; } this.dom.resultSection.style.display = 'block'; } createCarCard(car) { const card = document.createElement('div'); card.className = 'car-card'; card.style.cssText = 'background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 8px; overflow: hidden; transition: all 0.3s ease;'; card.innerHTML = ` ${car.series_name || 'Vehicle Image'}
${car.brand_name || i18n.t('tool.carInfo.unknownBrand', '未知品牌')}
${car.series_name || i18n.t('tool.carInfo.unknownModel', '未知型号')}
${car.official_price || i18n.t('tool.carInfo.priceNegotiable', '价格面议')}
${car.level || i18n.t('tool.carInfo.unknownLevel', '未知级别')}
`; card.addEventListener('mouseenter', () => { card.style.transform = 'translateY(-2px)'; card.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.1)'; card.style.borderColor = 'var(--primary-color)'; }); card.addEventListener('mouseleave', () => { card.style.transform = 'translateY(0)'; card.style.boxShadow = 'none'; card.style.borderColor = 'var(--border-color)'; }); return card; } showLoading() { this.dom.queryBtn.disabled = true; this.dom.queryBtn.innerHTML = ` ${i18n.t('tool.carInfo.querying', '查询中...')}`; this.dom.loadingContainer.style.display = 'block'; this.dom.resultSection.style.display = 'none'; this.dom.errorContainer.style.display = 'none'; } hideLoading() { this.dom.queryBtn.disabled = false; this.dom.queryBtn.innerHTML = ` ${i18n.t('tool.carInfo.query', '查询车辆信息')}`; this.dom.loadingContainer.style.display = 'none'; } showError(message) { this.dom.errorMessage.textContent = message; this.dom.errorContainer.style.display = 'block'; this.dom.resultSection.style.display = 'none'; this.dom.loadingContainer.style.display = 'none'; this.dom.queryBtn.disabled = false; this.dom.queryBtn.innerHTML = ` ${i18n.t('tool.carInfo.query', '查询车辆信息')}`; } } export default CarInfoTool;