Files
MC_Report/backend/routes/bans.js
canglan 8c5ce78fd0 chore: add AGPLv3 copyright header to all source files
- 52 JS files (backend + public/js): header with
  Copyright (C) 2026 Sea Network Technology Studio
  Author: CangLan <admin@sea-studio.top>
  + AGPLv3 notice
- idempotent (skips if header present), all syntax-checked
2026-08-19 20:27:05 +08:00

93 lines
4.3 KiB
JavaScript

/*
* MC Report System
* Copyright (C) 2026 Sea Network Technology Studio
* Author: CangLan <admin@sea-studio.top>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express');
const { query, getRow } = require('../db');
const { authenticate, requireRole } = require('../middleware/auth');
const router = express.Router();
function parseDuration(d) {
if (!d) return 0;
const m = String(d).match(/(\d+)/);
return m ? parseInt(m[1]) : 0;
}
router.get('/', authenticate, async (req, res) => {
const rows = await query('SELECT * FROM bans ORDER BY created_at DESC LIMIT 200');
res.json(rows);
});
router.get('/active', authenticate, async (req, res) => {
res.json(await query("SELECT * FROM bans WHERE status = 'active' ORDER BY created_at DESC"));
});
router.get('/min-duration', authenticate, async (req, res) => {
const { player_name, player_uid, type } = req.query;
const prev = await getRow('SELECT * FROM bans WHERE (player_name = ? OR player_uid = ?) AND type = ? AND status = ? ORDER BY created_at DESC LIMIT 1',
[player_name||'', player_uid||'', type||'ban', 'active']);
res.json({ minDays: prev ? parseDuration(prev.duration) : 0 });
});
router.post('/', authenticate, requireRole('owner'), async (req, res) => {
try {
const { player_name, player_uid, reason, type, duration, ticket_id, expires_at, source } = req.body;
if (!player_name) return res.status(400).json({ error: '玩家名为必填' });
const durDays = parseDuration(duration);
const prev = await getRow('SELECT * FROM bans WHERE (player_name = ? OR player_uid = ?) AND type = ? AND status = ? ORDER BY created_at DESC LIMIT 1',
[player_name, player_uid||'', type||'ban', 'active']);
if (prev && durDays > 0 && parseDuration(prev.duration) > durDays) {
return res.status(400).json({ error: `该玩家已有${parseDuration(prev.duration)}天处罚记录,新处罚时长不可少于上次(需 ≥ ${parseDuration(prev.duration)}天)` });
}
const r = await query('INSERT INTO bans(player_name, player_uid, source, reason, type, duration, ticket_id, created_by, expires_at) VALUES (?,?,?,?,?,?,?,?,?)',
[player_name, player_uid||'', source||'', reason||'', type||'ban', duration||'', ticket_id||null, req.user.id, expires_at||null]);
if (ticket_id) {
await query("INSERT INTO responses(ticket_id, user_id, content, is_staff) VALUES (?,?,?,1)",
[ticket_id, req.user.id, `【处罚记录】${type==='ban'?'封禁':type==='mute'?'禁言':type==='warn'?'警告':'其他'}: ${player_name}${duration?' 时长:'+duration:''}${reason?' 原因:'+reason:''}`]);
}
res.status(201).json({ id: r.insertId });
} catch (err) {
console.error('[bans]', err);
res.status(500).json({ error: '添加失败: ' + err.message });
}
});
router.put('/:id', authenticate, requireRole('owner'), async (req, res) => {
const fields = {};
if (req.body.status) {
if (!['active','expired','appealed','lifted'].includes(req.body.status)) return res.status(400).json({ error: '无效的状态值' });
fields.status = req.body.status;
}
if (req.body.reason !== undefined) fields.reason = req.body.reason;
if (req.body.duration !== undefined) fields.duration = req.body.duration;
if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
const sets = Object.keys(fields).map(k => `${k} = ?`).join(', ');
await query(`UPDATE bans SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]);
res.json({ message: '更新成功' });
});
router.delete('/:id', authenticate, requireRole('owner'), async (req, res) => {
await query('DELETE FROM bans WHERE id = ?', [req.params.id]);
res.json({ message: '已删除' });
});
module.exports = router;