228 lines
7.3 KiB
JavaScript
228 lines
7.3 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
const fs = require('fs').promises;
|
|
const xlsx = require('xlsx');
|
|
const multer = require('multer');
|
|
const {
|
|
getDataInterfaces,
|
|
addDataInterface,
|
|
updateDataInterface,
|
|
deleteDataInterface,
|
|
getDataInterfaceStructs,
|
|
addDataInterfaceStruct,
|
|
updateDataInterfaceStruct,
|
|
deleteDataInterfaceStruct
|
|
} = require('../utils/db-utils');
|
|
|
|
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 });
|
|
}
|
|
}
|
|
|
|
// 读取数据
|
|
async function readData() {
|
|
try {
|
|
await ensureDataDirectory();
|
|
const data = await fs.readFile(DATA_FILE, 'utf8');
|
|
return JSON.parse(data);
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
return [];
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// 保存数据
|
|
async function saveData(data) {
|
|
await ensureDataDirectory();
|
|
await fs.writeFile(DATA_FILE, JSON.stringify(data, null, 2));
|
|
}
|
|
|
|
// 获取接口列表
|
|
router.get('/list', async (req, res) => {
|
|
try {
|
|
const { systemName, productName } = req.query;
|
|
const interfaces = await getDataInterfaces(systemName, productName);
|
|
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 {
|
|
const { systemName, productName, ataName, modelStructName, interfaceName } = req.query;
|
|
const result = await deleteDataInterface(systemName, productName, ataName, modelStructName, interfaceName);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除接口失败:', error);
|
|
res.status(500).json({ error: error.message || '删除接口失败' });
|
|
}
|
|
});
|
|
|
|
// 获取接口结构体列表
|
|
router.get('/struct/list', async (req, res) => {
|
|
try {
|
|
const { systemName, productName, ataName } = req.query;
|
|
const structs = await getDataInterfaceStructs(systemName, productName, ataName);
|
|
res.json(structs);
|
|
} catch (error) {
|
|
console.error('获取接口结构体列表失败:', error);
|
|
res.status(500).json({ error: '获取接口结构体列表失败' });
|
|
}
|
|
});
|
|
|
|
// 添加接口结构体
|
|
router.post('/struct/add', async (req, res) => {
|
|
try {
|
|
const result = await addDataInterfaceStruct(req.body);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('添加接口结构体失败:', error);
|
|
res.status(500).json({ error: error.message || '添加接口结构体失败' });
|
|
}
|
|
});
|
|
|
|
// 更新接口结构体
|
|
router.put('/struct/update', async (req, res) => {
|
|
try {
|
|
const result = await updateDataInterfaceStruct(req.body);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('更新接口结构体失败:', error);
|
|
res.status(500).json({ error: error.message || '更新接口结构体失败' });
|
|
}
|
|
});
|
|
|
|
// 删除接口结构体
|
|
router.delete('/struct/delete', async (req, res) => {
|
|
try {
|
|
const { systemName, productName, ataName, modelStructName } = req.query;
|
|
const result = await deleteDataInterfaceStruct(systemName, productName, ataName, modelStructName);
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('删除接口结构体失败:', error);
|
|
res.status(500).json({ error: error.message || '删除接口结构体失败' });
|
|
}
|
|
});
|
|
|
|
// 下载模板
|
|
router.get('/template', async (req, res) => {
|
|
try {
|
|
const workbook = xlsx.utils.book_new();
|
|
const worksheet = xlsx.utils.json_to_sheet([
|
|
{
|
|
'系统名称': 'XNSim',
|
|
'产品名称': 'C909',
|
|
'ATA章节': '',
|
|
'模型结构名': '',
|
|
'接口名': '',
|
|
'接口类型': '',
|
|
'接口选项': '',
|
|
'是否为数组': '',
|
|
'数组大小1': '',
|
|
'数组大小2': '',
|
|
'备注': ''
|
|
}
|
|
]);
|
|
xlsx.utils.book_append_sheet(workbook, worksheet, '接口变量模板');
|
|
|
|
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
res.setHeader('Content-Disposition', 'attachment; filename=接口变量模板.xlsx');
|
|
|
|
const buffer = xlsx.write(workbook, { type: 'buffer', bookType: 'xlsx' });
|
|
res.send(buffer);
|
|
} catch (error) {
|
|
console.error('下载模板失败:', error);
|
|
res.status(500).json({ error: '下载模板失败' });
|
|
}
|
|
});
|
|
|
|
// 导入数据
|
|
router.post('/import', async (req, res) => {
|
|
try {
|
|
const importData = req.body;
|
|
if (!Array.isArray(importData)) {
|
|
return res.status(400).json({ error: '导入数据格式错误' });
|
|
}
|
|
|
|
const results = [];
|
|
for (const item of importData) {
|
|
try {
|
|
// 验证所有必填字段
|
|
const requiredFields = [
|
|
'SystemName',
|
|
'ProductName',
|
|
'ATAName',
|
|
'ModelStructName',
|
|
'InterfaceName',
|
|
'InterfaceType',
|
|
'InterfaceOption',
|
|
'InterfaceIsArray',
|
|
'InterfaceArraySize_1',
|
|
'InterfaceArraySize_2',
|
|
'InterfaceNotes'
|
|
];
|
|
|
|
// 检查所有必填字段是否存在
|
|
for (const field of requiredFields) {
|
|
if (item[field] === undefined || item[field] === null || item[field] === '') {
|
|
throw new Error(`字段 "${field}" 是必填字段`);
|
|
}
|
|
}
|
|
|
|
const result = await addDataInterface(item);
|
|
results.push({ success: true, data: item });
|
|
} 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;
|