chore: 支持背景音乐设置

This commit is contained in:
2025-12-15 17:44:10 +08:00
parent 4a829b7dfc
commit 5fad2f97f8
8 changed files with 890 additions and 707 deletions

View File

@@ -32,6 +32,7 @@ const storage = multer.diskStorage({
}
});
// 文件上传
const upload = multer({
storage,
limits: { fileSize: 5 * 1024 * 1024 }, // 5MB限制
@@ -47,6 +48,23 @@ const upload = multer({
}
});
// 音乐上传
const musicUpload = multer({
storage,
limits: { fileSize: 10 * 1024 * 1024 }, // 10MB音乐文件限制
fileFilter: (req, file, cb) => {
// 仅允许MP3格式
const allowedTypes = /mp3/;
const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = allowedTypes.test(file.mimetype) || file.mimetype === 'audio/mpeg';
if (extname && mimetype) {
return cb(null, true);
} else {
cb(new Error('只允许上传MP3格式的音频文件'));
}
}
});
// 中间件
app.use(cors());
app.use(express.json());
@@ -77,6 +95,7 @@ app.post('/api/config', (req, res) => {
}
});
// API: 上传图片
app.post('/api/upload', upload.single('image'), (req, res) => {
try {
@@ -115,6 +134,73 @@ app.delete('/api/upload/:filename', (req, res) => {
}
});
// API: 上传音乐
app.post('/api/music', musicUpload.single('music'), (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: '没有文件上传' });
}
// 返回文件的相对路径和完整信息
const relativePath = `/uploads/${req.file.filename}`;
res.json({
success: true,
filePath: relativePath,
filename: req.file.filename,
originalName: req.file.originalname,
size: req.file.size
});
} catch (error) {
console.error('音乐上传失败:', error);
res.status(500).json({ error: error.message || '音乐上传失败' });
}
});
// API: 删除音乐
app.delete('/api/music/:filename', (req, res) => {
try {
const filename = req.params.filename;
const filePath = path.join(uploadDir, filename);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
res.json({ success: true });
} else {
res.status(404).json({ error: '音乐文件不存在' });
}
} catch (error) {
console.error('音乐删除失败:', error);
res.status(500).json({ error: '音乐删除失败' });
}
});
// API: 获取音乐文件列表
app.get('/api/music', (req, res) => {
try {
const files = fs.readdirSync(uploadDir)
.filter(file => {
const ext = path.extname(file).toLowerCase();
return ['.mp3', '.wav', '.ogg', '.m4a'].includes(ext);
})
.map(file => {
const filePath = path.join(uploadDir, file);
const stats = fs.statSync(filePath);
return {
filename: file,
filePath: `/uploads/${file}`,
size: stats.size,
createdAt: stats.birthtime.toISOString(),
modifiedAt: stats.mtime.toISOString()
};
});
res.json({ success: true, files });
} catch (error) {
console.error('获取音乐列表失败:', error);
res.status(500).json({ error: '获取音乐列表失败' });
}
});
// 处理Vue Router历史模式 - 使用正则表达式代替通配符
app.get(/^((?!\/api).)*$/, (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));