const express = require('express'); const router = express.Router(); const path = require('path'); const fs = require('fs'); // 模拟PDF资料数据库(实际项目中应使用真实数据库) const pdfMaterials = [ { id: 1, aircraftId: 1, title: '波音737操作手册', fileName: 'B737_manual.pdf', description: '波音737详细操作手册', uploadDate: '2024-01-15', fileSize: '2.5MB' }, { id: 2, aircraftId: 1, title: '波音737维护手册', fileName: 'B737_maintenance.pdf', description: '波音737维护保养手册', uploadDate: '2024-01-20', fileSize: '3.2MB' }, { id: 3, aircraftId: 2, title: '空客A320操作手册', fileName: 'A320_manual.pdf', description: '空客A320详细操作手册', uploadDate: '2024-02-01', fileSize: '2.8MB' }, { id: 4, aircraftId: 2, title: '空客A320技术规格', fileName: 'A320_specs.pdf', description: '空客A320技术规格说明', uploadDate: '2024-02-05', fileSize: '1.5MB' }, { id: 5, aircraftId: 3, title: '波音787操作手册', fileName: 'B787_manual.pdf', description: '波音787详细操作手册', uploadDate: '2024-02-10', fileSize: '4.1MB' }, { id: 6, aircraftId: 4, title: '空客A350操作手册', fileName: 'A350_manual.pdf', description: '空客A350详细操作手册', uploadDate: '2024-02-15', fileSize: '3.9MB' } ]; // 获取机型的PDF资料列表 router.get('/aircraft/:aircraftId', (req, res) => { try { const aircraftId = parseInt(req.params.aircraftId); const { search } = req.query; let materials = pdfMaterials.filter(m => m.aircraftId === aircraftId); if (search) { const searchLower = search.toLowerCase(); materials = materials.filter(m => m.title.toLowerCase().includes(searchLower) || m.description.toLowerCase().includes(searchLower) ); } res.json({ success: true, data: materials }); } catch (error) { console.error('获取PDF资料错误:', error); res.status(500).json({ error: '服务器内部错误' }); } }); // 获取所有PDF资料 router.get('/list', (req, res) => { try { const { search } = req.query; let materials = pdfMaterials; if (search) { const searchLower = search.toLowerCase(); materials = materials.filter(m => m.title.toLowerCase().includes(searchLower) || m.description.toLowerCase().includes(searchLower) ); } res.json({ success: true, data: materials }); } catch (error) { console.error('获取PDF资料列表错误:', error); res.status(500).json({ error: '服务器内部错误' }); } }); // 获取PDF文件URL(实际项目中应该检查文件是否存在) router.get('/file/:id', (req, res) => { try { const id = parseInt(req.params.id); const material = pdfMaterials.find(m => m.id === id); if (!material) { return res.status(404).json({ error: '资料不存在' }); } // 返回PDF文件的URL // 实际项目中,这里应该检查文件是否真实存在 const fileUrl = `/uploads/${material.fileName}`; res.json({ success: true, data: { id: material.id, title: material.title, fileUrl: fileUrl, fileName: material.fileName } }); } catch (error) { console.error('获取PDF文件错误:', error); res.status(500).json({ error: '服务器内部错误' }); } }); module.exports = router;