init commit

This commit is contained in:
huanglinhuan
2025-12-03 22:20:43 +08:00
commit aaee847593
9 changed files with 495 additions and 0 deletions

120
routes/aircraft.js Executable file
View File

@@ -0,0 +1,120 @@
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;

90
routes/auth.js Executable file
View File

@@ -0,0 +1,90 @@
const express = require('express');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const { JWT_SECRET } = require('../middleware/auth');
const router = express.Router();
// 模拟用户数据库(实际项目中应使用真实数据库)
const users = [
{
id: 1,
username: 'admin',
password: '$2a$10$rOzJqJqJqJqJqJqJqJqJqOqJqJqJqJqJqJqJqJqJqJqJqJqJqJq', // password: admin123
name: '管理员'
}
];
// 初始化默认用户密码admin123
async function initDefaultUser() {
const hashedPassword = await bcrypt.hash('admin123', 10);
users[0].password = hashedPassword;
}
initDefaultUser();
// 登录接口
router.post('/login', async (req, res) => {
try {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: '用户名和密码不能为空' });
}
// 查找用户
const user = users.find(u => u.username === username);
if (!user) {
return res.status(401).json({ error: '用户名或密码错误' });
}
// 验证密码
const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValidPassword) {
return res.status(401).json({ error: '用户名或密码错误' });
}
// 生成JWT token
const token = jwt.sign(
{ id: user.id, username: user.username, name: user.name },
JWT_SECRET,
{ expiresIn: '24h' }
);
res.json({
success: true,
token,
user: {
id: user.id,
username: user.username,
name: user.name
}
});
} catch (error) {
console.error('登录错误:', error);
res.status(500).json({ error: '服务器内部错误' });
}
});
// 验证token接口
router.get('/verify', async (req, res) => {
try {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: '未提供访问令牌' });
}
jwt.verify(token, JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ error: '无效的访问令牌' });
}
res.json({ success: true, user });
});
} catch (error) {
res.status(500).json({ error: '服务器内部错误' });
}
});
module.exports = router;

145
routes/pdf.js Executable file
View File

@@ -0,0 +1,145 @@
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;