2025-04-30 14:50:27 +08:00
|
|
|
const express = require('express');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs').promises;
|
|
|
|
const xlsx = require('xlsx');
|
|
|
|
const multer = require('multer');
|
2025-05-07 13:46:48 +08:00
|
|
|
const {
|
|
|
|
getDataInterfaces,
|
|
|
|
addDataInterface,
|
|
|
|
updateDataInterface,
|
|
|
|
deleteDataInterface,
|
2025-05-15 16:59:12 +08:00
|
|
|
getDataInterfaceStructs
|
|
|
|
} = require('../utils/data-interface-utils');
|
2025-04-30 14:50:27 +08:00
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
const upload = multer({ storage: multer.memoryStorage() });
|
|
|
|
|
|
|
|
// 数据文件路径
|
|
|
|
const DATA_FILE = path.join(__dirname, '../data/interface.json');
|
|
|
|
|
|
|
|
// 确保数据目录存在
|
|
|
|
async function ensureDataDirectory() {
|
|
|
|
const dataDir = path.dirname(DATA_FILE);
|
|
|
|
try {
|
|
|
|
await fs.access(dataDir);
|
|
|
|
} catch {
|
|
|
|
await fs.mkdir(dataDir, { recursive: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取接口列表
|
|
|
|
router.get('/list', async (req, res) => {
|
|
|
|
try {
|
2025-05-15 16:59:12 +08:00
|
|
|
const { systemName, confID } = req.query;
|
|
|
|
if (!confID) {
|
|
|
|
return res.status(400).json({ error: 'ConfID 是必填字段' });
|
|
|
|
}
|
|
|
|
const interfaces = await getDataInterfaces(systemName, confID);
|
2025-04-30 14:50:27 +08:00
|
|
|
res.json(interfaces);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('获取接口列表失败:', error);
|
|
|
|
res.status(500).json({ error: '获取接口列表失败' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 添加接口
|
|
|
|
router.post('/add', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const result = await addDataInterface(req.body);
|
|
|
|
res.json(result);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('添加接口失败:', error);
|
|
|
|
res.status(500).json({ error: error.message || '添加接口失败' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 更新接口
|
|
|
|
router.put('/update', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const result = await updateDataInterface(req.body);
|
|
|
|
res.json(result);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('更新接口失败:', error);
|
|
|
|
res.status(500).json({ error: error.message || '更新接口失败' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 删除接口
|
|
|
|
router.delete('/delete', async (req, res) => {
|
|
|
|
try {
|
2025-05-15 16:59:12 +08:00
|
|
|
const { interfaceName, confID } = req.query;
|
|
|
|
if (!interfaceName || !confID) {
|
|
|
|
return res.status(400).json({ error: 'InterfaceName 和 ConfID 是必填字段' });
|
|
|
|
}
|
|
|
|
const result = await deleteDataInterface(interfaceName, confID);
|
2025-04-30 14:50:27 +08:00
|
|
|
res.json(result);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('删除接口失败:', error);
|
|
|
|
res.status(500).json({ error: error.message || '删除接口失败' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-05-07 13:46:48 +08:00
|
|
|
// 获取接口结构体列表
|
|
|
|
router.get('/struct/list', async (req, res) => {
|
|
|
|
try {
|
2025-05-15 16:59:12 +08:00
|
|
|
const { systemName, planeName, ataName, confID } = req.query;
|
|
|
|
if (!confID) {
|
|
|
|
return res.status(400).json({ error: 'ConfID 是必填字段' });
|
|
|
|
}
|
|
|
|
const structs = await getDataInterfaceStructs(systemName, planeName, ataName, confID);
|
2025-05-07 13:46:48 +08:00
|
|
|
res.json(structs);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('获取接口结构体列表失败:', error);
|
|
|
|
res.status(500).json({ error: '获取接口结构体列表失败' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-04-30 14:50:27 +08:00
|
|
|
// 导入数据
|
2025-05-07 13:46:48 +08:00
|
|
|
router.post('/import', async (req, res) => {
|
2025-04-30 14:50:27 +08:00
|
|
|
try {
|
2025-05-07 13:46:48 +08:00
|
|
|
const importData = req.body;
|
|
|
|
if (!Array.isArray(importData)) {
|
|
|
|
return res.status(400).json({ error: '导入数据格式错误' });
|
2025-04-30 14:50:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const results = [];
|
2025-05-07 13:46:48 +08:00
|
|
|
for (const item of importData) {
|
2025-04-30 14:50:27 +08:00
|
|
|
try {
|
|
|
|
// 验证所有必填字段
|
2025-05-07 13:46:48 +08:00
|
|
|
const requiredFields = [
|
|
|
|
'SystemName',
|
2025-05-15 16:59:12 +08:00
|
|
|
'PlaneName',
|
2025-05-07 13:46:48 +08:00
|
|
|
'ATAName',
|
|
|
|
'ModelStructName',
|
|
|
|
'InterfaceName',
|
|
|
|
'InterfaceType',
|
|
|
|
'InterfaceOption',
|
|
|
|
'InterfaceIsArray',
|
|
|
|
'InterfaceArraySize_1',
|
|
|
|
'InterfaceArraySize_2',
|
2025-05-15 16:59:12 +08:00
|
|
|
'ConfID'
|
2025-05-07 13:46:48 +08:00
|
|
|
];
|
2025-04-30 14:50:27 +08:00
|
|
|
|
|
|
|
// 检查所有必填字段是否存在
|
2025-05-07 13:46:48 +08:00
|
|
|
for (const field of requiredFields) {
|
|
|
|
if (item[field] === undefined || item[field] === null || item[field] === '') {
|
|
|
|
throw new Error(`字段 "${field}" 是必填字段`);
|
2025-04-30 14:50:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-07 13:46:48 +08:00
|
|
|
const result = await addDataInterface(item);
|
|
|
|
results.push({ success: true, data: item });
|
2025-04-30 14:50:27 +08:00
|
|
|
} catch (error) {
|
|
|
|
results.push({ success: false, error: error.message, data: item });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
results,
|
|
|
|
message: `导入完成,成功: ${results.filter(r => r.success).length}, 失败: ${results.filter(r => !r.success).length}`
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('导入数据失败:', error);
|
|
|
|
res.status(500).json({ error: '导入数据失败' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|