XNSim/XNSimHtml/routes/model-dev.js

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2025-04-28 12:25:20 +08:00
const express = require('express');
const router = express.Router();
const {
getATAChapters,
getModelsByChapterId,
getModelVersionsByClassName,
saveModelVersion
} = require('../utils/model-utils');
2025-04-28 12:25:20 +08:00
// 获取所有ATA章节
router.get('/ata-chapters', (req, res) => {
try {
const chapters = getATAChapters();
res.json(chapters);
} catch (error) {
console.error('处理ATA章节请求时出错:', error);
res.status(500).json({ error: '获取ATA章节数据失败', details: error.message });
}
});
// 获取指定章节ID的模型
router.get('/chapter-models/:chapterId', (req, res) => {
try {
const { chapterId } = req.params;
const { planeName } = req.query;
2025-04-28 12:25:20 +08:00
if (!chapterId) {
return res.status(400).json({ error: '缺少章节ID参数' });
}
if (!planeName) {
return res.status(400).json({ error: '缺少飞机名称参数' });
}
2025-04-28 12:25:20 +08:00
const models = getModelsByChapterId(chapterId, planeName);
2025-04-28 12:25:20 +08:00
res.json(models);
} catch (error) {
console.error('处理章节模型请求时出错:', error);
res.status(500).json({ error: '获取章节模型数据失败', details: error.message });
}
});
// 获取指定ClassName的模型版本
router.get('/model-versions/:className', (req, res) => {
try {
const className = req.params.className;
const { planeName, confID } = req.query;
2025-04-28 12:25:20 +08:00
if (!className) {
return res.status(400).json({ error: '模型类名不能为空' });
}
if (!planeName) {
return res.status(400).json({ error: '飞机名称不能为空' });
}
if (!confID) {
return res.status(400).json({ error: '配置ID不能为空' });
}
2025-04-28 12:25:20 +08:00
const versions = getModelVersionsByClassName(className, planeName, confID);
2025-04-28 12:25:20 +08:00
res.json(versions);
} catch (error) {
console.error(`获取模型版本失败: ${error.message}`);
res.status(500).json({ error: '获取模型版本失败', details: error.message });
}
});
// 保存模型版本信息
router.post('/model-versions', (req, res) => {
try {
const versionData = req.body;
if (!versionData || !versionData.ClassName) {
return res.status(400).json({ error: '缺少必要的模型版本数据' });
}
// 验证必填字段
const requiredFields = ['ClassName', 'Name', 'Version', 'Author', 'PlaneName', 'ConfID'];
for (const field of requiredFields) {
if (!versionData[field]) {
return res.status(400).json({ error: `${field} 是必填字段` });
}
}
// 验证 CmdList 是否为有效的 JSON 字符串
if (versionData.CmdList !== undefined) {
try {
JSON.parse(versionData.CmdList);
} catch (e) {
return res.status(400).json({ error: 'CmdList 必须是有效的 JSON 字符串' });
}
}
2025-04-28 12:25:20 +08:00
const result = saveModelVersion(versionData);
res.json(result);
} catch (error) {
console.error(`保存模型版本失败: ${error.message}`);
res.status(500).json({ error: '保存模型版本失败', details: error.message });
}
});
module.exports = router;