121 lines
3.2 KiB
JavaScript
Executable File
121 lines
3.2 KiB
JavaScript
Executable File
const express = require('express');
|
||
const router = express.Router();
|
||
|
||
// 模拟机型数据库(实际项目中应使用真实数据库)
|
||
const aircrafts = [
|
||
{
|
||
id: 1,
|
||
name: '波音737',
|
||
code: 'B737',
|
||
manufacturer: '波音公司',
|
||
type: '窄体客机',
|
||
description: '波音737是波音公司生产的中短程双发窄体客机,是全球最畅销的商用喷气式客机。',
|
||
specifications: {
|
||
maxPassengers: 215,
|
||
maxRange: '5700km',
|
||
cruiseSpeed: '840km/h',
|
||
length: '39.5m',
|
||
wingspan: '35.8m'
|
||
}
|
||
},
|
||
{
|
||
id: 2,
|
||
name: '空客A320',
|
||
code: 'A320',
|
||
manufacturer: '空中客车公司',
|
||
type: '窄体客机',
|
||
description: '空客A320是空中客车公司生产的单通道窄体客机,是空客最成功的机型之一。',
|
||
specifications: {
|
||
maxPassengers: 180,
|
||
maxRange: '6150km',
|
||
cruiseSpeed: '840km/h',
|
||
length: '37.57m',
|
||
wingspan: '35.8m'
|
||
}
|
||
},
|
||
{
|
||
id: 3,
|
||
name: '波音787',
|
||
code: 'B787',
|
||
manufacturer: '波音公司',
|
||
type: '宽体客机',
|
||
description: '波音787梦想客机是波音公司生产的双发宽体中远程客机,采用大量复合材料。',
|
||
specifications: {
|
||
maxPassengers: 330,
|
||
maxRange: '15700km',
|
||
cruiseSpeed: '913km/h',
|
||
length: '57m',
|
||
wingspan: '60m'
|
||
}
|
||
},
|
||
{
|
||
id: 4,
|
||
name: '空客A350',
|
||
code: 'A350',
|
||
manufacturer: '空中客车公司',
|
||
type: '宽体客机',
|
||
description: '空客A350是空中客车公司生产的双发宽体远程客机,采用先进材料和设计。',
|
||
specifications: {
|
||
maxPassengers: 440,
|
||
maxRange: '15000km',
|
||
cruiseSpeed: '903km/h',
|
||
length: '66.8m',
|
||
wingspan: '64.75m'
|
||
}
|
||
}
|
||
];
|
||
|
||
// 获取所有机型列表
|
||
router.get('/list', (req, res) => {
|
||
try {
|
||
const { search } = req.query;
|
||
|
||
let filteredAircrafts = aircrafts;
|
||
if (search) {
|
||
const searchLower = search.toLowerCase();
|
||
filteredAircrafts = aircrafts.filter(aircraft =>
|
||
aircraft.name.toLowerCase().includes(searchLower) ||
|
||
aircraft.code.toLowerCase().includes(searchLower) ||
|
||
aircraft.manufacturer.toLowerCase().includes(searchLower)
|
||
);
|
||
}
|
||
|
||
res.json({
|
||
success: true,
|
||
data: filteredAircrafts.map(aircraft => ({
|
||
id: aircraft.id,
|
||
name: aircraft.name,
|
||
code: aircraft.code,
|
||
manufacturer: aircraft.manufacturer,
|
||
type: aircraft.type
|
||
}))
|
||
});
|
||
} catch (error) {
|
||
console.error('获取机型列表错误:', error);
|
||
res.status(500).json({ error: '服务器内部错误' });
|
||
}
|
||
});
|
||
|
||
// 获取单个机型详细信息
|
||
router.get('/:id', (req, res) => {
|
||
try {
|
||
const id = parseInt(req.params.id);
|
||
const aircraft = aircrafts.find(a => a.id === id);
|
||
|
||
if (!aircraft) {
|
||
return res.status(404).json({ error: '机型不存在' });
|
||
}
|
||
|
||
res.json({
|
||
success: true,
|
||
data: aircraft
|
||
});
|
||
} catch (error) {
|
||
console.error('获取机型详情错误:', error);
|
||
res.status(500).json({ error: '服务器内部错误' });
|
||
}
|
||
});
|
||
|
||
module.exports = router;
|
||
|