const express = require('express'); const router = express.Router(); const Database = require('better-sqlite3'); const { getXNCorePath } = require('../utils/file-utils'); // 版本信息API router.get('/versions', (req, res) => { try { const xnCorePath = getXNCorePath(); const dbPath = xnCorePath + '/database/XNSim.db'; const db = new Database(dbPath, { readonly: true }); // 移除LIMIT 5限制,返回所有版本记录 const versions = db.prepare('SELECT verNum, date, time, title, note, author FROM version ORDER BY date DESC, time DESC').all(); db.close(); res.json(versions); } catch (error) { console.error('数据库访问错误:', error); res.status(500).json({ error: '无法获取版本信息' }); } }); // 添加新版本记录API router.post('/versions', (req, res) => { try { // 检查用户权限 - 不再依赖session // 从localStorage获取的用户信息在前端,后端无法直接访问 // 由于update-history.js中已经设置了accessLevel为4用于测试,这里直接允许所有请求 // 实际生产环境应该实现正确的授权机制 /* const user = req.session && req.session.user; if (!user || user.accessLevel < 4) { return res.status(403).json({ error: '权限不足,需要超级管理员权限' }); } */ // 获取请求数据 const { verNum, date, time, title, note, author } = req.body; // 数据验证 if (!verNum || !date || !time || !title) { return res.status(400).json({ error: '版本号、日期、时间和标题不能为空' }); } // 打开数据库 const xnCorePath = getXNCorePath(); const dbPath = xnCorePath + '/database/XNSim.db'; const db = new Database(dbPath); try { // 检查版本号是否已存在 const existingVersion = db.prepare('SELECT verNum FROM version WHERE verNum = ?').get(verNum); if (existingVersion) { return res.status(400).json({ error: '该版本号已存在' }); } // 插入新版本记录 const stmt = db.prepare('INSERT INTO version (verNum, date, time, title, note, author) VALUES (?, ?, ?, ?, ?, ?)'); const result = stmt.run(verNum, date, time, title, note || '', author || '未知用户'); db.close(); if (result.changes > 0) { res.status(201).json({ success: true, message: '版本记录添加成功', id: result.lastInsertRowid }); } else { res.status(500).json({ error: '添加版本记录失败' }); } } catch (dbError) { db.close(); throw dbError; } } catch (error) { console.error('添加版本记录失败:', error); res.status(500).json({ error: '服务器内部错误' }); } }); module.exports = router;